//
// Copyright IBM Corp. 2016, 2025
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//

@use '../button';
@use '../../config' as *;
@use '../../motion' as *;
@use '../../spacing' as *;
@use '../../theme' as *;
@use '../../type' as *;
@use '../../utilities/convert';
@use '../../utilities/focus-outline' as *;
@use '../../utilities/high-contrast-mode' as *;
@use '../../utilities/layout';
@use './tokens' as *;
@use '../button/tokens' as *;

@mixin content-switcher {
  .#{$prefix}--content-switcher {
    @include layout.use('size', $default: 'md', $min: 'sm', $max: 'lg');
    @include layout.use('density', $default: 'normal');

    display: flex;
    justify-content: space-evenly;
    border-radius: convert.to-rem(4px);
    block-size: layout.size('height');
    inline-size: 100%;
    outline: convert.to-rem(1px) solid $border-inverse;
    outline-offset: convert.to-rem(-1px);
  }

  .#{$prefix}--content-switcher:has(
      .#{$prefix}--content-switcher-btn:disabled
    ) {
    outline-color: $border-disabled;
  }

  .#{$prefix}--content-switcher-btn {
    @include reset;
    @include font-family('sans');
    @include focus-outline('reset');
    @include type-style('body-compact-01');

    position: relative;
    display: inline-flex;
    overflow: hidden;
    border: none;
    margin: 0;
    background-color: transparent;
    border-block-end: convert.to-rem(1px) solid $border-inverse;
    border-block-start: convert.to-rem(1px) solid $border-inverse;
    color: $text-secondary;
    text-align: start;
    text-decoration: none;
    white-space: nowrap;

    &::after {
      position: absolute;
      display: block;
      border-radius: convert.to-rem(4px);
      background-color: $layer-selected-inverse;
      block-size: 100%;
      content: '';
      inline-size: 100%;
      inset-block-start: 0;
      inset-inline-start: 0;
      transform: scaleY(0);
      transform-origin: bottom;
    }

    &:disabled::after {
      display: none;
    }

    &:focus {
      border: 0;
    }

    &:focus::after {
      border-radius: convert.to-rem(4px);
      box-shadow:
        inset 0 0 0 2px $focus,
        inset 0 0 0 3px $focus-inset;
    }

    &:hover {
      color: $text-primary;
      cursor: pointer;
    }

    &:hover,
    &:active {
      z-index: 3;
      background-color: $layer-hover;
      color: $text-primary;
    }

    &:disabled {
      border-color: $border-disabled;
      background-color: transparent;
      color: $text-disabled;

      &:hover {
        cursor: not-allowed;
      }
    }
  }

  .#{$prefix}--content-switcher-btn:not(
      .#{$prefix}--content-switcher--selected
    ) {
    &:focus::before {
      display: block;
      border-radius: convert.to-rem(4px);
      block-size: 100%;
      box-shadow: inset 0 0 0 convert.to-rem(2px) $focus;
      content: '';
      inline-size: 100%;
    }

    &:focus::after {
      background-color: $focus-inset;
      transform: scaleY(1);
    }

    &:focus:hover::after {
      border-radius: 0;
      background-color: $layer-hover;
      border-block-end: convert.to-rem(1px) solid $border-inverse;
      border-block-start: convert.to-rem(1px) solid $border-inverse;
      box-shadow: none;
    }
  }

  .#{$prefix}--content-switcher:not(.#{$prefix}--content-switcher--icon-only)
    .#{$prefix}--content-switcher-btn {
    align-items: center;
    padding: $spacing-03 layout.density('padding-inline');
    inline-size: 100%;
  }

  .#{$prefix}--content-switcher:not(.#{$prefix}--content-switcher--icon-only)
    .#{$prefix}--content-switcher-btn:first-child {
    border-end-start-radius: convert.to-rem(4px);
    border-start-start-radius: convert.to-rem(4px);
    box-shadow: inset convert.to-rem(1px) 0 0 0 $border-inverse;

    &:disabled {
      box-shadow: none;
    }
  }

  .#{$prefix}--content-switcher:not(.#{$prefix}--content-switcher--icon-only)
    .#{$prefix}--content-switcher-btn:last-child {
    border-end-end-radius: convert.to-rem(4px);
    border-start-end-radius: convert.to-rem(4px);
    box-shadow: inset convert.to-rem(-1px) 0 0 0 $border-inverse;

    &:disabled {
      box-shadow: none;
    }
  }

  .#{$prefix}--content-switcher
    .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected,
  .#{$prefix}--content-switcher
    .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected:first-child,
  .#{$prefix}--content-switcher
    .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected:last-child {
    border: 0;

    &:disabled::before {
      display: none;
    }
  }

  .#{$prefix}--content-switcher-btn::before {
    position: absolute;
    z-index: 2;
    display: block;
    background-color: $border-subtle;
    block-size: convert.to-rem(16px);
    content: '';
    inline-size: convert.to-rem(1px);
    inset-inline-start: 0;
  }

  .#{$prefix}--content-switcher:not(.#{$prefix}--content-switcher--icon-only)
    .#{$prefix}--content-switcher-btn:first-of-type::before {
    background-color: transparent;
  }

  .#{$prefix}--content-switcher-btn:focus::before,
  .#{$prefix}--content-switcher-btn:focus
    + .#{$prefix}--content-switcher-btn::before,
  .#{$prefix}--content-switcher-btn:hover::before,
  .#{$prefix}--content-switcher-btn:hover
    + .#{$prefix}--content-switcher-btn::before,
  .#{$prefix}--content-switcher--selected::before,
  .#{$prefix}--content-switcher--selected
    + .#{$prefix}--content-switcher-btn::before {
    background-color: transparent;
  }

  .#{$prefix}--content-switcher-btn:disabled::before,
  .#{$prefix}--content-switcher-btn:disabled:hover
    + .#{$prefix}--content-switcher-btn:disabled::before {
    background-color: $border-disabled;
  }

  .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected:disabled
    + .#{$prefix}--content-switcher-btn::before,
  .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected:disabled:hover
    + .#{$prefix}--content-switcher-btn::before {
    background-color: transparent;
  }

  .#{$prefix}--content-switcher__icon {
    fill: $icon-secondary;
  }

  .#{$prefix}--content-switcher__icon + span {
    margin-inline-start: $spacing-03;
  }

  .#{$prefix}--content-switcher__label {
    z-index: 1;
    overflow: hidden;
    max-inline-size: 100%;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .#{$prefix}--content-switcher-btn:hover .#{$prefix}--content-switcher__icon,
  .#{$prefix}--content-switcher-btn:focus .#{$prefix}--content-switcher__icon {
    fill: $icon-primary;
  }

  .#{$prefix}--content-switcher-btn:focus:has(
      + .#{$prefix}--content-switcher-btn:not(
          .#{$prefix}--content-switcher--selected
        ):hover
    ),
  .#{$prefix}--content-switcher-btn:not(
      .#{$prefix}--content-switcher--selected
    ):hover
    + .#{$prefix}--content-switcher-btn:focus,
  .#{$prefix}--content-switcher-btn:hover
    + .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected,
  .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected:has(
      + .#{$prefix}--content-switcher-btn:hover
    ) {
    &::before {
      z-index: 0;
      border-radius: 0;
      block-size: 100%;
      border-block-end: convert.to-rem(1px) solid $border-inverse;
      border-block-start: convert.to-rem(1px) solid $border-inverse;
      box-shadow: inset 0 0 0 convert.to-rem(1px) $layer-hover;
      content: '';
      inline-size: 100%;
    }

    &:not(.#{$prefix}--content-switcher--selected)::after {
      background-color: $focus-inset;
      transform: scaleY(1);
    }
  }

  .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected {
    z-index: 3;
    color: $text-inverse;

    &:hover {
      background-color: transparent;
    }

    &:disabled {
      border-radius: convert.to-rem(4px);
      background-color: $button-disabled;
      color: $text-disabled;
    }

    &::after {
      transform: scaleY(1);
    }
  }

  .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected
    .#{$prefix}--content-switcher__icon {
    fill: $icon-inverse;
  }

  // Windows HCM fix
  .#{$prefix}--content-switcher-btn:focus {
    @include high-contrast-mode('focus');
  }

  // Icon Only overrides
  .#{$prefix}--content-switcher--icon-only {
    justify-content: flex-start;
    inline-size: fit-content;
  }

  .#{$prefix}--content-switcher--icon-only .#{$prefix}--content-switcher-btn {
    align-items: center;
    padding: 0;
    transition: none;

    &:focus {
      box-shadow: none;
    }
  }

  .#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-popover__wrapper:first-child
    .#{$prefix}--content-switcher-btn:first-of-type::before {
    background-color: transparent;
  }

  .#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-popover__wrapper:first-child
    .#{$prefix}--content-switcher-btn {
    border-end-start-radius: convert.to-rem(4px);
    border-start-start-radius: convert.to-rem(4px);
    box-shadow: inset convert.to-rem(1px) 0 0 0 $border-inverse;
  }

  .#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-popover__wrapper:last-child
    .#{$prefix}--content-switcher-btn {
    border-end-end-radius: convert.to-rem(4px);
    border-start-end-radius: convert.to-rem(4px);
    box-shadow: inset convert.to-rem(-1px) 0 0 0 $border-inverse;
  }

  .#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-popover__wrapper:first-child
    .#{$prefix}--content-switcher-btn[disabled],
  .#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-popover__wrapper:last-child
    .#{$prefix}--content-switcher-btn[disabled] {
    box-shadow: none;
  }

  .#{$prefix}--content-switcher--lg .#{$prefix}--content-switcher-btn {
    // accounts for the larger icon sizes to keep the switcher at 48px
    padding-inline: convert.to-rem(14px) convert.to-rem(14px);
  }

  .#{$prefix}--content-switcher--lg .#{$prefix}--content-switcher-btn svg {
    block-size: 20px;
    inline-size: 20px;
  }

  .#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-btn
    svg {
    z-index: 1;
    fill: $icon-primary;
  }

  .#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected
    svg {
    z-index: 1;
    fill: $icon-inverse;
  }

  .#{$prefix}--content-switcher--icon-only.#{$prefix}--content-switcher--sm
    .#{$prefix}--btn--sm {
    block-size: 2rem;
  }

  // Subtle border overrides

  .#{$prefix}--content-switcher-btn:focus::before,
  .#{$prefix}--content-switcher-popover__wrapper:focus-within
    + .#{$prefix}--content-switcher-popover__wrapper
    .#{$prefix}--content-switcher-btn::before,
  .#{$prefix}--content-switcher-popover__wrapper:not(
      .#{$prefix}--content-switcher-popover--disabled
    ):hover
    + .#{$prefix}--content-switcher-popover__wrapper
    .#{$prefix}--content-switcher-btn:not(
      .#{$prefix}--content-switcher--selected
    ):not(:focus)::before,
  .#{$prefix}--content-switcher--selected::before,
  .#{$prefix}--content-switcher-popover--selected
    + .#{$prefix}--content-switcher-popover__wrapper
    .#{$prefix}--content-switcher-btn::before {
    background-color: transparent;
  }

  .#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-btn[disabled]
    svg {
    fill: $icon-disabled;
  }

  .#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-btn[disabled]:not(
      .#{$prefix}--content-switcher--selected
    ):hover,
  .#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-popover--selected
    + .#{$prefix}--content-switcher-popover--disabled
    .#{$prefix}--content-switcher-btn[disabled]:hover::before {
    background-color: transparent;
  }

  .#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-btn[disabled]:hover::before {
    background-color: $border-subtle;
  }

  .#{$prefix}--content-switcher-popover__wrapper:has(
      + .#{$prefix}--content-switcher-popover__wrapper
        .#{$prefix}--content-switcher-btn:not(
          .#{$prefix}--content-switcher--selected
        ):hover
    )
    .#{$prefix}--content-switcher-btn:focus,
  .#{$prefix}--content-switcher-popover__wrapper:not(
      .#{$prefix}--content-switcher-popover--selected
    ):hover
    + .#{$prefix}--content-switcher-popover__wrapper
    .#{$prefix}--content-switcher-btn:focus,
  .#{$prefix}--content-switcher-popover__wrapper:hover
    + .#{$prefix}--content-switcher-popover__wrapper
    .#{$prefix}--content-switcher--selected,
  .#{$prefix}--content-switcher-popover--selected:has(
      + .#{$prefix}--content-switcher-popover__wrapper
        .#{$prefix}--content-switcher-btn:hover
    )
    .#{$prefix}--content-switcher-btn {
    &::before {
      z-index: 0;
      border-radius: 0;
      block-size: 100%;
      border-block-end: convert.to-rem(1px) solid $border-inverse;
      border-block-start: convert.to-rem(1px) solid $border-inverse;
      box-shadow: inset 0 0 0 convert.to-rem(1px) $layer-hover;
      content: '';
      inline-size: 100%;
    }

    &:not(.#{$prefix}--content-switcher--selected)::after {
      background-color: $focus-inset;
      transform: scaleY(1);
    }
  }

  // Low contrast selected state styling
  .#{$prefix}--content-switcher--low-contrast {
    background-color: $content-switcher-background;
    outline-color: $border-strong;
  }

  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn {
    border-color: $border-strong;

    &:hover,
    &:active {
      background-color: $content-switcher-background-hover;
    }

    &::before {
      background-color: $border-strong;
    }

    &::after {
      background-color: $content-switcher-selected;
    }

    &:disabled {
      border-color: $border-disabled;
      background-color: transparent;
      color: $text-disabled;

      &:hover {
        cursor: not-allowed;
      }
    }
  }

  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn:not(
      .#{$prefix}--content-switcher--selected
    ) {
    &:focus::before {
      background: transparent;
    }

    &:focus::after {
      background-color: $content-switcher-background;
      box-shadow: inset 0 0 0 convert.to-rem(2px) $focus;
    }

    &:focus:hover::after {
      border-color: $border-strong;
      background-color: $content-switcher-background-hover;
      box-shadow: none;
    }
  }

  .#{$prefix}--content-switcher--low-contrast:not(
      .#{$prefix}--content-switcher--icon-only
    )
    .#{$prefix}--content-switcher-btn:first-child {
    box-shadow: inset convert.to-rem(1px) 0 0 0 $border-strong;
  }

  .#{$prefix}--content-switcher--low-contrast:not(
      .#{$prefix}--content-switcher--icon-only
    )
    .#{$prefix}--content-switcher-btn:last-child {
    box-shadow: inset convert.to-rem(-1px) 0 0 0 $border-strong;
  }

  .#{$prefix}--content-switcher--low-contrast {
    .#{$prefix}--content-switcher-btn:focus:has(
        + .#{$prefix}--content-switcher-btn:not(
            .#{$prefix}--content-switcher--selected
          ):hover
      ),
    .#{$prefix}--content-switcher-btn:not(
        .#{$prefix}--content-switcher--selected
      ):hover
      + .#{$prefix}--content-switcher-btn:focus,
    .#{$prefix}--content-switcher-btn:hover
      + .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected,
    .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected:has(
        + .#{$prefix}--content-switcher-btn:hover
      ) {
      &::before {
        border-color: $border-strong;
        box-shadow: inset 0 0 0 convert.to-rem(1px)
          $content-switcher-background-hover;
      }

      &:not(.#{$prefix}--content-switcher--selected)::after {
        background-color: $content-switcher-background;
      }
    }
  }

  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected {
    @include type-style('heading-compact-01');

    color: $text-primary;

    &:hover {
      background-color: transparent;
    }

    &::after {
      border-radius: convert.to-rem(4px);
      background-color: $content-switcher-selected;
      box-shadow: inset 0 0 0 convert.to-rem(1px) $border-inverse;
    }

    &:focus::after {
      box-shadow: inset 0 0 0 convert.to-rem(2px) $focus;
    }

    &:disabled {
      border: convert.to-rem(1px) solid $border-disabled;
      border-radius: convert.to-rem(4px);
      background-color: $content-switcher-selected;
      color: $text-disabled;
    }

    &:disabled::before {
      background-color: transparent;
    }
  }

  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn:focus::before,
  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn:focus
    + .#{$prefix}--content-switcher-btn::before,
  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn:hover::before,
  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn:hover
    + .#{$prefix}--content-switcher-btn::before,
  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher--selected::before,
  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher--selected
    + .#{$prefix}--content-switcher-btn::before {
    background-color: transparent;
  }

  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn:disabled::before,
  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn:disabled:hover
    + .#{$prefix}--content-switcher-btn:disabled::before {
    background-color: $border-disabled;
  }

  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected:disabled
    + .#{$prefix}--content-switcher-btn::before,
  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected:disabled:hover
    + .#{$prefix}--content-switcher-btn::before,
  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn:disabled:hover
    + .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected::before {
    background-color: transparent;
  }

  // low contrast icons

  .#{$prefix}--content-switcher--low-contrast.#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-popover__wrapper:first-child
    .#{$prefix}--content-switcher-btn:not([disabled]) {
    box-shadow: inset convert.to-rem(1px) 0 0 0 $border-strong;
  }

  .#{$prefix}--content-switcher--low-contrast.#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-popover__wrapper:last-child
    .#{$prefix}--content-switcher-btn:not([disabled]) {
    box-shadow: inset convert.to-rem(-1px) 0 0 0 $border-strong;
  }

  .#{$prefix}--content-switcher--low-contrast.#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-btn
    svg {
    fill: $icon-secondary;
  }

  .#{$prefix}--content-switcher--low-contrast.#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-btn:hover
    svg {
    fill: $icon-primary;
  }

  .#{$prefix}--content-switcher--low-contrast.#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-btn[disabled]
    svg {
    fill: $icon-disabled;
  }

  .#{$prefix}--content-switcher--low-contrast.#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected
    svg {
    fill: $icon-primary;
  }

  .#{$prefix}--content-switcher--low-contrast.#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected[disabled]
    svg {
    fill: $icon-disabled;
  }
  .#{$prefix}--content-switcher--low-contrast.#{$prefix}--content-switcher--icon-only
    .#{$prefix}--content-switcher-btn.#{$prefix}--content-switcher--selected:disabled {
    border-color: $border-disabled;
  }

  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-btn:not(
      .#{$prefix}--content-switcher__selected-hovered
    ):focus::before,
  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-popover__wrapper:focus-within
    + .#{$prefix}--content-switcher-popover__wrapper
    .#{$prefix}--content-switcher-btn::before,
  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-popover__wrapper:not(
      .#{$prefix}--content-switcher-popover--disabled
    ):hover
    + .#{$prefix}--content-switcher-popover__wrapper
    .#{$prefix}--content-switcher-btn:not(
      .#{$prefix}--content-switcher__selected-hovered
    )::before,
  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher--selected:not(
      .#{$prefix}--content-switcher__selected-hovered
    )::before,
  .#{$prefix}--content-switcher--low-contrast
    .#{$prefix}--content-switcher-popover--selected
    + .#{$prefix}--content-switcher-popover__wrapper
    .#{$prefix}--content-switcher-btn::before {
    background-color: transparent;
  }

  .#{$prefix}--content-switcher--low-contrast {
    .#{$prefix}--content-switcher-popover__wrapper:has(
        + .#{$prefix}--content-switcher-popover__wrapper
          .#{$prefix}--content-switcher-btn:not(
            .#{$prefix}--content-switcher--selected
          ):hover
      )
      .#{$prefix}--content-switcher-btn:focus,
    .#{$prefix}--content-switcher-popover__wrapper:not(
        .#{$prefix}--content-switcher-popover--selected
      ):hover
      + .#{$prefix}--content-switcher-popover__wrapper
      .#{$prefix}--content-switcher-btn:focus,
    .#{$prefix}--content-switcher-popover__wrapper:hover
      + .#{$prefix}--content-switcher-popover__wrapper
      .#{$prefix}--content-switcher--selected,
    .#{$prefix}--content-switcher-popover--selected:has(
        + .#{$prefix}--content-switcher-popover__wrapper
          .#{$prefix}--content-switcher-btn:hover
      )
      .#{$prefix}--content-switcher-btn {
      &::before {
        border-color: $border-strong;
        box-shadow: inset 0 0 0 convert.to-rem(1px)
          $content-switcher-background-hover;
      }

      &:not(.#{$prefix}--content-switcher--selected)::after {
        background-color: $content-switcher-background;
      }
    }
  }
}
