@use "sass:map";
@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}-icon-button";


$sizes: (
"xs": (
container: 32px,
round-radius: 16px,
icon: 20px,
square-radius: v.shape('medium'),
pressed-radius: v.shape('small'),
outline-width: 1px,
),
"s": (
container: 40px,
round-radius: 20px,
icon: 24px,
square-radius: v.shape('medium'),
pressed-radius: v.shape('small'),
outline-width: 1px,
),
"m": (
container: 56px,
round-radius: 28px,
icon: 24px,
square-radius: v.shape('large'),
pressed-radius: v.shape('medium'),
outline-width: 1px,
),
"l": (
container: 96px,
round-radius: 48px,
icon: 32px,
square-radius: v.shape('extra-large'),
pressed-radius: v.shape('large'),
outline-width: 2px,
),
"xl": (
container: 136px,
round-radius: 68px,
icon: 40px,
square-radius: v.shape('extra-large'),
pressed-radius: v.shape('large'),
outline-width: 3px,
),
);

$widths: (
"xs": (
narrow: 28px,
default: 32px,
wide: 40px,
),
"s": (
narrow: 32px,
default: 40px,
wide: 52px,
),
"m": (
narrow: 48px,
default: 56px,
wide: 72px,
),
"l": (
narrow: 64px,
default: 96px,
wide: 128px,
),
"xl": (
narrow: 104px,
default: 136px,
wide: 184px,
),
);

@function get-size($size, $property) {
@return map.get(map.get($sizes, $size), $property);
}

@function get-width($size, $width-variant) {
@return map.get(map.get($widths, $size), $width-variant);
}

.#{$component} {
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
border: none;
padding: 0;
background-color: transparent;
color: t.color("on-surface-variant");
font: inherit;
text-decoration: none;
cursor: pointer;
user-select: none;
vertical-align: middle;
appearance: none;
overflow: visible;
flex-shrink: 0;

width: f.density(get-size("s", container), 32px);
height: f.density(get-size("s", container), 32px);

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

transition:
background-color v.motion("duration-short2") v.motion("easing-standard"),
color v.motion("duration-short2") v.motion("easing-standard"),
border-color v.motion("duration-short2") v.motion("easing-standard"),
border-radius v.motion("duration-short1")
v.motion("easing-standard-accelerate"),
transform v.motion("duration-short1")
v.motion("easing-standard-accelerate"),
box-shadow v.motion("duration-short2") v.motion("easing-standard");

&:focus {
outline: none;
}

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

&::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 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: 0.08;
}

&:focus-visible::before {
opacity: 0.1;
}

&:active:not(:has(> .#{base.$prefix}-ripple))::before {
opacity: 0.1;
}

&__icon,
.#{base.$prefix}-icon,
.#{$component}__icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: get-size("s", icon);
height: get-size("s", icon);
pointer-events: none;

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

&--xs {
width: f.density(get-size("xs", container), 32px);
height: f.density(get-size("xs", container), 32px);

.#{base.$prefix}-icon,
.#{$component}__icon {
width: get-size("xs", icon);
height: get-size("xs", icon);
}
}

&::after {
content: "";
position: absolute;
width: 48px;
height: 48px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

&--m {
width: f.density(get-size("m", container), 32px);
height: f.density(get-size("m", container), 32px);

.#{base.$prefix}-icon,
.#{$component}__icon {
width: get-size("m", icon);
height: get-size("m", icon);
}
}

&--l {
width: f.density(get-size("l", container), 32px);
height: f.density(get-size("l", container), 32px);

.#{base.$prefix}-icon,
.#{$component}__icon {
width: get-size("l", icon);
height: get-size("l", icon);
}
}

&--xl {
width: f.density(get-size("xl", container), 32px);
height: f.density(get-size("xl", container), 32px);

.#{base.$prefix}-icon,
.#{$component}__icon {
width: get-size("xl", icon);
height: get-size("xl", icon);
}
}


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

&.#{$component}--xs {
border-radius: var(--#{base.$prefix}-button-shape, #{f.density(get-size("xs", round-radius), 16px, 2px)});
}

&.#{$component}--m {
border-radius: var(--#{base.$prefix}-button-shape, #{f.density(get-size("m", round-radius), 16px, 2px)});
}

&.#{$component}--l {
border-radius: var(--#{base.$prefix}-button-shape, #{f.density(get-size("l", round-radius), 16px, 2px)});
}

&.#{$component}--xl {
border-radius: var(--#{base.$prefix}-button-shape, #{f.density(get-size("xl", round-radius), 16px, 2px)});
}
}

&--square {
border-radius: var(--#{base.$prefix}-button-shape, get-size("s", square-radius));

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

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

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

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

&:active {
&.#{$component}--round {
border-radius: var(
--#{base.$prefix}-button-shape-pressed,
var(--#{base.$prefix}-icon-button-shape-pressed, get-size("s", pressed-radius))
);

&.#{$component}--xs {
border-radius: var(
--#{base.$prefix}-button-shape-pressed,
var(--#{base.$prefix}-icon-button-shape-pressed, get-size("xs", pressed-radius))
);
}

&.#{$component}--m {
border-radius: var(
--#{base.$prefix}-button-shape-pressed,
var(--#{base.$prefix}-icon-button-shape-pressed, get-size("m", pressed-radius))
);
}

&.#{$component}--l {
border-radius: var(
--#{base.$prefix}-button-shape-pressed,
var(--#{base.$prefix}-icon-button-shape-pressed, get-size("l", pressed-radius))
);
}

&.#{$component}--xl {
border-radius: var(
--#{base.$prefix}-button-shape-pressed,
var(--#{base.$prefix}-icon-button-shape-pressed, get-size("xl", pressed-radius))
);
}
}

&.#{$component}--square {
border-radius: var(--#{base.$prefix}-button-shape-pressed, get-size("s", pressed-radius));

&.#{$component}--xs {
border-radius: var(--#{base.$prefix}-button-shape-pressed, get-size("xs", pressed-radius));
}

&.#{$component}--m {
border-radius: var(--#{base.$prefix}-button-shape-pressed, get-size("m", pressed-radius));
}

&.#{$component}--l {
border-radius: var(--#{base.$prefix}-button-shape-pressed, get-size("l", pressed-radius));
}

&.#{$component}--xl {
border-radius: var(--#{base.$prefix}-button-shape-pressed, get-size("xl", pressed-radius));
}
}
}

&--narrow {
width: f.density-visible(get-width("s", narrow), 32px);

&.#{$component}--xs {
width: f.density-visible(get-width("xs", narrow), 32px);
}

&.#{$component}--m {
width: f.density-visible(get-width("m", narrow), 32px);
}

&.#{$component}--l {
width: f.density-visible(get-width("l", narrow), 32px);
}

&.#{$component}--xl {
width: f.density-visible(get-width("xl", narrow), 32px);
}
}

&--wide {
width: f.density-visible(get-width("s", wide), 32px);

&.#{$component}--xs {
width: f.density-visible(get-width("xs", wide), 32px);
}

&.#{$component}--m {
width: f.density-visible(get-width("m", wide), 32px);
}

&.#{$component}--l {
width: f.density-visible(get-width("l", wide), 32px);
}

&.#{$component}--xl {
width: f.density-visible(get-width("xl", wide), 32px);
}
}

&--standard {
background-color: transparent;
color: var(--#{base.$prefix}-icon-button-standard-color, #{t.color("on-surface-variant")});

&.#{$component}--selected {
background-color: var(--#{base.$prefix}-icon-button-selected-container, transparent);
color: var(--#{base.$prefix}-icon-button-selected-color, #{t.color("primary")});
}
}

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

&.#{$component}--toggle:not(.#{$component}--selected) {
background-color: t.color("surface-container");
color: t.color("on-surface-variant");
}
}

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

&.#{$component}--selected {
background-color: var(--#{base.$prefix}-icon-button-selected-container, #{t.color("secondary")});
color: var(--#{base.$prefix}-icon-button-selected-color, #{t.color("on-secondary")});
}
}

&--outlined {
background-color: transparent;
color: t.color("on-surface-variant");
border: get-size("s", outline-width) solid t.color("outline-variant");

@each $size, $config in $sizes {
&.#{$component}--#{$size} {
border-width: map.get($config, outline-width);
}
}

&.#{$component}--selected {
background-color: t.color("inverse-surface");
color: t.color("inverse-on-surface");
border-color: transparent;
}
}

&--toggle {
}

&--selected {
&.#{$component}--round:not(:active) {
border-radius: var(
--#{base.$prefix}-button-shape-selected,
var(--#{base.$prefix}-icon-button-shape-selected, get-size("s", square-radius))
);

&.#{$component}--xs {
border-radius: var(
--#{base.$prefix}-button-shape-selected,
var(--#{base.$prefix}-icon-button-shape-selected, get-size("xs", square-radius))
);
}

&.#{$component}--m {
border-radius: var(
--#{base.$prefix}-button-shape-selected,
var(--#{base.$prefix}-icon-button-shape-selected, get-size("m", square-radius))
);
}

&.#{$component}--l {
border-radius: var(
--#{base.$prefix}-button-shape-selected,
var(--#{base.$prefix}-icon-button-shape-selected, get-size("l", square-radius))
);
}

&.#{$component}--xl {
border-radius: var(
--#{base.$prefix}-button-shape-selected,
var(--#{base.$prefix}-icon-button-shape-selected, get-size("xl", square-radius))
);
}
}

&.#{$component}--square:not(:active) {
border-radius: var(--#{base.$prefix}-button-shape-selected, #{f.density(get-size("s", round-radius), 16px, 2px)});

&.#{$component}--xs {
border-radius: var(--#{base.$prefix}-button-shape-selected, #{f.density(get-size("xs", round-radius), 16px, 2px)});
}

&.#{$component}--m {
border-radius: var(--#{base.$prefix}-button-shape-selected, #{f.density(get-size("m", round-radius), 16px, 2px)});
}

&.#{$component}--l {
border-radius: var(--#{base.$prefix}-button-shape-selected, #{f.density(get-size("l", round-radius), 16px, 2px)});
}

&.#{$component}--xl {
border-radius: var(--#{base.$prefix}-button-shape-selected, #{f.density(get-size("xl", round-radius), 16px, 2px)});
}
}
}

&:disabled,
&.#{$component}--disabled {
pointer-events: none;
cursor: default;

&.#{$component}--standard,
&.#{$component}--filled,
&.#{$component}--tonal,
&.#{$component}--outlined {
color: t.alpha("on-surface", 0.38);
}

&.#{$component}--filled,
&.#{$component}--tonal {
background-color: t.alpha("on-surface", 0.1);
}

&.#{$component}--outlined {
border-color: t.color("outline-variant");

&.#{$component}--selected {
background-color: t.alpha("on-surface", 0.1);
border-color: transparent;
}
}

&::before {
display: none;
}
}
}
