@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;

$bubble-in: v.motion('duration-medium4') v.motion('easing-emphasized-decelerate');
$bubble-out: v.motion('duration-short3') v.motion('easing-emphasized-accelerate');
$bubble-transition: scale $bubble-out, opacity $bubble-out, visibility 0s linear v.motion('duration-short3');
$bubble-transition-visible: scale $bubble-in, opacity $bubble-in, visibility 0s;

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

.#{$component} {
position: relative;
width: 100%;
display: flex;
flex-direction: column;
user-select: none;
touch-action: none;

--#{base.$prefix}-slider-track-color: var(--#{base.$prefix}-slider-color, #{t.color('primary')});
--#{base.$prefix}-slider-inactive-track-color: var(--#{base.$prefix}-slider-container-color, #{t.color('secondary-container')});
--#{base.$prefix}-slider-tick-color: var(--#{base.$prefix}-slider-on-color, var(--#{base.$prefix}-slider-inactive-track-color));
--#{base.$prefix}-slider-active-opacity: 1;
--#{base.$prefix}-slider-inactive-opacity: 1;
--#{base.$prefix}-slider-tick-opacity: 1;
--#{base.$prefix}-slider-active-tick-opacity: 1;

&:focus {
outline: none;
}

&__label {
display: block;
@include m.typography('body-large');
color: t.color('on-surface');
margin-bottom: 4px;
&--end { margin-bottom: 0; margin-top: 4px; }
}

&__icon {
display: flex;
align-items: center;
justify-content: center;
color: t.color('on-surface-variant');
width: 40px;
height: 40px;

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

&--icon {
display: grid;
grid-template-columns: 40px minmax(0, 1fr);
grid-template-areas: ". label" "icon track";
column-gap: 8px;
align-items: center;
.#{$component}__label { grid-area: label; }
.#{$component}__container { grid-area: track; }
.#{$component}__icon { grid-area: icon; }
}
&--icon.#{$component}--label-end { grid-template-areas: "icon track" ". label"; }
&--icon-end {
grid-template-columns: minmax(0, 1fr) 40px;
grid-template-areas: "label ." "track icon";
}
&--icon-end.#{$component}--label-end { grid-template-areas: "track icon" "label ."; }

&__container {
position: relative;
height: 40px;
display: flex;
align-items: center;
}

@each $color in (secondary, tertiary, error) {
&--#{$color} {
--#{base.$prefix}-slider-track-color: var(--#{base.$prefix}-slider-color, #{t.color($color)});
--#{base.$prefix}-slider-inactive-track-color: var(--#{base.$prefix}-slider-container-color, #{t.color($color + '-container')});
}
}
&--disabled {
--#{base.$prefix}-slider-track-color: #{t.color('on-surface')};
--#{base.$prefix}-slider-inactive-track-color: #{t.color('on-surface')};
--#{base.$prefix}-slider-tick-color: #{t.color('on-surface')};
--#{base.$prefix}-slider-active-opacity: 0.38;
--#{base.$prefix}-slider-inactive-opacity: 0.12;
--#{base.$prefix}-slider-tick-opacity: 0.38;
--#{base.$prefix}-slider-active-tick-opacity: 0.12;
}
&__visual { position: absolute; inset: 0; pointer-events: none; container-type: size; }
&__track { position: absolute; top: 50%; width: 100%; transform: translateY(-50%); }
&__segment {
position: absolute; height: 100%; border-radius: 2px;
background: var(--#{base.$prefix}-slider-inactive-track-color); opacity: var(--#{base.$prefix}-slider-inactive-opacity);
&--active { background: var(--#{base.$prefix}-slider-track-color); opacity: var(--#{base.$prefix}-slider-active-opacity); }
}
&__ticks {
position: absolute; inset: 0; color: var(--#{base.$prefix}-slider-track-color); opacity: var(--#{base.$prefix}-slider-tick-opacity);
background-image: radial-gradient(circle at center, currentColor 2px, transparent 2px);
background-repeat: repeat-x;
&--active { color: var(--#{base.$prefix}-slider-tick-color); opacity: var(--#{base.$prefix}-slider-active-tick-opacity); }
}
&__inset-icon {
position: absolute; top: 50%; transform: translateY(-50%);
display: flex; align-items: center; justify-content: center;
color: var(--#{base.$prefix}-slider-tick-color); opacity: var(--#{base.$prefix}-slider-active-tick-opacity);
svg { width: 100%; height: 100%; fill: currentColor; }
&--inactive { color: var(--#{base.$prefix}-slider-track-color); opacity: var(--#{base.$prefix}-slider-tick-opacity); }
}
&__dot { position: absolute; top: calc(50% - 2px); width: 4px; height: 4px;
border-radius: 50%; background: var(--#{base.$prefix}-slider-track-color); opacity: var(--#{base.$prefix}-slider-active-opacity); }

&__handle {
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
cursor: pointer;
z-index: 4;
width: 14px;
height: 50px;
border-radius: 16px;
background-color: rgba(red, .0);

&:focus {
outline: none;
}

&::before {
content: '';
position: absolute;
width: 4px;
height: 100%;
background-color: var(--#{base.$prefix}-slider-color, #{t.color('primary')});
border-radius: v.shape('full');
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
pointer-events: none;

.#{$component}--secondary & {
background-color: var(--#{base.$prefix}-slider-color, #{t.color('secondary')});
}

.#{$component}--tertiary & {
background-color: var(--#{base.$prefix}-slider-color, #{t.color('tertiary')});
}

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

.#{$component}--dragging &::before,
&:active::before,
&:focus-visible::before,
&--focused::before {
width: 2px;
}

&:focus-visible::before {
outline: 2px solid t.color('secondary');
outline-offset: 2px;
}
}

&--range {
.#{$component}__handle {
&:nth-of-type(1) {
z-index: 3;
}
}

.#{$component}__dot {
z-index: 2;
}
}

&__value {
position: absolute;
bottom: calc(50% + var(--#{base.$prefix}-slider-handle-height, 44px) / 2 + 12px);
left: 0;
box-sizing: border-box;
min-width: 48px;
height: 44px;
padding: 0 16px;
display: flex;
align-items: center;
justify-content: center;
background-color: t.color('inverse-surface');
color: t.color('inverse-on-surface');
border-radius: v.shape('full');
@include m.typography('label-large');
white-space: nowrap;
transform: translateX(-50%);
transform-origin: 50% 100%;
scale: 0;
visibility: hidden;
opacity: 0;
transition: $bubble-transition;
pointer-events: none;

&--visible {
visibility: visible;
opacity: 1;
scale: 1;
transition: $bubble-transition-visible;
}
}

$settle-position: m.spring(left, default-spatial), m.spring(right, default-spatial), m.spring(bottom, default-spatial), m.spring(top, default-spatial);
$settle-length: m.spring(width, default-spatial), m.spring(height, default-spatial);
&--settling {
.#{$component}__handle, .#{$component}__inset-icon { transition: $settle-position; }
.#{$component}__segment { transition: $settle-position, $settle-length; }
.#{$component}__value { transition: $settle-position, $bubble-transition; }
.#{$component}__value--visible { transition: $settle-position, $bubble-transition-visible; }
}

&--vertical {
width: auto;
height: 100%;
align-items: center;

.#{$component}__container { height: 100%; min-height: 120px; }
.#{$component}__track { top: 0; left: 50%; width: auto; height: 100%; transform: translateX(-50%); }
.#{$component}__segment { width: 100%; }
.#{$component}__ticks { background-repeat: repeat-y; }
.#{$component}__dot { top: auto; left: calc(50% - 2px); }
.#{$component}__inset-icon { top: auto; left: 50%; transform: translateX(-50%); }
.#{$component}__handle {
top: auto;
left: 50%;
height: 14px;
&::before { width: 100%; height: 4px; }
}
&.#{$component}--dragging .#{$component}__handle::before,
.#{$component}__handle:active::before,
.#{$component}__handle:focus-visible::before,
.#{$component}__handle--focused::before {
width: 100%;
height: 2px;
}
.#{$component}__value {
left: auto;
bottom: auto;
right: calc(50% + var(--#{base.$prefix}-slider-handle-height, 44px) / 2 + 12px);
transform-origin: 100% 50%;
}
}

&--disabled {

.#{$component}__handle {
cursor: default;
pointer-events: none;

&::before {
opacity: 0.38;
background-color: t.color('on-surface');
}
}
}

@include m.high-contrast {
&__handle::before {
border: 1px solid ButtonText;
box-sizing: border-box;
}

&__handle:focus-visible::before {
outline: 2px solid Highlight;
outline-offset: 2px;
}
}

}