
@use "../../../variables/index" as *;

/**
 * C8Y Switch - Toggle switch form control
 *
 * Note: Uses $switch-sm variable and $form-control-* variables. Uses $size-* tokens where applicable.
 *
 * Intentionally hardcoded values:
 * - Fine-tuning offsets (2px, 3px, 6px): Switch handle positioning and spacing
 * - Hardcoded height (32px): Form control height reference in calc
 * - Box-shadow values (2px, 3px): Shadow depth
 * - Outline width (2px): Focus indicator
 * - Transition durations (0.25s): Animation timing
 * - Percentages (33.333%, 100%): Fractional layout
 * - Opacity values: Visual effects
 * - RGBA values: Shadow colors
 */

// Note: Mixins with @content not used - expanded inline for LESS compatibility

.c8y-switch {
  display: flex;
  position: relative;
  overflow: hidden;
  margin: 0;
  padding: 0 3px 0 calc($switch-sm * 2 + 5px);
  min-height: $form-control-height-base;
  color: $form-control-color-default;
  text-transform: none;
  text-overflow: ellipsis;
  white-space: nowrap;
  letter-spacing: 0;
  font-weight: normal;
  font-size: inherit;
  line-height: $form-control-height-base;

  + .c8y-switch {
    margin-left: 6px;
  }

  &:hover {
    cursor: pointer;
  }

  input[type='checkbox'] {
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0;

    & + span {
      position: absolute;
      top: calc((32px - $switch-sm) / 2);
      left: 2px;
      z-index: 1;
      height: $switch-sm;
      border: 0;
      background-color: transparent;

      &:before,
      &:after {
        position: absolute;
        content: '';
      }

      &::before {
        top: 0;
        bottom: 0;
        left: 0;
        z-index: -1;
        width: calc($switch-sm * 2 - 2px);
        border-radius: calc($switch-sm / 2);
        background-color: $switch-background-default;
        box-shadow: none;

        transition: background 0.25s ease;
      }

      &::after {
        position: absolute;
        top: 3px;
        bottom: 3px;
        left: 3px;
        z-index: -1;
        width: calc($switch-sm - 6px);
        border-radius: calc($switch-sm / 2) !important;
        background-color: var(--c8y-switch-handle-background, $form-control-background-default);
        content: '';

        box-shadow: 0 2px 3px rgba($black, 0.2);
        transition: all 0.25s ease !important;
      }
    }

    &:focus {
      + span {
        &::before {
          outline: 2px solid $component-color-focus;
        }
      }
    }
  }

  input[disabled] + span {
    opacity: 0.5;

    &:before,
    &:after {
      cursor: not-allowed;
    }
  }

  input:checked + span {
    &:before {
      background-color: $switch-background-active;
    }

    &:after {
      left: $switch-sm;
    }
  }

  &--inline {
    overflow: visible;
    min-height: $switch-sm;
    height: $switch-sm;
    line-height: $line-height-base;
    display: inline-flex;

    & input[type='checkbox'] + span {
      position: absolute;
      top: 0;
    }
  }
}

.c8y-switch-multistate {
  display: flex;
  justify-content: stretch;
  align-items: center;
  height: $form-control-height-base;
  border-radius: $size-16;
  width: 100%;
  background: $gray-60;
  position: relative;
  line-height: 1;
  [class^='dlt-c8y-icon-'],
  [class*=' dlt-c8y-icon-'],
  .c8y-icon {
    font-size: $size-16;
  }

  input {
    opacity: 0;
    width: 0;
    height: 0;
    margin: 0;
  }

  label {
    flex: 1 1 100%;
    margin: 0;
    opacity: 1;
    display: block;
    text-align: center;
    cursor: pointer;
    position: relative;
    z-index: 5;
  }

  &__handle {
    display: block;
    height: $size-24;
    width: calc(33.333% - $size-8);
    background-color: var(--c8y-switch-handle-background, $form-control-background-default);
    box-shadow: 0 2px 3px rgba($black, 0.2);
    border-radius: $size-16;
    position: absolute;
    transition: left 0.25s ease;
    top: $size-4;
    z-index: 3;
    transform: translate(-50%, 0);
    .c8y-dark-theme & {
      --c8y-switch-handle-background: var(--brand-primary, var(--c8y-brand-primary));
    }
    .c8y-system-theme & {
      @media (prefers-color-scheme: dark) {
        --c8y-switch-handle-background: var(--brand-primary, var(--c8y-brand-primary));
      }
    }
  }

  input {
    &:focus {
      ~ .c8y-switch-multistate__handle {
        outline: 2px solid $component-color-focus;
      }
    }

    &:checked,
    &:focus {
      &:nth-child(1) {
        ~ .c8y-switch-multistate__handle {
          left: calc(100% / 6);
        }
      }

      &:nth-child(3) {
        ~ .c8y-switch-multistate__handle {
          left: calc((100% / 6) * 3);
        }
      }

      &:nth-child(5) {
        ~ .c8y-switch-multistate__handle {
          left: calc((100% / 6) * 5);
        }
      }
    }
  }
  .c8y-dark-theme & {
    input:checked + label {
      color: $gray-100;
    }
  }
  .c8y-system-theme & {
    @media (prefers-color-scheme: dark) {
      input:checked + label {
        color: $gray-100;
      }
    }
  }
}
