@use 'sass:map';

@use 'mixins/mixins' as *;
@use 'mixins/var' as *;
@use 'common/var' as *;

@include b(tag) {
  @include set-component-css-var('tag', $tag);
}

$tag-border-width: 1px;

$tag-icon-span-gap: () !default;
$tag-icon-span-gap: map.merge(
  (
    'large': 8px,
    'default': 6px,
    'small': 4px,
  ),
  $tag-icon-span-gap
);

@mixin genTheme(
  $backgroundColorWeight,
  $borderColorWeight,
  $textColorWeight,
  $hoverColorWeight
) {
  --sg-tag-bg-color: #{mix(
      map.get($tag-color, 'primary'),
      $color-white,
      $backgroundColorWeight
    )};
  --sg-tag-border-color: #{mix(
      map.get($tag-color, 'primary'),
      $color-white,
      $borderColorWeight
    )};
  --sg-tag-text-color: #{mix(
      map.get($tag-color, 'primary'),
      $color-white,
      $textColorWeight
    )};
  --sg-tag-hover-color: #{mix(
      map.get($tag-color, 'primary'),
      $color-white,
      $hoverColorWeight
    )};

  background-color: var(--sg-tag-bg-color);
  border-color: var(--sg-tag-border-color);
  color: var(--sg-tag-text-color);

  @include when(hit) {
    border-color: map.get($tag-color, 'primary');
  }
  @include when(round) {
    border-radius: map.get($tag, 'border-radius-rounded');
  }

  .#{$namespace}-tag__close {
    color: inherit;
    &:hover {
      color: var(--sg-color-white);
      background-color: var(--sg-tag-hover-color);
    }
  }

  // $types: primary, success, warning, danger, error, info;
  @each $type in $types {
    &.#{$namespace}-tag--#{$type} {
      --sg-tag-bg-color: #{mix(
          map.get($tag-color, $type),
          $color-white,
          $backgroundColorWeight
        )};
      --sg-tag-border-color: #{mix(
          map.get($tag-color, $type),
          $color-white,
          $borderColorWeight
        )};
      --sg-tag-text-color: #{mix(
          map.get($tag-color, $type),
          $color-white,
          $textColorWeight
        )};
      --sg-tag-hover-color: #{mix(
          map.get($tag-color, $type),
          $color-white,
          $hoverColorWeight
        )};

      @include when(hit) {
        border-color: map.get($tag-color, $type);
      }
    }
  }
}

@include b(tag) {
  @include genTheme(10%, 30%, 100%, 100%);
  display: inline-flex;
  justify-content: center;
  align-items: center;

  height: map.get($tag-height, 'default');
  padding: 0 map.get($tag-padding, 'default') - $border-width-base;
  font-size: var(--sg-tag-font-size);
  line-height: 1;
  border-width: $tag-border-width;
  border-style: solid;
  border-radius: var(--sg-tag-border-radius);
  box-sizing: border-box;
  white-space: nowrap;

  --sg-icon-size: 14px;

  .#{$namespace}-icon {
    border-radius: 50%;
    text-align: center;
    position: relative;
    cursor: pointer;

    font-size: calc(var(--sg-icon-size) - 1px);

    height: var(--sg-icon-size);
    width: var(--sg-icon-size);
    line-height: var(--sg-icon-size);

    svg {
      margin: 1px;
    }
  }

  .#{$namespace}-tag__close {
    margin-left: map.get($tag-icon-span-gap, 'default');
  }

  @include m(dark) {
    @include genTheme(100%, 100%, 0, 80%);
  }

  @include m(plain) {
    @include genTheme(0, 40%, 100%, 100%);
  }

  &.is-closable {
    padding-right: map.get($tag-icon-span-gap, 'default') - $border-width-base;
  }

  @each $size in (large, default, small) {
    @include m($size) {
      padding: 0 map.get($tag-padding, $size) - $tag-border-width;
      height: map.get($tag-height, $size);

      --sg-icon-size: #{map.get($tag-icon-size, $size)};

      .#{$namespace}-tag__close {
        margin-left: map.get($tag-icon-span-gap, $size);
      }

      &.is-closable {
        padding-right: map.get($tag-icon-span-gap, $size) - $border-width-base;
      }
    }
  }

  @include m(small) {
    .#{$namespace}-icon-close {
      transform: scale(0.8);
    }
  }

  @include when(checkable) {
    &:not(.is-checked) {
      background: none;
      border-color: transparent;
      color: #000;
        .sg-tag__close {
          color: #fff;
          background-color: #000;
        }
    }
  }
}
