@use 'sass:list';
@use 'sass:map';
@use '../abstract/functions' as f;
@use '../abstract/variables' as v;
@use '../abstract/mixins' as m;
@use '../abstract/emit';
@use '../abstract/base';
@use 'config' as c;

$sizes: (
'xs': (32px, 18px, 13px, 'label-large', 20px),
's': (40px, 12px, 17px, 'label-large', 24px),
'm': (56px, 8px, 25px, 'title-medium', 24px),
'l': (96px, 8px, 50px, 'headline-small', 32px),
'xl': (136px, 8px, 67px, 'headline-large', 40px),
);
$densities: ('comfortable': 4px, 'compact': 8px);
$icon-first: ':has(> [icon]:empty:first-child)';

@mixin item($size, $font: true, $letter: true, $weight: true) {
$t: map.get($sizes, $size);
$type: map.get(v.$typescale, list.nth($t, 4));
$role: list.nth($t, 4);
height: f.density(list.nth($t, 1), 32px);
padding: 0 list.nth($t, 3);
@if $font {
@if emit.$typescale-tokens {
font-family: m.typography-value($role, 'font', var(--#{base.$prefix}-ref-typeface-#{f.typeface($role)}, #{map.get($type, 'font-family')}));
}
font-size: m.typography-value($role, 'font-size', map.get($type, 'font-size'));
line-height: m.typography-value($role, 'line-height', map.get($type, 'line-height'));
@if $letter { letter-spacing: m.typography-value($role, 'letter-spacing', map.get($type, 'letter-spacing')); }
}
@if $weight or emit.$typescale-tokens { font-weight: m.typography-value($role, 'font-weight', map.get($type, 'font-weight')); }
}

@include c.host('button-group') {
display: inline-flex;
vertical-align: baseline;
gap: 12px;
white-space: nowrap;

&::before {
width: 0;
height: 32px;
margin-inline-end: -12px;
align-self: flex-start;
margin-block-start: max(-4px, 2px * var(--mtrl-density, 0));
}

&#{$icon-first} {
align-items: center;

&::before { content: ""; }
}

&[size='xs'] { gap: 18px; }
&:is([size='m'], [size='l'], [size='xl']) { gap: 8px; }

@each $density, $step in $densities {
&[density='#{$density}'] > * { height: max(32px, 40px - $step); }
}

@each $size in ('xs', 'm', 'l', 'xl') {
&[size='#{$size}'] {
$t: map.get($sizes, $size);

> * {
@if $size == 'xs' { @include item($size, $font: false, $weight: false); }
@else if $size == 'm' { @include item($size, $weight: false); }
@else { @include item($size, $letter: false, $weight: false); }
}

@each $density, $step in $densities {
&[density='#{$density}'] > * { height: max(32px, list.nth($t, 1) - $step); }
}

&::before {
height: list.nth($t, 1) * 0.5 + list.nth($t, 5) * 0.5;
margin-block-start: max(#{16px - list.nth($t, 1) * 0.5}, 2px * var(--mtrl-density, 0));
@if $size == 'xs' { margin-inline-end: -1 * list.nth($t, 2); }
}
}
}

&:is([size='m'], [size='l'], [size='xl'])::before { margin-inline-end: -8px; }
@if emit.$typescale-tokens {
@each $size, $role in ('l': 'headline-small', 'xl': 'headline-large') {
&[size='#{$size}'] > * {
letter-spacing: m.typography-value($role, 'letter-spacing', 0);
font-weight: m.typography-value($role, 'font-weight', 400);
}
}
} @else {
&:is([size='l'], [size='xl']) > * { letter-spacing: 0; font-weight: 400; }
}

@each $density, $step in $densities {
&[density='#{$density}']::before { margin-block-start: min($step, 40px - 32px) * -0.5; }
}

@each $size in ('xs', 'm', 'l', 'xl') {
$t: map.get($sizes, $size);

@each $density, $step in $densities {
&[size='#{$size}'][density='#{$density}']::before {
margin-block-start: min($step, list.nth($t, 1) - 32px) * -0.5;
}
}
}
}

@include c.declarations('button-group') {
display: inline-flex;
align-items: center;
flex: none;
min-width: 58px;
font-family: var(--mtrl-ref-typeface-plain,Roboto,sans-serif);
@include item('s');

&[icon]:empty { min-width: 0; padding: 0; aspect-ratio: 1; }
}

@include c.host('button-group') {
&[kind='connected'] {
gap: 2px;

&::before { margin-inline-end: -2px; }
}

&:is(:not([size]), [size='xs'], [size='s'])[kind='connected'] > * { min-width: 48px; }
}
