@import "../variables/variables";
@import "../mixins/private/token-mixins";

.switch {
    box-sizing: border-box;
    height: 40px;
    position: relative;
    vertical-align: middle;
}

div.switch {
    display: flex;
}

span.switch {
    display: inline-flex;
}

span.switch__button {
    align-self: center;
    border-radius: 400px;
    border-style: solid;
    border-width: 1px;
    color: transparent;
    display: inline-block;
    height: 24px;
    position: relative;
    text-indent: 100%;
    transition: left 0.15s ease-out 0s;
    width: 40px;
    @include background-color-token(
        switch-unchecked-background-color,
        color-background-secondary
    );
    @include border-color-token(switch-border-color, color-stroke-strong);

    &::after {
        @include background-color-token(
            switch-foreground-color,
            color-background-primary
        );
        @include border-color-token(
            switch-foreground-border-color,
            color-stroke-strong
        );

        border-radius: 50%;
        border-style: solid;
        border-width: 1px;
        content: "";
        display: block;
        height: 24px;
        left: -1px;
        position: absolute;
        top: -1px;
        transform: translate3d(0, 0, 0);
        transition: left 0.15s ease-out 0s;
        width: 24px;
    }
}

input.switch__control,
span.switch__control {
    height: 24px;
    left: 0;
    margin: 0;
    outline: 0;
    padding: 0;
    position: absolute;
    top: 8px;
    width: 40px;
    z-index: 1;
}

/* OFF > Track > Disabled */
input.switch__control[disabled] + span.switch__button,
span.switch__control[aria-disabled="true"] + span.switch__button {
    @include border-color-token(switch-border-color, color-stroke-disabled);
}

input.switch__control {
    opacity: 0;

    &:focus + span.switch__button {
        @include outline-token(
            1px,
            auto,
            switch-custom-outline-color,
            color-foreground-secondary
        );
    }

    &:focus:not(:focus-visible) + span.switch__button {
        outline: none;
    }
}

/* OFF > Knob > Disabled */
input.switch__control[disabled] + span.switch__button::after,
span.switch__control[aria-disabled="true"] + span.switch__button::after {
    @include border-color-token(switch-border-color, color-background-disabled);
}

input.switch__control:not([disabled]):hover + span.switch__button,
span.switch__control:not([aria-disabled="true"]):hover + span.switch__button,
input.switch__control:not([disabled]):focus + span.switch__button,
span.switch__control:not([aria-disabled="true"]):focus + span.switch__button {
    @include background-color-token(
        switch-unchecked-hover-background-color,
        color-state-secondary-hover
    );
}

/* ON > Track */
input.switch__control:not([disabled]):checked + span.switch__button,
span.switch__control:not([aria-disabled="true"])[aria-checked="true"]
    + span.switch__button {
    @include background-color-token(
        switch-checked-background-color,
        color-background-accent
    );
}

/* ON > Track > Disabled */
input.switch__control[disabled]:checked + span.switch__button,
span.switch__control[aria-disabled="true"][aria-checked="true"]
    + span.switch__button {
    @include background-color-token(
        switch-disabled-background-color,
        color-background-disabled
    );
    @include border-color-token(switch-border-color, color-background-disabled);
}

span.switch__control:not([aria-disabled="true"]):focus-visible
    + span.switch__button,
input.switch__control:not([disabled]):focus-visible + span.switch__button {
    box-shadow:
        0 0 0 2px var(--color-background-primary),
        0 0 0 4px var(--color-state-accent-hover);
}

input.switch__control:not([disabled]):checked:hover + span.switch__button,
span.switch__control:not([aria-disabled="true"])[aria-checked="true"]:hover
    + span.switch__button,
input.switch__control:not([disabled]):checked:focus + span.switch__button,
span.switch__control:not([aria-disabled="true"])[aria-checked="true"]:focus
    + span.switch__button {
    @include background-color-token(
        switch-checked-hover-background-color,
        color-state-accent-hover
    );
}

/* ON > Knob */
input.switch__control:checked + span.switch__button::after,
span.switch__control[aria-checked="true"] + span.switch__button::after {
    left: 15px;
}

/* https://developer.paciellogroup.com/blog/2018/03/focus-visible-and-backwards-compatibility/ */
span.switch__control:focus:not(:focus-visible) {
    outline: 0;
}

@media screen and (-ms-high-contrast: active) {
    input.switch__control {
        opacity: 1;
    }
}
