@use 'sass:color';
@use 'sass:map';
@use 'sass:meta';
@use '@angular/material' as mat;
@use '../../token-definition';
@use '../../../style/elevation';

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

$_selected-fade-amount: .6;
$_today-fade-amount: .2;

@function private-get-calendar-color-palette-color-tokens($theme, $palette-name) {
  $palette-color: mat.get-theme-color($theme, $palette-name);
  $default-contrast: mat.get-theme-color($theme, $palette-name, default-contrast);
  $active-background-color: mat.private-safe-color-change($palette-color, $alpha: .3);
  $active-disabled-color: null;

  @if (meta.type-of($palette-color) == color) {
    $active-disabled-color: color.adjust($palette-color, $alpha: -$_selected-fade-amount);
  }
  @else {
    $active-disabled-color: mat.get-theme-color($theme, foreground, disabled-button);
  }

  @return (
    calendar-header-background-color: $palette-color,

    calendar-date-selected-state-text-color: $default-contrast,
    calendar-date-selected-state-background-color: $palette-color,
    calendar-date-selected-disabled-state-background-color: $active-disabled-color,
    calendar-date-today-selected-state-outline-color: $default-contrast,
    calendar-date-focus-state-background-color: $active-background-color,
    calendar-date-hover-state-background-color: $active-background-color,

    clock-hand-background-color: $palette-color,
    clock-cell-selected-state-background-color: $palette-color,

    time-input-active-state-text-color: $palette-color,
    time-input-active-state-background-color: mat.private-safe-color-change($palette-color, $alpha: .2),
    time-input-focus-state-outline-color: $palette-color,
    time-input-focus-state-placeholder-text-color: mat.private-safe-color-change($palette-color, $alpha: .6),

    time-ampm-selected-state-background-color: mat.private-safe-color-change($palette-color, $alpha: .2),
  );
}

@function private-get-toggle-color-palette-color-tokens($theme, $palette-name) {
  @return (
    toggle-active-state-icon-color: mat.get-theme-color($theme, $palette-name, text),
  );
}

// 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-elevation-shadow: elevation.get-box-shadow(4),
    container-touch-elevation-shadow: elevation.get-box-shadow(24),
    container-shape: 4px,
    container-touch-shape: 4px,
    selector-container-shape: 4px,
  );
}

// Tokens that can be configured through Angular Material's color theming API.
@function get-color-tokens($theme) {
  $inactive-icon-color: mat.get-theme-color($theme, foreground, icon);
  $text-color: mat.get-theme-color($theme, foreground, 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);
  $hint-text-color: mat.get-theme-color($theme, foreground, hint-text);
  $preview-outline-color: $divider-color;
  $today-disabled-outline-color: null;

  $primary-color: mat.get-theme-color($theme, primary);
  $calendar-tokens: private-get-calendar-color-palette-color-tokens($theme, primary);
  $toggle-tokens: private-get-toggle-color-palette-color-tokens($theme, primary);

  @return mat.private-merge-all($calendar-tokens, $toggle-tokens, (
    toggle-icon-color: $inactive-icon-color,
    calendar-body-label-text-color: $secondary-text-color,
    // calendar-period-button-icon-color: $inactive-icon-color,
    // calendar-navigation-button-icon-color: $inactive-icon-color,
    calendar-header-text-color: white,
    calendar-header-divider-color: transparent,
    calendar-table-header-text-color: $secondary-text-color,

    // Note: though it's not text, the border is a hint about the fact
    // that this is today's date, so we use the hint color.
    calendar-date-today-outline-color: $hint-text-color,
    // calendar-date-today-disabled-state-outline-color: $today-disabled-outline-color,
    calendar-date-text-color: $text-color,
    calendar-date-outline-color: transparent,
    calendar-date-disabled-state-text-color: $disabled-text-color,
    // calendar-date-preview-state-outline-color: $preview-outline-color,

    container-background-color: mat.get-theme-color($theme, background, card),
    container-text-color: $text-color,

    clock-dial-background-color: $divider-color,
    clock-cell-text-color: $text-color,
    clock-cell-hover-state-background-color: mat.get-theme-color($theme, background, hover),
    clock-cell-disabled-state-text-color: $disabled-text-color,

    time-input-text-color: $text-color,
    time-input-background-color: $divider-color,
    time-input-focus-state-background-color: mat.get-theme-color($theme, background, background),
    time-input-warn-state-outline-color: mat.get-theme-color($theme, warn),
    time-ampm-text-color: mat.private-safe-color-change($text-color, $alpha: .75),
    time-ampm-outline-color: $hint-text-color,
    time-ampm-selected-state-text-color: $text-color,
  ));
}

// Tokens that can be configured through Angular Material's typography theming API.
@function get-typography-tokens($theme) {
  @return (
    calendar-text-font: mat.get-theme-typography($theme, body-1, font-family),
    calendar-text-size: 13px,

    calendar-body-label-text-size: mat.get-theme-typography($theme, button, font-size),
    calendar-body-label-text-weight: mat.get-theme-typography($theme, button, font-weight),

    calendar-period-button-text-size: mat.get-theme-typography($theme, button, font-size),
    calendar-period-button-text-weight: mat.get-theme-typography($theme, button, font-weight),

    calendar-table-header-text-size: 11px,
    calendar-table-header-text-weight: mat.get-theme-typography($theme, body-1, font-weight),

    clock-text-size: 14px,
  );
}

// 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(token-definition.$placeholder-color-config),
    get-typography-tokens(token-definition.$placeholder-typography-config),
    get-density-tokens(token-definition.$placeholder-density-config)
  );
}
