@use 'sass:map';

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

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

@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;
  background-color: getCssVar('button', 'bg-color');
  border: getCssVar('border');
  border-color: getCssVar('button', 'border-color');
  color: getCssVar('button', 'text-color');
  -webkit-appearance: none;
  text-align: center;
  box-sizing: border-box;
  outline: none;
  transition: 0.1s;
  font-weight: getCssVar('button', 'font-weight');
  user-select: none;
  vertical-align: middle;

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

  & + & {
    margin-left: 12px;
  }
  // icon默认14px字体大小，小尺寸el-button下应该被覆盖
  & .el-icon {
    font-size: inherit;
  }

  @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')
  );

  &:hover,
  &:focus {
    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;
  }

  &::-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,
    &:focus {
      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(circle) {
    border-radius: 50%;
    padding: map.get($button-padding-vertical, 'default') - $button-border-width;
  }

  @include e(text) {
    @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(text) {
    border-color: transparent;
    color: getCssVar('color', 'primary');
    background: transparent;
    padding: 0;
    min-height: 0;

    &:hover,
    &:focus {
      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;
    }

    &.is-disabled,
    &.is-disabled:hover,
    &.is-disabled:focus {
      color: var(--el-color-primary-light-5);
      border-color: transparent;
    }
  }
}
