/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
@use 'sass:list';
@use 'sass:map';
@use '@material/theme/theme' as mdc-theme;
@use '@material/theme/theme-color' as mdc-theme-color;
@use '@material/typography/typography' as mdc-typography;
@use '@material/feature-targeting/feature-targeting' as mdc-feature-targeting;
@use '../theme';
@use './variables';
@use '../theme/custom-properties';

@mixin core-styles() {
  .forge-typography {
    @include mdc-typography.base;
    @include theme.css-custom-property(color, --mdc-theme-text-primary-on-background, mdc-theme-color.prop-value(text-primary-on-background));
  }

  // Create custom classes for built-in MDC typography styles
  @each $style in map.keys(mdc-typography.$styles) {
    .forge-typography--#{$style} {
      @include mdc-typography.typography($style);
      @include apply-additional-styles(variables.$mdc-typography-additional-styles, $style);
    }
  }
  
  // Create classes for forge-specific typography styles
  @each $style in map.keys(variables.$styles) {
    .forge-typography--#{$style} {
      @include typography($style);
    }
  }
  
  :root {
    // forge-specific MDC typography overrides
    @each $style, $properties in variables.$mdc-typography-overrides {
      @each $property in map.keys($properties) {
        --mdc-typography-#{$style}-#{$property}: #{map.get($properties, $property)};
      }
    }
  }
}

@mixin apply-additional-styles($styles, $style) {
  @if map.has-key($styles, $style) {
    $additional-styles: map.get($styles, $style);
    @each $additional-style in map.keys($additional-styles) {
      #{$additional-style}: #{map.get($additional-styles, $additional-style)};
    }
  }
}

/// Generates all styles for a specific typography style.
@mixin typography($style, $query: mdc-feature-targeting.all(), $exclude-props: ()) {
  // We first check if the style exists in the MDC typography styles map and pass to MDC if so,
  // otherwise we handle it and assumed it's a Forge-specfic style
  @if map.has-key(mdc-typography.$styles, $style) {
    @include mdc-typography.typography($style);
  } @else {
    $feat-typography: mdc-feature-targeting.create-target($query, typography);
    $style-props: map.get(variables.$styles, $style);
  
    @if not map.has-key(variables.$styles, $style) {
      @error "Invalid style specified! #{$style} doesn't exist. Choose one of #{map-keys(variables.$styles)}";
    }
  
    @include mdc-typography.smooth-font($query: $query);
    @include mdc-feature-targeting.targets($feat-typography) {
      @each $key, $value in $style-props {
        @if list.index($exclude-props, $key) == null {
          $fallback: $value;
          @if $key == 'font-family' {
            // Each style has its own font-family custom property, but should
            // fallback to a generic font-family property. This allows
            // customizing the font-family for all styles or only specific ones.
            $fallback: (
              varname: --forge-typography-font-family,
              fallback: $fallback
            );
          }
  
          @include theme.property($key, (
            varname: --forge-typography-#{$style}-#{$key},
            fallback: $fallback
          ));
        }
      }
    }
  }
}
