@use 'sass:map';

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

$button-icon-span-gap: () !default;
$button-icon-span-gap: map.merge(
  (
    'large': 8px,
    'default': 6px,
    'small': 4px,
  ),
  $button-icon-span-gap
);

$--button-primary-font-color: #ffffff;

@include b(button) {
  @include set-component-css-var('button', $button);
}

@include b(button) {
  display: inline-flex;
  justify-content: center;
  align-items: center;

  line-height: 1;
  // min-height will expand when in flex
  height: map.get($input-height, 'default');
  white-space: nowrap;
  cursor: pointer;
  color: getCssVar('button', 'text-color');
  text-align: center;
  box-sizing: border-box;
  outline: none;
  transition: 0.1s;
  font-weight: getCssVar('button', 'font-weight');
  user-select: none;
  vertical-align: middle;
  -webkit-appearance: none;
  background-color: getCssVar('button', 'bg-color');
  border: getCssVar('border');
  border-color: getCssVar('button', 'border-color');

  &:hover {
    color: getCssVar('button', 'hover', 'text-color');
    border-color: getCssVar('button', 'hover', 'border-color');
    background-color: getCssVar('button', 'hover', 'bg-color');
    outline: none;
  }

  &:active {
    color: getCssVar('button', 'active', 'text-color');
    border-color: getCssVar('button', 'active', 'border-color');
    background-color: getCssVar('button', 'active', 'bg-color');
    outline: none;
  }

  &:focus-visible {
    outline: 2px solid getCssVar('button', 'outline-color');
    outline-offset: 1px;
    transition:
      outline-offset 0s,
      outline 0s;
  }

  > span {
    display: inline-flex;
    align-items: center;
  }

  & + & {
    margin-left: 12px;
  }

  & {
    @include button-size(
      map.get($button-padding-vertical, 'default') - $button-border-width,
      map.get($button-padding-horizontal, 'default') - $button-border-width,
      map.get($button-font-size, 'default'),
      map.get($button-border-radius, 'default')
    );
  }

  &::-moz-focus-inner {
    border: 0;
  }

  & [class*='#{$namespace}-icon'] {
    & + span {
      margin-left: map.get($button-icon-span-gap, 'default');
    }
    svg {
      vertical-align: bottom;
    }
  }

  @include when(plain) {
    @include css-var-from-global(
      ('button', 'hover', 'text-color'),
      ('color', 'primary')
    );
    @include css-var-from-global(
      ('button', 'hover', 'bg-color'),
      ('fill-color', 'blank')
    );
    @include css-var-from-global(
      ('button', 'hover', 'border-color'),
      ('color', 'primary')
    );
  }

  @include when(active) {
    color: getCssVar('button', 'active', 'text-color');
    border-color: getCssVar('button', 'active', 'border-color');
    background-color: getCssVar('button', 'active', 'bg-color');
    outline: none;
  }

  @include when(disabled) {
    &,
    &:hover {
      color: getCssVar('button', 'disabled', 'text-color');
      cursor: not-allowed;
      background-image: none;
      background-color: getCssVar('button', 'disabled', 'bg-color');
      border-color: getCssVar('button', 'disabled', 'border-color');
    }
  }

  @include when(loading) {
    position: relative;
    pointer-events: none;

    &:before {
      // mask the button
      z-index: 1;
      pointer-events: none;
      content: '';
      position: absolute;
      left: -1px;
      top: -1px;
      right: -1px;
      bottom: -1px;
      border-radius: inherit;
      background-color: getCssVar('mask-color', 'extra-light');
    }
  }
  @include when(round) {
    border-radius: getCssVar('border-radius', 'round');
  }

  @include when(dashed) {
    border-style: dashed;
    @include css-var-from-global(
      ('button', 'hover', 'text-color'),
      ('color', 'primary')
    );
    @include css-var-from-global(
      ('button', 'hover', 'bg-color'),
      ('fill-color', 'blank')
    );
    @include css-var-from-global(
      ('button', 'hover', 'border-color'),
      ('color', 'primary')
    );
  }

  @include when(circle) {
    width: map.get($input-height, 'default');
    border-radius: 50%;
    padding: map.get($button-padding-vertical, 'default') - $button-border-width;
  }

  @include when(text) {
    color: getCssVar('button', 'text-color');
    border: 0 solid transparent;
    background-color: transparent;

    @include when(disabled) {
      color: getCssVar('button', 'disabled', 'text-color');
      background-color: transparent !important;
    }

    &:not(.is-disabled) {
      &:hover {
        background-color: getCssVar('fill-color', 'light');
      }

      &:focus-visible {
        outline: 2px solid getCssVar('button', 'outline-color');
        outline-offset: 1px;
        transition:
          outline-offset 0s,
          outline 0s;
      }

      &:active {
        background-color: getCssVar('fill-color');
      }

      @include when(has-bg) {
        background-color: getCssVar('fill-color', 'light');

        &:hover {
          background-color: getCssVar('fill-color');
        }

        &:active {
          background-color: getCssVar('fill-color', 'dark');
        }
      }
    }
  }

  @include e(text) {
    @include m(expand) {
      letter-spacing: 0.3em;
      margin-right: -0.3em;
    }
  }

  @include when(link) {
    border-color: transparent;
    color: getCssVar('button', 'text-color');
    background: transparent;
    padding: 2px;
    height: auto;

    &:hover {
      color: getCssVar('button', 'hover', 'link-text-color');
    }

    @include when(disabled) {
      color: getCssVar('button', 'disabled', 'text-color');
      background-color: transparent !important;
      border-color: transparent !important;
    }

    &:not(.is-disabled) {
      &:hover {
        border-color: transparent;
        background-color: transparent;
      }

      &:active {
        color: getCssVar('button', 'active-color');
        border-color: transparent;
        background-color: transparent;
      }
    }
  }

  @include m(text) {
    border-color: transparent;
    background: transparent;
    color: getCssVar('color', 'primary');
    padding-left: 0;
    padding-right: 0;
    @include when(disabled) {
      color: getCssVar('button', 'disabled', 'text-color');
      background-color: transparent !important;
      border-color: transparent !important;
    }

    &:not(.is-disabled) {
      &:hover {
        color: getCssVar('color', 'primary', 'light-3');
        border-color: transparent;
        background-color: transparent;
      }

      &:active {
        color: getCssVar('color', 'primary', 'dark-2');
        border-color: transparent;
        background-color: transparent;
      }
    }
  }

  @include e(link) {
    @include m(expand) {
      letter-spacing: 0.3em;
      margin-right: -0.3em;
    }
  }

  @each $type in (primary, success, warning, danger, info) {
    @include m($type) {
      @include button-variant($type);
    }
  }

  @each $size in (large, small) {
    @include m($size) {
      @include set-css-var-value(
        ('button', 'size'),
        map.get($input-height, $size)
      );

      height: getCssVar('button', 'size');

      & [class*='#{$namespace}-icon'] {
        & + span {
          margin-left: map.get($button-icon-span-gap, $size);
        }
      }

      & {
        @include button-size(
          map.get($button-padding-vertical, $size) - $button-border-width,
          map.get($button-padding-horizontal, $size) - $button-border-width,
          map.get($button-font-size, $size),
          map.get($button-border-radius, $size)
        );
      }

      @include when(circle) {
        width: getCssVar('button', 'size');
        padding: map.get($button-padding-vertical, $size) - $button-border-width;
      }
    }
  }

  @include m(colour-1) {
    $background-color: linear-gradient(
      0deg,
      rgba(6, 14, 131, 1) 0%,
      rgba(12, 25, 180, 1) 100%
    );
    $background-color-hover: linear-gradient(
      0deg,
      rgba(0, 3, 255, 1) 0%,
      rgba(2, 126, 251, 1) 100%
    );
    $background-color-disabled: linear-gradient(
      0deg,
      rgba(0, 3, 255, 0.5) 0%,
      rgba(2, 126, 251, 0.5) 100%
    );
    @include button-variant-custom(
      $--button-primary-font-color,
      $background-color,
      $background-color-hover,
      $background-color-disabled
    );
  }
  @include m(colour-2) {
    $background-color: linear-gradient(
      0deg,
      rgba(96, 9, 240, 1) 0%,
      rgba(129, 5, 240, 1) 100%
    );
    $background-color-hover: linear-gradient(
      0deg,
      rgba(127, 69, 231, 1) 0%,
      rgb(160, 102, 217) 100%
    );
    $background-color-disabled: linear-gradient(
      0deg,
      rgba(127, 69, 231, 0.5) 0%,
      rgba(160, 102, 217, 0.5) 100%
    );
    @include button-variant-custom(
      $--button-primary-font-color,
      $background-color,
      $background-color-hover,
      $background-color-disabled
    );
  }
  @include m(colour-3) {
    $background-color: linear-gradient(
      0deg,
      rgba(0, 172, 238, 1) 0%,
      rgba(2, 126, 251, 1) 100%
    );
    $background-color-hover: linear-gradient(
      0deg,
      rgba(87, 168, 243, 1) 0%,
      rgb(123, 184, 243) 100%
    );
    $background-color-disabled: linear-gradient(
      0deg,
      rgba(87, 168, 243, 0.5) 0%,
      rgba(123, 184, 243, 0.5) 100%
    );
    @include button-variant-custom(
      $--button-primary-font-color,
      $background-color,
      $background-color-hover,
      $background-color-disabled
    );
  }
  @include m(colour-4) {
    $background-color: linear-gradient(0deg, #4dccc6 0%, #96e4df 100%);
    $background-color-hover: linear-gradient(0deg, #03c8a8 0%, #2dc7ad 100%);
    $background-color-disabled: linear-gradient(
      0deg,
      rgba(3, 200, 168, 0.5) 0%,
      rgba(45, 199, 173, 0.5) 100%
    );
    @include button-variant-custom(
      $--button-primary-font-color,
      $background-color,
      $background-color-hover,
      $background-color-disabled
    );
  }
  @include m(colour-5) {
    $background-color: linear-gradient(
      0deg,
      rgb(227, 44, 23) 0%,
      rgba(251, 75, 2, 1) 100%
    );
    $background-color-hover: linear-gradient(
      0deg,
      rgb(219, 69, 53) 0%,
      rgb(213, 99, 87) 100%
    );
    $background-color-disabled: linear-gradient(
      0deg,
      rgba(219, 69, 53, 0.5) 0%,
      rgba(213, 99, 87, 0.5) 100%
    );
    @include button-variant-custom(
      $--button-primary-font-color,
      $background-color,
      $background-color-hover,
      $background-color-disabled
    );
  }
  @include m(colour-6) {
    $background-color: linear-gradient(
      to right,
      #6253e1,
      #852d91,
      #a3a1ff,
      #f24645
    );
    $box-shadow-color: 0 1px 5px 0 rgba(126, 52, 161, 0.75);
    $background-color-hover: linear-gradient(
      to right,
      #6253e1,
      #852d91,
      #a3a1ff,
      #f24645
    );
    $box-shadow-color-hover: 0 1px 5px 0 rgba(126, 52, 161, 0.75);
    $background-color-disabled: linear-gradient(
      to right,
      rgba(98, 83, 225, 0.5),
      rgba(133, 45, 145, 0.5),
      rgba(163, 161, 255, 0.5),
      rgba(242, 70, 69, 0.5)
    );
    @include button-variant-custom-tow(
      $--button-primary-font-color,
      $background-color,
      $box-shadow-color,
      $background-color-hover,
      $box-shadow-color-hover,
      $background-color-disabled
    );
  }
  @include m(colour-7) {
    $background-color: linear-gradient(
      to right,
      #4fbdc7,
      #40e495,
      #30dd8a,
      #2bb673
    );
    $box-shadow-color: 0 1px 5px 0 rgba(49, 196, 190, 0.75);
    $background-color-hover: linear-gradient(
      to right,
      #4fbdc7,
      #40e495,
      #30dd8a,
      #2bb673
    );
    $box-shadow-color-hover: 0 1px 5px 0 rgba(49, 196, 190, 0.75);
    $background-color-disabled: linear-gradient(
      to right,
      rgba(79, 189, 199, 0.5),
      rgba(64, 228, 149, 0.5),
      rgba(48, 221, 138, 0.5),
      rgba(43, 182, 115, 0.5)
    );
    @include button-variant-custom-tow(
      $--button-primary-font-color,
      $background-color,
      $box-shadow-color,
      $background-color-hover,
      $box-shadow-color-hover,
      $background-color-disabled
    );
  }
  @include m(colour-8) {
    $background-color: linear-gradient(
      to right,
      #fc6076,
      #ff9a44,
      #ef9d43,
      #e75516
    );
    $box-shadow-color: 0 1px 5px 0 rgba(252, 104, 110, 0.75);
    $background-color-hover: linear-gradient(
      to right,
      #fc6076,
      #ff9a44,
      #ef9d43,
      #e75516
    );
    $box-shadow-color-hover: 0 1px 5px 0 rgba(252, 104, 110, 0.75);
    $background-color-disabled: linear-gradient(
      to right,
      rgba(252, 96, 118, 0.5),
      rgba(255, 154, 68, 0.5),
      rgba(239, 157, 67, 0.5),
      rgba(231, 85, 22, 0.5)
    );
    @include button-variant-custom-tow(
      $--button-primary-font-color,
      $background-color,
      $box-shadow-color,
      $background-color-hover,
      $box-shadow-color-hover,
      $background-color-disabled
    );
  }
  @include m(colour-9) {
    $background-color: linear-gradient(
      to right,
      #eb3941,
      #f15e64,
      #e14e53,
      #e2373f
    );
    $box-shadow-color: 0 1px 5px rgba(242, 97, 103, 0.4);
    $background-color-hover: linear-gradient(
      to right,
      #eb3941,
      #f15e64,
      #e14e53,
      #e2373f
    );
    $box-shadow-color-hover: 0 1px 5px rgba(242, 97, 103, 0.4);
    $background-color-disabled: linear-gradient(
      to right,
      rgba(235, 57, 65, 0.5),
      rgba(241, 94, 100, 0.5),
      rgba(225, 78, 83, 0.5),
      rgba(226, 55, 63, 0.5)
    );
    @include button-variant-custom-tow(
      $--button-primary-font-color,
      $background-color,
      $box-shadow-color,
      $background-color-hover,
      $box-shadow-color-hover,
      $background-color-disabled
    );
  }
  @include m(colour-10) {
    $background-color: linear-gradient(
      to right,
      #25aae1,
      #4481eb,
      #04befe,
      #3f86ed
    );
    $box-shadow-color: 0 1px 5px 0 rgba(65, 132, 234, 0.75);
    $background-color-hover: linear-gradient(
      to right,
      #25aae1,
      #4481eb,
      #04befe,
      #3f86ed
    );
    $box-shadow-color-hover: 0 1px 5px 0 rgba(65, 132, 234, 0.75);
    $background-color-disabled: linear-gradient(
      to right,
      rgba(37, 170, 225, 0.5),
      rgba(68, 129, 235, 0.5),
      rgba(4, 190, 254, 0.5),
      rgba(63, 134, 237, 0.5)
    );
    @include button-variant-custom-tow(
      $--button-primary-font-color,
      $background-color,
      $box-shadow-color,
      $background-color-hover,
      $box-shadow-color-hover,
      $background-color-disabled
    );
  }
  @include m(colour-11) {
    $color: #409eff;
    $background: #ecf5ff;
    $border-color: #b3d8ff;
    $color-hover: $--button-primary-font-color;
    $background-image-hover: linear-gradient(to right, #25aae1, #3f86ed);
    $box-shadow-hover: 0 1px 5px 0 rgba(65, 132, 234, 0.75);
    $border-color-hover: #3f86ed;
    $color-disabled: #409eff;
    $background-disabled: #ecf5ff;
    $border-color-disabled: #b3d8ff;
    @include button-variant-custom-three(
      $color,
      $background,
      $border-color,
      $color-hover,
      $background-image-hover,
      $box-shadow-hover,
      $border-color-hover,
      $color-disabled,
      $background-disabled,
      $border-color-disabled
    );
  }
  @include m(colour-12) {
    $color: #f56c6c;
    $background: #fef0f0;
    $border-color: #fbc4c4;
    $color-hover: $--button-primary-font-color;
    $background-image-hover: linear-gradient(to right, #eb3941, #e2373f);
    $box-shadow-hover: 0 1px 5px rgba(242, 97, 103, 0.75);
    $border-color-hover: #e2373f;
    $color-disabled: #f56c6c;
    $background-disabled: #fef0f0;
    $border-color-disabled: #fbc4c4;
    @include button-variant-custom-three(
      $color,
      $background,
      $border-color,
      $color-hover,
      $background-image-hover,
      $box-shadow-hover,
      $border-color-hover,
      $color-disabled,
      $background-disabled,
      $border-color-disabled
    );
  }
}
