@use 'sass:map';
@use '../../base/token';
@use '../../abstract';

:root {
  --button-group-sm: 32px;
  --button-group-md: 40px;
  --button-group-lg: 44px;
  --button-group-xl: 48px;
}

$button-group-size: (
  'sm': (
    width: var(--button-group-sm),
    height: var(--button-group-sm),
  ),
  'md': (
    width: var(--button-group-md),
  ),
  'lg': (
    width: var(--button-group-lg),
    height: var(--button-group-lg),
  ),
  'xl': (
    width: var(--button-group-xl),
    height: var(--button-group-xl),
  ),
);

$button-group-padding: (
  'sm': (
    padding: 8px 16px,
  ),
  'md': (
    padding: 10px 16px,
  ),
  'lg': (
    padding: 10px 12px,
  ),
  'xl': (
    padding: 10px 12px,
  ),
);

.cds-button-group {
  $this: &;
  display: inline-flex;
  overflow: hidden;
  color: var(--gray-400);
  font-weight: abstract.font-weight-to-number(token.$font-weights-commerce-sans-2);

  &.has-border {
    border: 1px solid var(--gray-200);
    border-radius: 8px;
    box-shadow: var(--shadow-xs);

    #{$this}__item {
      flex: 1;

      &:hover {
        background-color: var(--gray-50);
      }

      &:focus {
        color: var(--gray-600);
      }

      &:disabled {
        background-color: var(--base-white);
        color: var(--gray-200);
      }

      &.is-active {
        background-color: var(--secondary-gray-blue-50);
        color: var(--gray-600);

        &:disabled {
          color: var(--gray-200);
        }
      }

      svg {
        display: block;
      }
    }
  }

  &.cds-button-group--xs:not(.has-border) {
    #{$this}__item {
      height: auto;
    }
  }

  &__item {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    background-color: var(--base-white);
    font-size: token.$font-size-1;

    &:first-child {
      padding-left: 0;
    }

    &:last-child {
      padding-right: 0;
    }

    &:nth-child(n + 2) {
      border-left: 1px solid var(--gray-200);
    }

    &.is-active {
      color: var(--gray-600);
    }
  }

  @each $size, $info in $button-group-size {
    &--#{$size} &__item {
      box-sizing: border-box;
      width: map.get($info, 'width');
      height: map.get($info, 'height');
    }
  }

  @each $size, $info in $button-group-padding {
    &--#{$size} &__item {
      padding: map.get($info, 'padding');
    }
  }
}
