@use 'sass:map';
@use 'sass:list';
@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;

$component: '#{base.$prefix}-button';
$collapsed: '.#{base.$prefix}-toolbar--collapsed';


$sizes: xs, s, m, l, xl;

$label-typescale: (
'xs': 'label-large',
's': 'label-large',
'm': 'title-medium',
'l': 'headline-small',
'xl': 'headline-large'
);

$content-color: (
'filled': 'on-primary',
'elevated': 'primary',
'tonal': 'on-secondary-container',
'outlined': 'on-surface-variant',
'text': 'primary'
);

.#{$component} {
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
gap: v.button('icon-gap-s');
min-width: v.button('min-width');
height: f.density(v.button('height-s'), 32px);
padding: 0 v.button('padding-s');
border: none;
border-radius: var(--#{base.$prefix}-button-shape, #{f.density(v.button('shape-round-s'), 16px, 2px)});
background-color: transparent;
color: inherit;
font: inherit;
text-decoration: none;
cursor: pointer;
user-select: none;
vertical-align: middle;
appearance: none;
will-change: border-radius;

@include m.typography('label-large');

transition:
background-color v.motion('spring-default-effects-duration') v.motion('spring-default-effects-easing'),
color v.motion('spring-default-effects-duration') v.motion('spring-default-effects-easing'),
box-shadow v.motion('spring-default-effects-duration') v.motion('spring-default-effects-easing'),
border-color v.motion('spring-default-effects-duration') v.motion('spring-default-effects-easing'),
border-radius v.motion('spring-fast-spatial-duration') v.motion('spring-fast-spatial-easing');

&:focus {
outline: none;
}

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

&:disabled {
pointer-events: none;
color: t.alpha('on-surface-variant', 0.38);
background-color: t.alpha('on-surface', 0.1);
box-shadow: none;

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

&::before {
content: '';
position: absolute;
inset: 0;
background-color: currentColor;
opacity: 0;
pointer-events: none;
border-radius: inherit;
transition: opacity v.motion('duration-short2') v.motion('easing-standard');
}

&:hover::before {
opacity: f.get-state-opacity('hover');
}

&:focus-visible::before {
opacity: f.get-state-opacity('focus');
}

&:active:not(:has(> .#{base.$prefix}-ripple))::before,
&--active:not(:has(> .#{base.$prefix}-ripple))::before {
opacity: f.get-state-opacity('pressed');
}

&__icon {
display: inline-flex;
align-items: center;
justify-content: center;
flex: none;
width: v.button('icon-size-s');
height: v.button('icon-size-s');

svg {
width: 100%;
height: 100%;
}
}

&__progress {
display: inline-flex;
align-items: center;
justify-content: center;
flex: none;
width: v.button('icon-size-s');
height: v.button('icon-size-s');

&.#{base.$prefix}-progress {
width: v.button('icon-size-s');
height: v.button('icon-size-s');
transition: opacity v.motion('duration-short2') v.motion('easing-standard');
}

.#{base.$prefix}-progress__canvas {
color: currentColor;
}
}

&__text {
@include m.truncate;

:host-context(#{$collapsed}) .#{$component}--icon & {
@include m.visually-hidden;
}

#{$collapsed} .#{$component}--icon & {
@include m.visually-hidden;
}
}

@each $size in $sizes {
&--#{$size} {
height: f.density(v.button('height-#{$size}'), 32px);
padding: 0 v.button('padding-#{$size}');
gap: v.button('icon-gap-#{$size}');
@include m.typography(map.get($label-typescale, $size));

.#{$component}__icon,
.#{$component}__progress,
.#{$component}__progress.#{base.$prefix}-progress {
width: v.button('icon-size-#{$size}');
height: v.button('icon-size-#{$size}');
}

border-radius: var(--#{base.$prefix}-button-shape, #{f.density(v.button('shape-round-#{$size}'), 16px, 2px)});

&.#{$component}--square {
border-radius: var(--#{base.$prefix}-button-shape, v.button('shape-square-#{$size}'));
}

&.#{$component}--outlined {
border-width: v.button('outline-width-#{$size}');
}
}
}

&--s.#{$component}--icon {
padding-inline-start: v.button('padding-icon');

&:has(.#{$component}__icon--end) {
padding-inline-start: v.button('padding-s');
padding-inline-end: v.button('padding-icon');
}
}

&::after {
content: '';
position: absolute;
left: 0;
right: 0;
top: 50%;
height: v.button('touch-target');
transform: translateY(-50%);
}

&:active,
&--active {
border-radius: var(--#{base.$prefix}-button-shape-pressed, v.button('shape-pressed-s'));
box-shadow: none;
}

@each $size in $sizes {
&--#{$size} {
&:active,
&.#{$component}--active {
border-radius: var(--#{base.$prefix}-button-shape-pressed, v.button('shape-pressed-#{$size}'));
}
}
}

@each $variant, $role in $content-color {
&--#{$variant} {
@if $variant == 'text' {
color: var(--#{base.$prefix}-button-text-color, #{t.color($role)});
} @else {
color: t.color($role);
}
}
}

&--filled {
background-color: t.color('primary');

&:hover {
@include m.elevation(1);
}

&:active,
&.#{$component}--active,
&:disabled {
@include m.elevation(0);
}
}

&--elevated {
background-color: t.color('surface-container-low');
@include m.elevation(1);

&:hover {
@include m.elevation(2);
}

&:active,
&.#{$component}--active {
@include m.elevation(1);
}

&:disabled {
box-shadow: none;
}
}

&--tonal {
background-color: t.color('secondary-container');

&:hover {
@include m.elevation(1);
}

&:active,
&.#{$component}--active,
&:disabled {
@include m.elevation(0);
}
}

&--outlined {
border: v.button('outline-width-s') solid t.color('outline-variant');

&:disabled {
background-color: transparent;
border-color: t.alpha('outline-variant', 0.1);
}
}

&--text {
padding: 0 v.button('padding-horizontal-small');

&.#{$component}--icon {
padding-inline: v.button('padding-horizontal-small') v.button('padding-s');

&:has(.#{$component}__icon--end) {
padding-inline: v.button('padding-s') v.button('padding-horizontal-small');
}
}

@each $size in m, l, xl {
&.#{$component}--#{$size} {
padding: 0 v.button('padding-#{$size}');
}
}

&:disabled {
background-color: transparent;
}
}

&--toggle {
&.#{$component}--filled,
&.#{$component}--tonal {
&,
&:hover,
&:active,
&.#{$component}--active {
box-shadow: none;
}
}

&.#{$component}--filled {
background-color: t.color('surface-container');
color: t.color('on-surface-variant');
}

&.#{$component}--selected {
$selected: (
'filled': ('primary', 'on-primary'),
'elevated': ('primary', 'on-primary'),
'tonal': ('secondary', 'on-secondary'),
'outlined': ('inverse-surface', 'inverse-on-surface')
);

@each $variant, $roles in $selected {
$container: list.nth($roles, 1);
$content: list.nth($roles, 2);

&.#{$component}--#{$variant} {
background-color: t.color($container);
color: t.color($content);
}
}

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

@each $size in $sizes {
&.#{$component}--#{$size} {
border-radius: var(--#{base.$prefix}-button-shape-selected, v.button('shape-square-#{$size}'));
}
}

@each $size in $sizes {
&.#{$component}--square.#{$component}--#{$size} {
border-radius: var(--#{base.$prefix}-button-shape-selected, #{f.density(v.button('shape-round-#{$size}'), 16px, 2px)});
}
}

@each $size in $sizes {
&.#{$component}--#{$size}:active,
&.#{$component}--#{$size}.#{$component}--active {
border-radius: var(--#{base.$prefix}-button-shape-pressed, v.button('shape-pressed-#{$size}'));
}
}
}

&:disabled,
&.#{$component}--selected:disabled {
color: t.alpha('on-surface-variant', 0.38);
background-color: t.alpha('on-surface', 0.1);
border-color: t.alpha('outline-variant', 0.1);
box-shadow: none;
}

&.#{$component}--outlined:disabled {
background-color: transparent;
}
}

&--circular {
flex: none !important;
border-radius: 50%;
padding: 8px;
min-width: unset;
width: f.density(v.button('height-s'), 32px) !important;
height: f.density(v.button('height-s'), 32px);

&:active,
&.#{$component}--active {
border-radius: 50%;

&.#{$component}--square {
border-radius: v.shape('medium') !important;
}
}

&.#{$component}--square {
border-radius: v.shape('medium');
}

@each $size in $sizes {
&.#{$component}--#{$size} {
width: f.density(v.button('height-#{$size}'), 32px);
height: f.density(v.button('height-#{$size}'), 32px);
padding: 0;
}
}

.#{$component}__text {
display: none;
}

&.#{$component}--progress {
padding: 0 !important;
}
}

&[disabled].#{$component}--progress .#{$component}__progress {
opacity: 0.7;
}
}

#{$collapsed} .#{$component}.#{$component}--icon.#{$component}--icon {
min-width: v.button('touch-target');
padding-inline: v.button('padding-icon');
}

:host-context(#{$collapsed}) .#{$component}.#{$component}--icon.#{$component}--icon {
min-width: v.button('touch-target');
padding-inline: v.button('padding-icon');
}
