@use 'sass:map';
@use 'sass:math';
@use 'sass:string';
@use 'base' as *;
@use 'emit' as emit;

$spring-fast-spatial-points: (0.0, 0.102, 0.323, 0.57, 0.784, 0.942, 1.039, 1.085, 1.094, 1.082, 1.06, 1.037, 1.018, 1.004, 0.995, 0.992, 0.991, 0.992, 0.995, 0.997, 0.999, 1.0, 1.001, 1.001, 1);

@function spring-linear($points, $clamp: false) {
$text: '';
@each $point in $points {
$value: if($clamp, math.min($point, 1), $point);
$text: if($text == '', '#{$value}', '#{$text}, #{$value}');
}
@return string.unquote('linear(#{$text})');
}

$state: (
'hover-state-layer-opacity': 0.08,
'focus-state-layer-opacity': 0.1,
'pressed-state-layer-opacity': 0.1,
'dragged-state-layer-opacity': 0.16
) !default;

$motion: (
'duration-short1': 50ms,
'duration-short2': 100ms,
'duration-short3': 150ms,
'duration-short4': 200ms,
'duration-medium1': 250ms,
'duration-medium2': 300ms,
'duration-medium3': 350ms,
'duration-medium4': 400ms,
'duration-long1': 450ms,
'duration-long2': 500ms,
'duration-long3': 550ms,
'duration-long4': 600ms,
'duration-extra-long1': 700ms,
'duration-extra-long2': 800ms,
'duration-extra-long3': 900ms,
'duration-extra-long4': 1000ms,

'easing-standard': cubic-bezier(0.2, 0.0, 0.0, 1.0),
'easing-standard-accelerate': cubic-bezier(0.3, 0.0, 1.0, 1.0),
'easing-standard-decelerate': cubic-bezier(0.0, 0.0, 0.2, 1.0),

'easing-emphasized': cubic-bezier(0.3, 0.0, 0.0, 1.0),
'easing-emphasized-accelerate': cubic-bezier(0.3, 0.0, 0.8, 0.15),
'easing-emphasized-decelerate': cubic-bezier(0.05, 0.7, 0.1, 1.0),

'easing-accelerated': cubic-bezier(0.3, 0.0, 1.0, 1.0),
'easing-decelerated': cubic-bezier(0.0, 0.0, 0.2, 1.0),
'easing-exit': cubic-bezier(0.3, 0.0, 1.0, 1.0),
'easing-enter': cubic-bezier(0.0, 0.0, 0.2, 1.0),

'easing-appear': cubic-bezier(0.0, 0.0, 0.2, 1.0),
'easing-disappear': cubic-bezier(0.3, 0.0, 1.0, 1.0),
'easing-expand': cubic-bezier(0.0, 0.0, 0.2, 1.0),
'easing-collapse': cubic-bezier(0.3, 0.0, 1.0, 1.0),

'spring-default-spatial-duration': 450ms,
'spring-default-spatial-easing': linear(0.0, 0.055, 0.18, 0.332, 0.483, 0.619, 0.733, 0.823, 0.891, 0.94, 0.973, 0.994, 1.007, 1.013, 1.015, 1.015, 1.013, 1.011, 1.009, 1.006, 1.005, 1.003, 1.002, 1.001, 1),
'spring-fast-spatial-duration': 425ms,
'spring-fast-spatial-easing': spring-linear($spring-fast-spatial-points),
'spring-fast-spatial-clamped-easing': spring-linear($spring-fast-spatial-points, $clamp: true),
'spring-slow-spatial-duration': 625ms,
'spring-slow-spatial-easing': linear(0.0, 0.056, 0.182, 0.335, 0.488, 0.624, 0.738, 0.827, 0.895, 0.943, 0.975, 0.996, 1.007, 1.013, 1.015, 1.015, 1.013, 1.011, 1.008, 1.006, 1.004, 1.003, 1.002, 1.001, 1),
'spring-default-effects-duration': 250ms,
'spring-default-effects-easing': linear(0.0, 0.066, 0.203, 0.355, 0.496, 0.616, 0.713, 0.788, 0.845, 0.888, 0.92, 0.943, 0.96, 0.971, 0.98, 0.986, 0.99, 0.993, 0.995, 0.997, 0.998, 0.998, 0.999, 0.999, 1),
'spring-fast-effects-duration': 175ms,
'spring-fast-effects-easing': linear(0.0, 0.075, 0.227, 0.39, 0.537, 0.657, 0.751, 0.822, 0.874, 0.912, 0.939, 0.958, 0.971, 0.98, 0.987, 0.991, 0.994, 0.996, 0.997, 0.998, 0.999, 0.999, 0.999, 1.0, 1),
'spring-slow-effects-duration': 350ms,
'spring-slow-effects-easing': linear(0.0, 0.065, 0.2, 0.351, 0.491, 0.611, 0.708, 0.783, 0.841, 0.885, 0.917, 0.941, 0.958, 0.97, 0.979, 0.985, 0.99, 0.993, 0.995, 0.997, 0.998, 0.998, 0.999, 0.999, 1),
) !default;

$elevation: (
'level-0': none,
'level-1': (0px 1px 2px rgba(0, 0, 0, 0.3), 0px 1px 3px 1px rgba(0, 0, 0, 0.15)),
'level-2': (0px 1px 2px rgba(0, 0, 0, 0.3), 0px 2px 6px 2px rgba(0, 0, 0, 0.15)),
'level-3': (0px 1px 3px rgba(0, 0, 0, 0.3), 0px 4px 8px 3px rgba(0, 0, 0, 0.15)),
'level-4': (0px 2px 3px rgba(0, 0, 0, 0.3), 0px 6px 10px 4px rgba(0, 0, 0, 0.15)),
'level-5': (0px 4px 4px rgba(0, 0, 0, 0.3), 0px 8px 12px 6px rgba(0, 0, 0, 0.15))
) !default;

$typescale: (
'display-large': (
'font-family': ('Roboto', sans-serif),
'font-size': 57px,
'line-height': 64px,
'letter-spacing': -0.25px,
'font-weight': 400
),
'display-medium': (
'font-family': ('Roboto', sans-serif),
'font-size': 45px,
'line-height': 52px,
'letter-spacing': 0,
'font-weight': 400
),
'display-small': (
'font-family': ('Roboto', sans-serif),
'font-size': 36px,
'line-height': 44px,
'letter-spacing': 0,
'font-weight': 400
),
'headline-large': (
'font-family': ('Roboto', sans-serif),
'font-size': 32px,
'line-height': 40px,
'letter-spacing': 0,
'font-weight': 400
),
'headline-medium': (
'font-family': ('Roboto', sans-serif),
'font-size': 28px,
'line-height': 36px,
'letter-spacing': 0,
'font-weight': 400
),
'headline-small': (
'font-family': ('Roboto', sans-serif),
'font-size': 24px,
'line-height': 32px,
'letter-spacing': 0,
'font-weight': 400
),
'title-large': (
'font-family': ('Roboto', sans-serif),
'font-size': 22px,
'line-height': 28px,
'letter-spacing': 0,
'font-weight': 400
),
'title-medium': (
'font-family': ('Roboto', sans-serif),
'font-size': 16px,
'line-height': 24px,
'letter-spacing': 0.15px,
'font-weight': 500
),
'title-small': (
'font-family': ('Roboto', sans-serif),
'font-size': 14px,
'line-height': 20px,
'letter-spacing': 0.1px,
'font-weight': 500
),
'body-large': (
'font-family': ('Roboto', sans-serif),
'font-size': 16px,
'line-height': 24px,
'letter-spacing': 0.5px,
'font-weight': 400
),
'body-medium': (
'font-family': ('Roboto', sans-serif),
'font-size': 14px,
'line-height': 20px,
'letter-spacing': 0.25px,
'font-weight': 400
),
'body-small': (
'font-family': ('Roboto', sans-serif),
'font-size': 12px,
'line-height': 16px,
'letter-spacing': 0.4px,
'font-weight': 400
),
'label-large': (
'font-family': ('Roboto', sans-serif),
'font-size': 14px,
'line-height': 20px,
'letter-spacing': 0.1px,
'font-weight': 500
),
'label-medium': (
'font-family': ('Roboto', sans-serif),
'font-size': 12px,
'line-height': 16px,
'letter-spacing': 0.5px,
'font-weight': 500
),
'label-small': (
'font-family': ('Roboto', sans-serif),
'font-size': 11px,
'line-height': 16px,
'letter-spacing': 0.5px,
'font-weight': 500
)
) !default;

$shape: (
'none': 0,
'extra-small': 4px,
'small': 8px,
'medium': 12px,
'large': 16px,
'large-increased': 20px,
'extra-large': 28px,
'extra-large-increased': 32px,
'extra-extra-large': 48px,
'full': 9999px
) !default;

$breakpoints: (
'xs': 0,
'sm': 600px,
'md': 960px,
'lg': 1280px,
'xl': 1920px
) !default;

$z-index: (
'modal': 1000,
'snackbar': 950,
'popover': 900,
'drawer': 800,
'dialog': 700,
'dropdown': 600,
'tooltip': 500,
'menu': 700,
'sticky': 100,
'fixed': 50,
'default': 1,
'below': -1
) !default;

$card: (
'width': 344px,
'width-small': 344px,
'width-medium': 480px,
'width-large': 624px,
'border-radius': 12px,
'padding': 16px
) !default;

$chip-config: (
'height': 32px,
'border-radius': 8px,
'padding-horizontal': 12px,
'outlined-border-opacity': 0.12,
'outlined-border-hover-opacity': 0.29,
'outlined-border-focus-opacity': 0.38,
'outlined-selected-border-opacity': 0,
'icon-size': 18px
) !default;

$button: (
'height': 40px,
'min-width': 58px,
'padding-horizontal': 16px,
'padding-horizontal-small': 12px,
'padding-icon': 12px,
'transition-duration': map.get($motion, 'duration-short2'),
'transition-easing': map.get($motion, 'easing-standard'),
'height-xs': 32px,
'height-s': 40px,
'height-m': 56px,
'height-l': 96px,
'height-xl': 136px,
'padding-xs': 12px,
'padding-s': 16px,
'padding-m': 24px,
'padding-l': 48px,
'padding-xl': 64px,
'icon-size-xs': 20px,
'icon-size-s': 20px,
'icon-size-m': 24px,
'icon-size-l': 32px,
'icon-size-xl': 40px,
'icon-gap-xs': 4px,
'icon-gap-s': 8px,
'icon-gap-m': 8px,
'icon-gap-l': 12px,
'icon-gap-xl': 16px,
'outline-width-xs': 1px,
'outline-width-s': 1px,
'outline-width-m': 1px,
'outline-width-l': 2px,
'outline-width-xl': 3px,
'shape-square-xs': map.get($shape, 'medium'),
'shape-square-s': map.get($shape, 'medium'),
'shape-square-m': map.get($shape, 'large'),
'shape-square-l': map.get($shape, 'extra-large'),
'shape-square-xl': map.get($shape, 'extra-large'),
'shape-round-xs': 16px,
'shape-round-s': 20px,
'shape-round-m': 28px,
'shape-round-l': 48px,
'shape-round-xl': 68px,
'shape-pressed-xs': map.get($shape, 'small'),
'shape-pressed-s': map.get($shape, 'small'),
'shape-pressed-m': map.get($shape, 'medium'),
'shape-pressed-l': map.get($shape, 'large'),
'shape-pressed-xl': map.get($shape, 'large'),
'touch-target': 48px
) !default;


$spacing-scale: (
'0': 0,
'1': 4px,
'2': 8px,
'3': 12px,
'4': 16px,
'5': 20px,
'6': 24px,
'8': 32px,
'10': 40px,
'12': 48px,
'14': 56px,
'16': 64px,
'20': 80px,
'24': 96px,
'32': 128px
);

@function spacing($key) {
@if not map.has-key($spacing-scale, $key) {
@error 'Spacing key "#{$key}" not found in spacing scale.';
}
@return map.get($spacing-scale, $key);
}

@function state($key) {
@return map.get($state, $key);
}

@function _spring-var($key) {
@return string.unquote('var(--#{$prefix}-sys-motion-#{$key}, #{map.get($motion, $key)})');
}

@function motion($key) {
$value: map.get($motion, $key);
@if emit.$spring-tokens and $value != null and string.index($key, 'spring-') == 1 {
@return _spring-var($key);
}
@return $value;
}

@function spring-duration-div($key, $by) {
$value: map.get($motion, $key);
@if not emit.$spring-tokens {
@return math.div($value, $by);
}
@if $value == null {
@error 'No motion token "#{$key}".';
}
@return string.unquote('calc(#{_spring-var($key)} / #{$by})');
}

@function spring-delay-scale($key, $amount) {
@if not emit.$spring-tokens {
@return $amount;
}
$basis: map.get($motion, $key);
@if $basis == null {
@error 'No motion token "#{$key}".';
}
@return string.unquote('calc(#{_spring-var($key)} * (#{math.div($amount, 1ms)} / #{math.div($basis, 1ms)}))');
}

@function elevation($level) {
@return map.get($elevation, $level);
}

@function typography($scale) {
@return map.get($typescale, $scale);
}

@function shape($size) {
@if not map.has-key($shape, $size) {
@error "No shape step '#{$size}'. M3's scale: #{map.keys($shape)}. "
+ "extra-tiny, tiny and pill were removed in material 3.0.0: write a literal radius, or use full.";
}
@return var(--#{$prefix}-sys-shape-corner-#{$size}, #{map.get($shape, $size)});
}

@function z-index($layer) {
@return map.get($z-index, $layer);
}

@function breakpoint($size) {
@return map.get($breakpoints, $size);
}

$button-shape-tokens: (
'shape-square-xs': 'medium', 'shape-square-s': 'medium', 'shape-square-m': 'large',
'shape-square-l': 'extra-large', 'shape-square-xl': 'extra-large',
'shape-pressed-xs': 'small', 'shape-pressed-s': 'small', 'shape-pressed-m': 'medium',
'shape-pressed-l': 'large', 'shape-pressed-xl': 'large'
);

@function button($prop) {
@if map.has-key($button-shape-tokens, $prop) {
@return shape(map.get($button-shape-tokens, $prop));
}
@return map.get($button, $prop);
}

@function card($key) {
@if $key == 'border-radius' { @return shape('medium'); }
@return map.get($card, $key);
}

@function chip($key, $fallback: null) {
@if $key == 'border-radius' { @return shape('small'); }
@if map.has-key($chip-config, $key) {
@return map.get($chip-config, $key);
}
@return $fallback;
}

$text-field: (
'width': 280px,
'height': 56px,
'height-small': 48px,
'height-large': 64px,
'height-multiline': 100px,
'padding-top-multiline': 16px,
'padding-top-multiline-label': 24px,
'padding-horizontal': 16px,
'padding-vertical': 16px,
'border-radius': map.get($shape, 'extra-small'),
'border-width': 1px,
'label-offset': 16px,
'icon-size': 24px
) !default;

@function text-field($key) {
@if $key == 'border-radius' { @return shape('extra-small'); }
@return map.get($text-field, $key);
}