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

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

// 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 (
    container-shape: 4px,
  );
}

// Tokens that can be configured through Angular Material's color theming API.
@function get-color-tokens($config) {
  $foreground: map.get($config, foreground);
  $background: map.get($config, background);

  @return (
    container-background-color: theming.get-color-from-palette($background, card),
    container-text-color: theming.get-color-from-palette($foreground, text),
    actions-divider-color: theming.get-color-from-palette($foreground, divider),
    header-hover-state-layer-color: theming.get-color-from-palette($background, hover),
    header-focus-state-layer-color: theming.get-color-from-palette($background, hover),
    header-disabled-state-text-color: theming.get-color-from-palette($foreground, disabled-button),
    header-text-color: theming.get-color-from-palette($foreground, text),
    header-description-color: theming.get-color-from-palette($foreground, secondary-text),
    header-indicator-color: theming.get-color-from-palette($foreground, secondary-text),
  );
}

// Tokens that can be configured through Angular Material's typography theming API.
@function get-typography-tokens($config) {
  $fallback-font-family: typography-utils.font-family($config);

  @return (
    header-text-font: typography-utils.font-family($config, subheading-1) or $fallback-font-family,
    header-text-size: typography-utils.font-size($config, subheading-1),
    header-text-weight: typography-utils.font-weight($config, subheading-1),

    // TODO(crisbeto): these two properties weren't set at all before the introduction of tokens,
    // but it's inconsistent not to provide them since the container sets all of them. Eventually
    // we should change the values to use come from `subheading-1`.
    header-text-line-height: inherit,
    header-text-tracking: inherit,

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

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($config) {
  $scale: theming.clamp-density($config, -3);
  $collapsed-scale: (
    0: 48px,
    -1: 44px,
    -2: 40px,
    -3: 36px,
  );
  $expanded-scale: (
    0: 64px,
    -1: 60px,
    -2: 56px,
    -3: 48px,
  );

  @return (
    header-collapsed-state-height: map.get($collapsed-scale, $scale),
    header-expanded-state-height: map.get($expanded-scale, $scale),
  );
}

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