@use '../../styles/abstract/base' as base;
@use '../../styles/abstract/variables' as v;
@use '../../styles/abstract/functions' as f;
@use '../../styles/abstract/mixins' as m;
@use '../../styles/abstract/theme' as t;
@use '../../styles/abstract/direction' as d;

$component: '#{base.$prefix}-switch';

.#{$component} {
display: inline-flex;
flex-direction: column;
position: relative;
width: 100%;
padding: 4px 0;
user-select: none;

&__container {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
min-height: 48px;
width: 100%;
column-gap: 12px;
}

&:where(:not(:has(.#{$component}__label, .#{$component}__helper))) {
.#{$component}__container {
column-gap: 0;
min-height: 40px;
}
}

&__content {
display: flex;
flex-direction: column;
flex-grow: 1;
}

&--label-end &__container {
flex-direction: row-reverse;
}

&__label {
@include m.typography('body-large');
color: t.color('on-surface');
}

&__input {
font: inherit;
position: absolute;
opacity: 0;
width: 100%;
height: 100%;
margin: 0;
cursor: pointer;
z-index: 1;

&:disabled {
cursor: not-allowed;
}

&:focus-visible ~ .#{$component}__track {
outline: 3px solid t.color('secondary');
outline-offset: 2px;
}
}

&__track {
position: relative;
flex-shrink: 0;
min-width: 52px;
height: 32px;
box-sizing: border-box;
border-radius: v.shape('full');
background-color: t.color('surface-container-highest');
border: 2px solid t.color('outline');
transition: m.spring(background-color, fast-effects), m.spring(border-color, fast-effects);

&::after {
content: '';
position: absolute;
width: 40px;
height: 40px;
top: -6px;
@include d.start-value(--#{$component}-layer-start, -6px);
background-color: t.color('on-surface');
opacity: 0;
border-radius: 50%;
z-index: 0;
transition: m.spring(opacity, fast-effects), m.spring(inset-inline-start, fast-spatial),
m.spring(inset, fast-spatial);
pointer-events: none;
}
}

&__thumb {
position: absolute;
top: 6px;
@include d.start-value(--#{$component}-thumb-start, 6px);
width: 16px;
height: 16px;
border-radius: 50%;
background-color: t.color('outline');
transition: m.spring(inset-inline-start, fast-spatial), m.spring(inset, fast-spatial),
m.spring(width, fast-spatial), m.spring(height, fast-spatial), m.spring(background-color, fast-effects);
display: flex;
align-items: center;
justify-content: center;
z-index: 1; /* Ensure thumb is above ripple */
pointer-events: none;

&-icon {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
transform: scale(0);
transition: m.spring(opacity, fast-effects), m.spring(transform, fast-spatial);
color: t.color('on-primary-container');

svg {
width: 16px;
height: 16px;
}

&--unselected {
color: t.color('surface-container-highest');
}
}
}

&__helper {
@include m.typography('body-medium');
color: t.color('on-surface-variant');

&--error {
color: t.color('error');
}
}

&--icons:not(&--checked) {
.#{$component}__thumb {
width: 24px;
height: 24px;
top: 2px;
@include d.start-value-set(--#{$component}-thumb-start, 2px);
}

.#{$component}__thumb-icon--unselected {
opacity: 1;
transform: scale(1);
}
}

&--checked {
.#{$component}__track {
background-color: t.color('primary');
border-color: t.color('primary');

&::after {
@include d.start-value-set(--#{$component}-layer-start, 14px);
background-color: t.color('primary');
}
}

.#{$component}__thumb {
background-color: t.color('on-primary');
width: 24px;
height: 24px;
top: 2px;
@include d.start-value-set(--#{$component}-thumb-start, 22px);
}

.#{$component}__thumb-icon:not(.#{$component}__thumb-icon--unselected) {
opacity: 1;
transform: scale(1);
}
}

&--dense {
.#{$component}__container {
min-height: initial;
}
}

&--error {
.#{$component}__track {
border-color: t.color('error');
}
}

&--disabled {
pointer-events: none;

.#{$component}__track {
background-color: t.alpha('on-surface', 0.12);
border-color: t.alpha('on-surface', 0.12);
}

.#{$component}__thumb {
background-color: t.alpha('on-surface', 0.38);

&-icon--unselected {
color: t.alpha('surface-container-highest', 0.38);
}
}

.#{$component}__label, .#{$component}__helper {
color: t.alpha('on-surface', 0.38);
}

&.#{$component}--checked {
.#{$component}__track {
border-color: transparent;
}

.#{$component}__thumb {
background-color: t.color('surface');

&-icon {
color: t.alpha('on-surface', 0.38);
}
}
}
}

&:not(.#{$component}--disabled) {
.#{$component}__input:is(:hover, :focus-visible, :active) ~ .#{$component}__track .#{$component}__thumb {
background-color: t.color('on-surface-variant');
}

&.#{$component}--checked .#{$component}__input:is(:hover, :focus-visible, :active) ~ .#{$component}__track .#{$component}__thumb {
background-color: t.color('primary-container');
}

.#{$component}__input:hover ~ .#{$component}__track::after {
opacity: v.state('hover-state-layer-opacity');
}

.#{$component}__input:focus-visible ~ .#{$component}__track::after {
opacity: v.state('focus-state-layer-opacity');
}

.#{$component}__input:active ~ .#{$component}__track::after {
opacity: v.state('pressed-state-layer-opacity');
}

.#{$component}__input:active ~ .#{$component}__track .#{$component}__thumb {
width: 28px;
height: 28px;
top: 0;
@include d.start-value-set(--#{$component}-thumb-start, 0);
}

&.#{$component}--checked .#{$component}__input:active ~ .#{$component}__track .#{$component}__thumb {
@include d.start-value-set(--#{$component}-thumb-start, 20px);
}
}

@include d.arms($self: true) {
.#{$component}__track::after {
left: auto;
right: var(--#{$component}-layer-start, -6px);
}

.#{$component}__thumb {
left: auto;
right: var(--#{$component}-thumb-start, 6px);
}
}

@include m.reduced-motion {
.#{$component}__track,
.#{$component}__thumb,
.#{$component}__thumb-icon {
transition: none;
}
}

@include m.high-contrast {
.#{$component}__track {
border-width: 2px;
}

&--checked .#{$component}__track {
border-color: currentColor;
}
}

}