@use 'sass:map';
@use '../../token-utils';
@use '../../../theming/theming';
@use '../../../style/sass-utils';
@use '../../../typography/typography-utils';
@use '../../../mdc-helpers/mdc-helpers';

// The prefix used to generate the fully qualified name for tokens in this file.
$prefix: (mat, option);

// 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 ();
}

// Tokens that can be configured through Angular Material's color theming API.
@function get-color-tokens($config) {
  $is-dark: map.get($config, is-dark);
  $foreground: map.get($config, foreground);
  $primary: map.get($config, primary);
  $on-surface: if($is-dark, #fff, #000);
  $active-state-layer-color: rgba($on-surface, if($is-dark, 0.08, 0.04));
  $palette-tokens: private-get-color-palette-color-tokens($primary);

  @return map.merge($palette-tokens, (
    label-text-color: theming.get-color-from-palette($foreground, text),
    hover-state-layer-color: $active-state-layer-color,
    focus-state-layer-color: $active-state-layer-color,
    selected-state-layer-color: $active-state-layer-color,
  ));
}

// Tokens that can be configured through Angular Material's typography theming API.
@function get-typography-tokens($config) {
  // TODO(crisbeto): The earlier implementation of the option used MDC's APIs to create the
  // typography tokens. As a result, we unintentionally allowed `null` typography configs to be
  // passed in. Since there a lot of apps that now depend on this pattern, we need this temporary
  // fallback.
  @if ($config == null) {
    $config: mdc-helpers.private-fallback-typography-from-mdc();
  }

  @return (
    label-text-font: typography-utils.font-family($config, body-1) or
      typography-utils.font-family($config),
    label-text-line-height: typography-utils.line-height($config, body-1),
    label-text-size: typography-utils.font-size($config, body-1),
    label-text-tracking: typography-utils.letter-spacing($config, body-1),
    label-text-weight: typography-utils.font-weight($config, body-1)
  );
}

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

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($config) {
  @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 sass-utils.deep-merge-all(
      get-unthemable-tokens(),
      get-color-tokens(token-utils.$placeholder-color-config),
      get-typography-tokens(token-utils.$placeholder-typography-config),
      get-density-tokens(token-utils.$placeholder-density-config)
  );
}
