@use '../variables/color';
@use '../variables/commons';
@use '../variables/type';
@use '../variables/spacing';
@use '../variables/animation';
@use '../mixins/color' as color-mx;
@use '../mixins/forms' as forms-mx;
@use '../mixins/icons' as icons-mx;
@use '../mixins/utilities' as utility-mx;
@use '../tokens/components' as components;
@use '../functions/tokens' as tokens;

tokens.$default-map: components.$post-switch;

$switch-handle-icon: url("data:image/svg+xml,<svg viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'><path d='M7.51712 12.1332L4.30712 8.69325C4.02712 8.39325 4.03712 7.91325 4.34712 7.63325C4.64712 7.35325 5.12712 7.36325 5.40712 7.67325L7.31712 9.72325L10.7171 4.57325C10.9471 4.22325 11.4071 4.13325 11.7571 4.36325C12.1071 4.59325 12.1971 5.05325 11.9671 5.40325L7.51712 12.1332Z'/></svg>");

.form-switch {
  @include forms-mx.form-check-base();

  .form-check-input {
    appearance: none;
    cursor: pointer;
    position: relative;
    display: flex;
    align-items: center;
    height: tokens.get('switch-element-height');
    width: tokens.get('switch-element-width');
    margin-block: tokens.get('switch-icon-padding-container');
    border: tokens.get('switch-border-width') tokens.get('switch-border-style-default')
      tokens.get('switch-element-enabled-stroke');
    border-radius: tokens.get('switch-border-radius-round');
    background-color: tokens.get('switch-element-enabled-bg');
    padding-inline: calc(
      tokens.get('switch-padding-inline-handle') - tokens.get('switch-border-width')
    );
    transition:
      background-color animation.$transition-base-timing,
      border-color animation.$transition-base-timing;

    &::before,
    &::after {
      position: absolute;
      display: block;
      cursor: pointer;
      content: '';
      width: tokens.get('switch-element-handle');
      height: tokens.get('switch-element-handle');
      background-color: tokens.get('switch-handle-enabled');
      border-radius: tokens.get('switch-border-radius-round');
      transition:
        transform animation.$transition-base-timing,
        background-color animation.$transition-base-timing;
    }

    &::before {
      mask-image: $switch-handle-icon;
      background-color: transparent;
      z-index: 1;
      mask-size: tokens.get('switch-element-handle');
      mask-repeat: no-repeat;
      mask-position: center;
    }

    &::after {
      border: tokens.get('switch-border-width') tokens.get('switch-border-style-default')
        tokens.get('switch-handle-enabled-stroke');
    }

    &:checked {
      background-color: tokens.get('switch-element-selected-bg');
      border-color: tokens.get('switch-element-selected-stroke');

      &::before,
      &::after {
        transform: translateX(
          calc(
            tokens.get('switch-element-width') - tokens.get('switch-element-handle') - 2 *
              tokens.get('switch-padding-inline-handle')
          )
        );
      }

      &::before {
        background-color: tokens.get('switch-icon-success');
      }

      &::after {
        @include icons-mx.remove-icon;
        background-color: tokens.get('switch-handle-selected');
        border-color: tokens.get('switch-handle-selected-stroke');
      }

      + .form-check-label {
        color: tokens.get('switch-text-selected-fg');
      }
    }

    @include utility-mx.high-contrast-mode {
      transition: none;
      border: tokens.get('switch-border-width') tokens.get('switch-border-style-default') CanvasText;

      &:checked {
        background-color: SelectedItem !important;
      }

      &::after {
        max-width: tokens.get('switch-element-handle');
        border-color: Canvas;
        background-color: CanvasText;
      }

      &:checked::after,
      &:checked::before {
        transform: translateX(
          calc(
            tokens.get('switch-element-width') - tokens.get('switch-element-handle') - tokens.get(
                'switch-padding-inline-handle'
              ) *
              2 - tokens.get('switch-border-width') * 2
          )
        );
      }
    }

    &[disabled] {
      background-color: tokens.get('switch-element-disabled-bg');
      border: tokens.get('switch-border-width') tokens.get('switch-border-style-disabled')
        tokens.get('switch-element-disabled-stroke');

      @include utility-mx.high-contrast-mode {
        border: tokens.get('switch-border-width') tokens.get('switch-border-style-disabled')
          GrayText;
      }

      + .form-check-label {
        color: tokens.get('switch-text-disabled-fg');
      }

      &::after {
        background-color: tokens.get('switch-handle-disabled');
        border: tokens.get('switch-border-width') tokens.get('switch-border-style-disabled')
          tokens.get('switch-handle-disabled-stroke');

        @include color-mx.on-dark-background {
          border: tokens.get('switch-border-width') tokens.get('switch-border-style-disabled')
            color.$white-alpha-80;
        }

        @include utility-mx.high-contrast-mode {
          border: tokens.get('switch-border-width') tokens.get('switch-border-style-disabled')
            GrayText;
        }
      }

      &:checked::before {
        background-color: tokens.get('switch-icon-disabled');
        mask-image: $switch-handle-icon;
        mask-repeat: no-repeat;
        mask-position: center;
      }

      @include utility-mx.high-contrast-mode {
        &::after {
          border-color: Field;
        }

        &:checked {
          background-color: GrayText !important;
        }
      }
    }

    &.is-invalid {
      border-color: transparent;

      + label {
        color: tokens.get('switch-text-enabled-fg');
      }
    }
  }

  &:has(input:not([disabled]):hover) input {
    background-color: tokens.get('switch-element-hover-bg');
    border-color: tokens.get('switch-element-hover-stroke');

    &::after {
      background-color: tokens.get('switch-handle-hover');
      border-color: tokens.get('switch-handle-hover-stroke');
    }

    &:checked::before {
      background-color: tokens.get('switch-icon-hover');
    }

    @include utility-mx.high-contrast-mode {
      border-color: Highlight;

      &::after {
        background-color: CanvasText;
      }
    }
  }

  .form-check-label {
    color: tokens.get('switch-text-enabled-fg');
    padding-block: tokens.get('switch-padding-block-container-text');

    &.order-first {
      flex: 0 auto;
      padding-inline-end: tokens.get('switch-gap-inline-text');
    }

    &:not(.order-first) {
      padding-inline-start: tokens.get('switch-gap-inline-text');
    }
  }

  > input[type='checkbox'] {
    ~ label {
      color: tokens.get('switch-text-enabled-fg');
    }

    &:not([disabled]):hover ~ label {
      color: tokens.get('switch-text-enabled-fg');
    }

    &[disabled] ~ label {
      color: tokens.get('switch-text-disabled-fg');
    }
  }
}
