@use '../../variables' as *;
@use '../../mixins' as mixins;
@use '../../compat-tokens-mixins' as compatMixins;

@include compatMixins.sky-modern-overrides($encapsulate: false) {
  --sky-override-icon-color-group: var(--sky-color-icon-action);
  --sky-override-icon-switch-border-width-disabled: 0;
  --sky-override-icon-switch-font-size: 18px;
  --sky-override-icon-switch-font-size-group: 20px;
  --sky-override-icon-switch-size: 35px;
  --sky-override-icon-switch-size-group: 40px;
  --sky-override-switch-background-color: var(--modern-color-white);
  --sky-override-switch-background-color-danger: #{lighten(
      $sky-background-color-danger,
      10%
    )};
  --sky-override-switch-background-color-success: #{lighten(
      $sky-background-color-success,
      10%
    )};
  --sky-override-switch-background-color-warning: #{lighten(
      $sky-background-color-warning,
      10%
    )};
  --sky-override-switch-border-color: var(--modern-color-blue-74);
  --sky-override-switch-checked-color: var(--sky-color-icon-action);
  --sky-override-switch-icon-checked-color-no-group: var(
    --sky-color-text-default
  );
  --sky-switch-label-margin-right: #{$sky-margin};
}

@include mixins.sky-component('modern', '.sky-switch', false) {
  cursor: pointer;
  display: inline-flex;
  position: relative;

  &:has(~ .sky-control-help-container) .sky-switch-label {
    margin-right: 0;
  }

  &.sky-switch-disabled {
    cursor: not-allowed;
  }

  &.sky-control-label-required {
    .sky-switch-label {
      margin-right: 0;
    }
  }

  .sky-switch-input {
    border: 0;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
    outline: 0;
    -webkit-appearance: none;

    &.sky-switch-invalid + .sky-switch-control {
      border-width: var(--sky-border-width-input-error);
      border-color: var(--sky-color-border-input-error);
    }

    &:checked:not(:disabled) + .sky-switch-control,
    &[type='checkbox']:indeterminate:not(:disabled) + .sky-switch-control {
      &:not(.sky-switch-control-icon) {
        color: var(
          --sky-override-switch-checked-color,
          var(--sky-color-icon-inverse)
        );
      }

      &.sky-switch-control-icon {
        color: var(
          --sky-override-switch-icon-checked-color-no-group,
          var(--sky-color-icon-inverse)
        );
      }

      background-color: var(--sky-color-background-selected-heavy);
      border-width: var(--sky-border-width-selected-s);
      border-color: var(--sky-color-border-selected);

      &:is(
          .sky-switch-control-success,
          .sky-switch-control-warning,
          .sky-switch-control-danger
        ) {
        color: var(--sky-color-icon-default);
      }

      &.sky-switch-control-success {
        background-color: var(
          --sky-override-switch-background-color-success,
          var(--sky-color-background-selected-success)
        );
      }

      &.sky-switch-control-warning {
        background-color: var(
          --sky-override-switch-background-color-warning,
          var(--sky-color-background-selected-warning)
        );
      }

      &.sky-switch-control-danger {
        background-color: var(
          --sky-override-switch-background-color-danger,
          var(--sky-color-background-selected-danger)
        );
      }
    }

    &:disabled + .sky-switch-control {
      background-color: var(--sky-color-background-input-disabled);
      border-width: var(--sky-border-width-input-disabled);
      border-color: var(--sky-color-border-input-disabled);
      color: var(--sky-color-text-deemphasized);
    }

    &:not(:disabled) {
      &:hover + .sky-switch-control {
        border-width: var(--sky-border-width-input-hover);
        border-color: var(--sky-color-border-input-hover);
      }

      &:active + .sky-switch-control {
        border-width: var(--sky-border-width-input-active);
        border-color: var(--sky-color-border-input-active);
      }

      &:focus-visible + .sky-switch-control {
        border-width: var(--sky-border-width-input-focus);
        border-color: var(--sky-color-border-input-focus);
      }

      &:focus-visible:not(:active) + .sky-switch-control {
        box-shadow: var(--sky-elevation-focus);
        outline: none;
      }

      &:enabled:not(:hover):not(:active):not(:focus-visible) {
        &:checked + .sky-switch-control,
        &[type='checkbox']:indeterminate + .sky-switch-control {
          &.sky-switch-control-success {
            border-color: var(
              --sky-override-switch-border-color,
              var(--sky-color-border-success)
            );
          }

          &.sky-switch-control-warning {
            border-color: var(
              --sky-override-switch-border-color,
              var(--sky-color-border-warning)
            );
          }

          &.sky-switch-control-danger {
            border-color: var(
              --sky-override-switch-border-color,
              var(--sky-color-border-danger)
            );
          }
        }
      }
    }
  }

  .sky-switch-control {
    width: var(--sky-size-switch);
    max-width: var(--sky-size-switch);
    height: var(--sky-size-switch);
    flex: 1 0 var(--sky-size-switch);
    margin: 0 var(--sky-space-gap-label-s) auto auto;
    display: inline-flex;
    position: relative;
    border: var(--sky-border-width-input-base) solid
      var(--sky-color-border-input-base);
    background-color: var(
      --sky-override-switch-background-color,
      var(--sky-color-background-input-base)
    );
    color: var(--sky-color-icon-default);
    text-align: center;
    line-height: 1;
    align-items: center;
    justify-content: center;
    transition: $sky-form-border-and-color-transitions;

    &.sky-switch-control-icon {
      max-width: none;
      width: var(
        --sky-override-icon-switch-size,
        calc(
          calc(var(--sky-font-line_height-body-m) * var(--sky-font-size-body-m)) +
            calc(var(--sky-space-inset-balanced-s) * 2)
        )
      );
      height: var(
        --sky-override-icon-switch-size,
        calc(
          calc(var(--sky-font-line_height-body-m) * var(--sky-font-size-body-m)) +
            calc(var(--sky-space-inset-balanced-s) * 2)
        )
      );
      flex: 1 0
        var(
          --sky-override-icon-switch-size,
          calc(
            calc(
                var(--sky-font-line_height-body-m) * var(--sky-font-size-body-m)
              ) + calc(var(--sky-space-inset-balanced-s) * 2)
          )
        );
      font-size: var(
        --sky-override-icon-switch-font-size,
        var(--sky-size-icon-m)
      );
    }

    &::before {
      content: '';
    }
  }

  .sky-switch-label {
    line-height: var(--sky-size-switch);
    flex: 1 1 auto;
    width: 100%;
    margin-right: var(--sky-switch-label-margin-right, 0);

    // Prevent truncated text from spilling out of bounds.
    // See: https://css-tricks.com/flexbox-truncated-text/
    min-width: 0;
  }
}

@include mixins.sky-component('modern', '.sky-switch-icon-group', false) {
  .sky-switch-control-icon {
    margin-left: 0;
    margin-right: 0;
    border-radius: 0;
  }

  .sky-switch-control {
    background-color: var(--sky-color-background-action-tertiary-base);
    border-width: var(--sky-width-action-base);
    border-color: var(--sky-color-border-action-tertiary-base);
  }

  .sky-switch-input {
    &:checked:not(:disabled) + .sky-switch-control.sky-switch-control-icon,
    &[type='checkbox']:indeterminate:not(:disabled)
      + .sky-switch-control.sky-switch-control-icon {
      border-width: var(--sky-border-width-selected-s);
      color: var(
        --sky-override-switch-checked-color,
        var(--sky-color-icon-inverse)
      );
    }

    &:disabled + .sky-switch-control.sky-switch-control-icon {
      border-color: var(--sky-color-border-action-tertiary-disabled);
      border-width: var(
        --sky-override-icon-switch-border-width-disabled,
        var(--sky-border-width-action-disabled)
      );
    }

    &:checked:disabled + .sky-switch-control.sky-switch-control-icon,
    &[type='checkbox']:indeterminate:disabled
      + .sky-switch-control.sky-switch-control-icon {
      color: var(--sky-color-icon-action);
    }

    &:not(:disabled) {
      &:hover + .sky-switch-control {
        border-width: var(--sky-border-width-action-hover);
        border-color: var(--sky-color-border-action-tertiary-hover);
      }

      &:active + .sky-switch-control {
        border-width: var(--sky-border-width-action-active);
        border-color: var(--sky-color-border-action-tertiary-active);
      }

      &:focus-visible + .sky-switch-control {
        border-width: var(--sky-border-width-action-focus);
        border-color: var(--sky-color-border-action-tertiary-focus);
      }
    }
  }

  .sky-switch-control {
    &.sky-switch-control-icon {
      width: var(
        --sky-override-icon-switch-size-group,
        calc(
          calc(var(--sky-font-line_height-body-m) * var(--sky-font-size-body-m)) +
            calc(var(--sky-space-inset-balanced-s) * 2)
        )
      );
      height: var(
        --sky-override-icon-switch-size-group,
        calc(
          calc(var(--sky-font-line_height-body-m) * var(--sky-font-size-body-m)) +
            calc(var(--sky-space-inset-balanced-s) * 2)
        )
      );
      font-size: var(
        --sky-override-icon-switch-font-size-group,
        var(--sky-size-icon-m)
      );
    }
  }

  sky-checkbox .sky-switch-control-icon {
    margin-right: var(--sky-space-gap-action_group-m);
  }

  sky-checkbox:last-of-type .sky-switch-control-icon {
    margin-right: 0;
  }

  sky-radio .sky-switch-control-icon {
    border-radius: var(--sky-border-radius-s);
  }
}
