@use 'sass:map';
@use 'sass:math';
@use '@angular/material' as mat;
@use '../core/tokens/m2-utils';
@use '../core/style/elevation';

// Generates the tokens used to theme the option based on a palette.
@function private-get-color-palette-color-tokens($theme, $palette-name) {
  @return (
    select-option-selected-state-text-color: mat.get-theme-color($theme, $palette-name),
  );
}

// Tokens that can't be configured through Angular Material's current theming API,
// but may be in a future version of the theming API.
@function get-unthemable-tokens() {
  @return (
    select-container-shape: 4px,
    select-container-elevation-shadow: elevation.get-box-shadow(8),
  );
}

// Tokens that can be configured through Angular Material's color theming API.
@function get-color-tokens($theme) {
  $text-color: mat.get-theme-color($theme, foreground, text);
  $hint-text-color: mat.get-theme-color($theme, foreground, hint-text);
  $secondary-text-color: mat.get-theme-color($theme, foreground, secondary-text);
  $disabled-text-color: mat.get-theme-color($theme, foreground, disabled-text);
  $divider-color: mat.get-theme-color($theme, foreground, divider);
  $hover-color: mat.get-theme-color($theme, background, hover);
  $palette-tokens: private-get-color-palette-color-tokens($theme, primary);

  @return map.merge($palette-tokens, (
    select-container-text-color: $text-color,
    select-placeholder-text-color: $hint-text-color,
    select-disabled-text-color: $hint-text-color,

    select-multiple-value-background-color: mat.get-theme-color($theme, background, unselected-chip),
    select-multiple-value-outline-color: $divider-color,
    select-multiple-value-disabled-outline-color: mat.get-theme-color($theme, foreground, divider, 0.06),
    select-multiple-value-icon-hover-background-color: $divider-color,

    select-clear-icon-color: $secondary-text-color,
    select-clear-icon-hover-color: mat.get-theme-color($theme, warn),

    select-enabled-arrow-color: $secondary-text-color,
    select-disabled-arrow-color: $disabled-text-color,
    select-invalid-arrow-color: mat.get-theme-color($theme, warn),

    select-panel-background-color: mat.get-theme-color($theme, background, card),
    select-panel-divider-color: $divider-color,
    select-optgroup-label-text-color: $secondary-text-color,
    select-option-label-text-color: $text-color,
    select-option-selected-state-background-color: $hover-color,
    select-option-hover-state-background-color: $hover-color,
    select-option-disabled-state-text-color: $hint-text-color,
  ));
}

// Tokens that can be configured through Angular Material's typography theming API.
@function get-typography-tokens($theme) {
  @return ();
}

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($theme) {
  @return ();
}

// Combines the tokens generated by the above functions into a single map with placeholder values.
// This is used to create token slots.
@function get-token-slots() {
  @return mat.private-deep-merge-all(
    get-unthemable-tokens(),
    get-color-tokens(m2-utils.$placeholder-color-config),
    get-typography-tokens(m2-utils.$placeholder-typography-config),
    get-density-tokens(m2-utils.$placeholder-density-config)
  );
}
