/**
 * Component: Button
 */
@include comp('button') {
  position: relative;
  display: inline-flex;
  flex-wrap: nowrap;
  justify-content: center;
  align-content: center;
  align-items: center;
  vertical-align: middle;

  text-decoration: none;
  user-select: none;
  font-weight: normal;
  line-height: $button-line-height;
  white-space: nowrap;

  padding: 0;
  margin: 0;

  color: inherit;
  background-color: transparent;
  border: rem($border-base) solid transparent;

  cursor: pointer;

  overflow: hidden;

  // overwrite
  -webkit-appearance: none;
  &:focus {
    outline: none;
  }

  // --------------------------------
  // STYLE shape & size

  @each $size in $type-size {
    $font-size-value: map-get(
      $map: $button-font-size-map,
      $key: $size,
    );
    $size-value: map-get(
      $map: $button-size-map,
      $key: $size,
    );
    @include prop(
      (
        'size': $size,
      )
    ) {
      font-size: rem($font-size-value);
      // line-height: rem($size-value);
    }
  }

  // rect square
  @include prop(
    (
      'shape': 'rect',
    ),
    (
      'shape': 'square',
    )
  ) {
    border-radius: rem($radius-base);
  }

  @include prop(
    (
      'shape': 'circle',
    )
  ) {
    border-radius: 50%;
  }

  @each $size, $size-value in $button-size-map {
    @include prop(
      (
        'size': $size,
        'shape': 'pill',
      )
    ) {
      border-radius: rem($size-value / 2);
    }

    @include prop(
      (
        'size': $size,
      )
    ) {
      height: rem($size-value);
    }

    // rect, pill
    @include prop(
      (
        'size': $size,
        'shape': 'rect',
      ),
      (
        'size': $size,
        'shape': 'pill',
      )
    ) {
      min-width: rem($size-value * $button-min-width-multiple);
      padding: 0 rem($size-value * $button-padding-multiple);

      @include when('wide') {
        flex: 1 0 100%;
        width: 100%;
        max-width: 100%;
      }
    }

    // square, circle
    @include prop(
      (
        'size': $size,
        'shape': 'square',
      ),
      (
        'size': $size,
        'shape': 'circle',
      )
    ) {
      width: rem($size-value);
    }
  }

  // ----------------------------
  // STYLE skin & tone & theme

  // fill

  @include prop(
    (
      'skin': 'fill',
    )
  ) {
    color: $color-white;
  }

  &::before {
    display: block;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;

    opacity: 0;

    transition: opacity ease-in-out $button-transition-duration;
  }
  @include prop(
    (
      'skin': 'plain',
    ),
    (
      'skin': 'flat',
    ),
    (
      'skin': 'silk',
    )
  ) {
    &::before {
      content: ' ';
    }
  }

  @each $tone in $type-tone {
    @include prop(
      (
        'skin': 'fill',
        'tone': $tone,
      )
    ) {
      background-color: tone($tone);

      &:hover {
        background-color: tone($tone, 'lighten');
      }
      &:active {
        background-color: tone($tone, 'darken');
      }
    }
  }

  // flat & plain & silk

  @each $theme in $type-theme {
    @include prop(
      (
        'skin': 'flat',
        'theme': $theme,
      )
    ) {
      color: theme($theme, 'fg', 'emphasize');
    }
    @include prop(
      (
        'skin': 'flat',
        'theme': $theme,
      ),
      (
        'skin': 'plain',
        'theme': $theme,
      )
    ) {
      background-color: theme($theme, 'bg');
    }
  }

  @each $tone in $type-tone {
    @include prop(
      (
        'skin': 'plain',
        'tone': $tone,
      ),
      (
        'skin': 'silk',
        'tone': $tone,
      )
    ) {
      color: tone($tone);
    }
    @include prop(
      (
        'skin': 'plain',
        'tone': $tone,
      ),
      (
        'skin': 'silk',
        'tone': $tone,
      )
    ) {
      border-color: tone($tone);
    }

    @include prop(
      (
        'tone': $tone,
      )
    ) {
      &::before {
        background-color: tone($tone);
      }
      &:hover {
        &::before {
          opacity: $button-translucent-hover;
        }
      }
      &:active {
        &::before {
          opacity: $button-translucent-active;
        }
      }
    }
    @include when('disabled') {
      &,
      &:hover,
      &:active {
        &::before {
          opacity: 0;
        }
      }
    }
  }

  // ----------------------------
  // STYLE disabled

  @include when('disabled') {
    cursor: not-allowed;
  }

  @each $theme in $type-theme {
    @include prop(
      (
        'theme': $theme,
        'skin': 'fill',
      ),
      (
        'theme': $theme,
        'skin': 'flat',
      ),
      (
        'theme': $theme,
        'skin': 'plain',
      ),
      (
        'theme': $theme,
        'skin': 'silk',
      )
    ) {
      @include when('disabled') {
        &,
        &:hover,
        &:active {
          color: theme($theme, 'fg', 'disabled');
        }
      }
    }

    @include prop(
      (
        'theme': $theme,
        'skin': 'fill',
      )
    ) {
      @include when('disabled') {
        &,
        &:hover,
        &:active {
          background-color: theme($theme, 'bg', 'disabled');
        }
      }
    }

    @include prop(
      (
        'theme': $theme,
        'skin': 'flat',
      ),
      (
        'theme': $theme,
        'skin': 'plain',
      )
    ) {
      @include when('disabled') {
        &,
        &:hover,
        &:active {
          background-color: theme($theme, 'bg', 'normal');
        }
      }
    }

    @include prop(
      (
        'theme': $theme,
        'skin': 'plain',
      ),
      (
        'theme': $theme,
        'skin': 'silk',
      )
    ) {
      @include when('disabled') {
        &,
        &:hover,
        &:active {
          border-color: theme($theme, 'bd', 'disabled');
        }
      }
    }
  }

  // ----------------------------
  // STYLE loading

  @include when('loading') {
    cursor: wait;
  }

  // ----------------------------
  // transition

  transition: ease-in-out $button-transition-duration;
  transition-property: color, background-color, border-color, box-shadow;

  // ----------------------------
  // slots

  @include elem-adjacent('loading', 'content') {
    margin-left: $button-gap-icon-text * 1em;
  }
  @include elem-adjacent('left', 'content') {
    margin-left: $button-gap-icon-text * 1em;
  }
  @include elem-adjacent('content', 'right') {
    margin-left: $button-gap-icon-text * 1em;
  }

  @include elem('loading', 'left', 'right') {
    width: 1em;
  }

  @include elem('loading-indicator') {
    display: inline-block;
    width: 1em;
    height: 1em;
    line-height: 1;
    vertical-align: middle;
    border-radius: 50%;
    border: rem(2) solid transparent;
    border-right-color: currentColor;
    border-bottom-color: currentColor;

    @keyframes vd-button-loading-indicator-spin {
      from {
        transform: rotate(0);
      }
      to {
        transform: rotate(360deg);
      }
    }
    animation: vd-button-loading-indicator-spin 750ms linear infinite;
  }
}

/**
 * Component: ButtonGroup
 */
@include comp('button-group') {
  position: relative;
  flex: none;

  // --------------------------------
  // STYLE layout

  @include elem('wrapper') {
    position: relative;
    display: flex;
    flex-direction: row;
    align-content: stretch;
    align-items: stretch;
    justify-content: flex-start;
  }

  @include when('gap') {
    flex-wrap: wrap;
  }
  @include when('compact') {
    flex-wrap: nowrap;
  }

  @each $direction in $type-flex-direction {
    @include prop(
      (
        'direction': $direction,
      )
    ) {
      @include elem('wrapper') {
        flex-direction: $direction;
      }
    }
  }

  @each $direction in $type-flex-direction {
    // gap
    @each $size, $size-value in $button-size-map {
      @include prop(
        (
          'size': $size,
          'direction': $direction,
        )
      ) {
        @include when('gap') {
          $gap-value-half: $size-value / 2 * $button-group-gap-multiple;

          padding: rem($gap-value-half);

          @include elem('wrapper') {
            margin: rem(-$gap-value-half * 3);
            padding: rem($gap-value-half);
          }

          @include elem('item') {
            margin: rem($gap-value-half);
          }
        }
      }
    }

    // compact
    @include prop(
      (
        'direction': $direction,
      )
    ) {
      @include when('compact') {
        @include elem('item') {
          @if str-index($string: $direction, $substring: 'column') == 1 {
            border-left: 0;
            border-right: 0;
          } @else {
            border-top: 0;
            border-bottom: 0;
          }

          &:not(:first-child) {
            @if str-index($string: $direction, $substring: 'column') == 1 {
              margin-top: rem(-$border-base);
            } @else {
              margin-left: rem(-$border-base);
            }
          }
        }
      }
    }
  }

  // --------------------------------
  // STYLE shape & size

  @include when('compact') {
    overflow: hidden;
    border: rem($border-base) solid transparent;

    @include elem('item') {
      border-radius: 0;
    }
  }

  @each $shape in ('rect', 'square') {
    @include when('compact') {
      border-radius: rem($radius-base);
    }
  }

  @each $size, $size-value in $button-size-map {
    @each $shape in ('pill', 'circle') {
      @include prop(
        (
          'shape': $shape,
          'size': $size,
        )
      ) {
        @include when('compact') {
          border-radius: rem($size-value / 2);
        }
      }
    }

    @each $direction in $type-flex-direction {
      @include prop(
        (
          'size': $size,
          'direction': $direction,
        )
      ) {
        @include when('compact') {
          $gap-value-half: $size-value / 2 * $button-group-gap-multiple;
          @if str-index($string: $direction, $substring: 'column') {
            padding: rem($gap-value-half - $border-base) 0;
          } @else {
            padding: 0 rem($gap-value-half - $border-base);
          }
        }
      }

      @each $skin in ('plain', 'silk') {
        @each $shape in ('rect', 'square') {
          @include prop(
            (
              'skin': $skin,
              'shape': $shape,
              'size': $size,
              'direction': $direction,
            )
          ) {
            @include when('compact') {
              padding: 0;
              @if str-index($string: $direction, $substring: 'column') {
                border-width: 0 rem($border-base);
              } @else {
                border-width: rem($border-base) 0;
              }
            }
          }
        }

        @each $shape in ('pill', 'circle') {
          @include prop(
            (
              'skin': $skin,
              'shape': $shape,
              'size': $size,
              'direction': $direction,
            )
          ) {
            @include when('compact') {
              @if str-index($string: $direction, $substring: 'column') {
                padding: rem($size-value / 2 - $border-base) 0;
              } @else {
                padding: 0 rem($size-value / 2 - $border-base);
              }
            }
          }
        }
      }
    }
  }

  // ----------------------------
  // STYLE skin & tone & theme

  @each $tone in $type-tone {
    @include prop(
      (
        'tone': $tone,
        'skin': 'fill',
      )
    ) {
      @include when('compact') {
        background-color: tone($tone);
      }
    }
  }

  @each $theme in $type-theme {
    @include prop(
      (
        'skin': 'flat',
        'theme': $theme,
      ),
      (
        'skin': 'plain',
        'theme': $theme,
      )
    ) {
      @include when('compact') {
        background-color: theme($theme, 'bg');
      }
    }
  }

  @each $tone in $type-tone {
    @include prop(
      (
        'skin': 'plain',
        'tone': $tone,
      ),
      (
        'skin': 'silk',
        'tone': $tone,
      )
    ) {
      border-color: tone($tone);
    }
  }

  // dropdown

  @each $direction in $type-flex-direction {
    @each $shape in ('rect', 'pill') {
      @include prop(
        (
          'direction': $direction,
          'shape': $shape,
        )
      ) {
        @if str-index($string: $direction, $substring: 'column') == 1 {
          @include when('dropdown') {
            @include elem('item') {
              justify-content: flex-start;
            }
          }
        }
      }
    }
  }

  @include when('dropdown', 'compact') {
    box-shadow: raise-shadow(2);
  }
  @include prop(
    (
      'skin': 'silk',
    )
  ) {
    @include when('dropdown', 'compact') {
      box-shadow: none;
    }
  }
}
