@use 'sass:map';
@use 'sass:string';

@use 'mixins/mixins' as *;
@use 'common/var' as *;

$color-picker-size: () !default;
$color-picker-size: map.merge(
  (
    'default': 36px,
    'small': 32px,
    'large': 44px,
  ),
  $color-picker-size
);

@mixin color-picker-thumb {
  position: absolute;
  cursor: pointer;
  box-sizing: border-box;
  left: 0;
  top: 0;
  width: 4px;
  height: 100%;
  border-radius: 1px;
  background: getCssVar('color-white');
  border: 1px solid getCssVar('color-neutral-2');
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.6);
  z-index: 1;
}

@mixin bar-background($side: right) {
  background: linear-gradient(
    to $side,
    #f00 0%,
    #ff0 17%,
    #0f0 33%,
    #0ff 50%,
    #00f 67%,
    #f0f 83%,
    #f00 100%
  );
}

@include b(color-predefine) {
  display: flex;
  font-size: 12px;
  font-weight: 600;
  margin-top: 8px;
  width: 316px;

  @include e(colors) {
    display: flex;
    flex: 1;
    flex-wrap: wrap;
    margin: -4px;
  }

  @include e(color-selector) {
    margin: 4px;
    width: 24px;
    height: 24px;
    border-radius: 4px;
    cursor: pointer;

    &.selected {
      box-shadow: 0 0 0 2px getCssVar('color-primary-3');
    }

    > div {
      display: flex;
      height: 100%;
      border-radius: 4px;
    }

    @include when(alpha) {
      background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAIAAADZF8uwAAAAGUlEQVQYV2M4gwH+YwCGIasIUwhT25BVBADtzYNYrHvv4gAAAABJRU5ErkJggg==);
    }
  }
}

@include b(color-hue-slider) {
  position: relative;
  box-sizing: border-box;
  width: 310px;
  height: 12px;
  background-color: #f00;
  padding: 0 2px;
  float: right;

  @include e(bar) {
    position: relative;
    @include bar-background;
    height: 100%;
  }

  @include e(thumb) {
    @include color-picker-thumb;
  }

  @include when(vertical) {
    width: 12px;
    height: 180px;
    padding: 4px 0;

    .#{$namespace}-color-hue-slider__bar {
      @include bar-background(bottom);
    }

    .#{$namespace}-color-hue-slider__thumb {
      left: 0;
      top: 0;
      width: 100%;
      height: 4px;
    }
  }
}

@include b(color-svpanel) {
  position: relative;
  width: 310px;
  height: 180px;

  @include e(('white', 'black')) {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }

  @include e('white') {
    background: linear-gradient(to right, #fff, rgba(255, 255, 255, 0));
  }

  @include e('black') {
    background: linear-gradient(to top, #000, rgba(0, 0, 0, 0));
  }

  @include e(cursor) {
    position: absolute;

    > div {
      cursor: head;
      width: 8px;
      height: 8px;
      box-shadow: 0 0 0 1.5px #fff, inset 0 0 1px 1px rgba(0, 0, 0, 0.3),
        0 0 1px 2px rgba(0, 0, 0, 0.4);
      border-radius: 50%;
      transform: translate(-4px, -4px);
    }
  }
}

@mixin alpha-bg {
  background-image: linear-gradient(
      45deg,
      getCssVar('color-picker', 'alpha-bg-a') 25%,
      getCssVar('color-picker', 'alpha-bg-b') 25%
    ),
    linear-gradient(
      135deg,
      getCssVar('color-picker', 'alpha-bg-a') 25%,
      getCssVar('color-picker', 'alpha-bg-b') 25%
    ),
    linear-gradient(
      45deg,
      getCssVar('color-picker', 'alpha-bg-b') 75%,
      getCssVar('color-picker', 'alpha-bg-a') 75%
    ),
    linear-gradient(
      135deg,
      getCssVar('color-picker', 'alpha-bg-b') 75%,
      getCssVar('color-picker', 'alpha-bg-a') 75%
    );
  background-size: 12px 12px;
  background-position: 0 0, 6px 0, 6px -6px, 0 6px;
}

@include b(color-alpha-slider) {
  position: relative;
  box-sizing: border-box;
  width: 310px;
  height: 12px;
  @include alpha-bg;

  @include e(bar) {
    position: relative;
    background: linear-gradient(
      to right,
      rgba(255, 255, 255, 0) 0%,
      getCssVar('color-white') 100%
    );
    height: 100%;
  }

  @include e(thumb) {
    @include color-picker-thumb;
  }

  @include when(vertical) {
    width: 20px;
    height: 180px;

    .#{$namespace}-color-alpha-slider__bar {
      background: linear-gradient(
        to bottom,
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 1) 100%
      );
    }

    .#{$namespace}-color-alpha-slider__thumb {
      left: 0;
      top: 0;
      width: 100%;
      height: 4px;
    }
  }
}

@include b(color-dropdown) {
  @include e(main-wrapper) {
    margin-bottom: 8px;

    &::after {
      content: '';
      display: table;
      clear: both;
    }
  }

  @include e(btns) {
    margin-top: 12px;
    display: flex;
  }

  @include e(value) {
    margin-right: auto;
    width: 168px;
  }

  @include e(btn) {
    margin-left: 8px;
    min-width: 64px;
  }
}

@include b(color-picker) {
  display: inline-block;
  position: relative;

  @include when(disabled) {
    .#{$namespace}-color-picker__trigger {
      cursor: not-allowed;
    }
  }

  @include e(mask) {
    height: map.get($color-picker-size, 'default') - 2px;
    width: map.get($color-picker-size, 'default') - 2px;
    border-radius: getCssVar('border-radius-sm');
    position: absolute;
    top: 1px;
    left: 1px;
    z-index: 1;
    cursor: not-allowed;
    background-color: rgba(255, 255, 255, 0.7);
  }

  @include e(trigger) {
    display: flex;
    justify-content: center;
    align-items: center;

    box-sizing: border-box;
    height: map.get($color-picker-size, 'default');
    width: map.get($color-picker-size, 'default');
    padding: 2px;
    border: getCssVar('border');
    border-radius: getCssVar('border-radius-md');
    font-size: 0;
    position: relative;

    &:hover {
      cursor: pointer;
      box-shadow: getCssVar('box-shadow-lv1');
    }

    @include when(focus) {
      border-color: getCssVar('color-primary-3');
      box-shadow: getCssVar('box-shadow-lv1');
    }
  }

  @include e(color) {
    position: relative;
    display: block;
    box-sizing: border-box;
    width: map.get($color-picker-size, 'default') - 5px;
    height: map.get($color-picker-size, 'default') - 5px;
    border-radius: calc(var(--b-border-radius-md) - 2px);

    @include when(alpha) {
      @include alpha-bg;
      // background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAIAAADZF8uwAAAAGUlEQVQYV2M4gwH+YwCGIasIUwhT25BVBADtzYNYrHvv4gAAAABJRU5ErkJggg==);
    }
  }

  @include e(color-inner) {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    border-radius: string.unquote(
      "calc(var(#{getCssVarName('border-radius-md')}) - 2px)"
    );
  }

  & .#{$namespace}-color-picker__empty {
    font-size: 12px;
    color: getCssVar('color-neutral-8');
  }

  & .#{$namespace}-color-picker__icon {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    color: getCssVar('color-white');
    font-size: 16px;
  }

  @include e(panel) {
    &.#{$namespace}-popper {
      border: 1px solid getCssVar('color-neutral-2');
      padding: 16px;
      width: 364px;
    }
  }

  @each $size in (large, small) {
    @include m($size) {
      .#{$namespace}-color-picker__trigger {
        height: map.get($color-picker-size, $size);
        width: map.get($color-picker-size, $size);
      }

      .#{$namespace}-color-picker__color {
        width: map.get($color-picker-size, $size) - 5px;
        height: map.get($color-picker-size, $size) - 5px;
      }

      .#{$namespace}-color-picker__mask {
        height: map.get($color-picker-size, $size) - 2px;
        width: map.get($color-picker-size, $size) - 2px;
      }
    }
  }

  @include m(large) {
    .#{$namespace}-color-picker__trigger {
      border-radius: getCssVar('border-radius-lg');
    }

    .#{$namespace}-color-picker__color {
      border-radius: calc(var(--b-border-radius-lg) - 2px);
    }

    .#{$namespace}-color-picker__color-inner {
      border-radius: string.unquote(
        "calc(var(#{getCssVarName('border-radius-lg')}) - 2px)"
      );
    }

    .#{$namespace}-color-picker__icon,
    .#{$namespace}-color-picker__empty {
      font-size: 20px;
    }
  }
}

.#{bem('color-picker')},
.#{bem('color-picker', 'panel')} {
  @include set-css-var-value(('color-picker', 'alpha-bg-a'), '#ccc');
  @include set-css-var-value(('color-picker', 'alpha-bg-b'), 'transparent');
}

.dark {
  .#{bem('color-picker')},
  .#{bem('color-picker', 'panel')} {
    @include set-css-var-value(('color-picker', 'alpha-bg-a'), '#333333');
  }
}
