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

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

// 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);
  $on-surface: if($is-dark, #fff, #000);

  @return (
    // MDC has a token for the enabled placeholder, but not for the disabled one.
    disabled-input-text-placeholder-color: rgba($on-surface, 0.38),
  );
}

// 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 (
    // MDC uses `subtitle1` for the input value, placeholder and floating label. The spec
    // shows `body1` for text fields though, so we manually override the typography.
    // Note: Form controls inherit the typography from the parent form field.
    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),

    // In the container styles, we updated the floating label to use the `body-1` typography level.
    // The MDC notched outline overrides this accidentally (only when the label floats) to a
    // `rem`-based value. This results in different label widths when floated/docked and ultimately
    // breaks the notch width as it has been measured in the docked state (where `body-1` is
    // applied). We try to unset these styles set by the `mdc-notched-outline`:
    // https://github.com/material-components/material-components-web/blob/master/packages/mdc-notched-outline/_mixins.scss#L272-L292.
    // This is why we can't use their `label-text-populated-size` token and we have to declare
    // our own version of it.
    outlined-label-text-populated-size: typography-utils.font-size($config, body-1),

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

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