@use 'sass:map';
@use '@material/theme/theme-color' as mdc-theme-color;
@use '@material/data-table/data-table' as mdc-data-table;
@use '@material/data-table' as mdc-data-table-theme;
@use '../core/theming/theming';
@use '../core/mdc-helpers/mdc-helpers';
@use '../core/typography/typography';

@mixin color($config-or-theme) {
  $config: theming.get-color-config($config-or-theme);
  // Save original values of MDC global variables. We need to save these so we can restore the
  // variables to their original values and prevent unintended side effects from using this mixin.
  $orig-selected-row-fill-color: mdc-data-table-theme.$selected-row-fill-color;
  $orig-divider-color: mdc-data-table-theme.$divider-color;
  $orig-row-hover-fill-color: mdc-data-table-theme.$row-hover-fill-color;
  $orig-header-row-text-color: mdc-data-table-theme.$header-row-text-color;
  $orig-row-text-color: mdc-data-table-theme.$row-text-color;
  $orig-stroke-color: mdc-data-table-theme.$stroke-color;

  @include mdc-helpers.using-mdc-theme($config) {
    mdc-data-table-theme.$selected-row-fill-color: rgba(mdc-theme-color.prop-value(primary), 0.04);
    mdc-data-table-theme.$divider-color: rgba(mdc-theme-color.prop-value(on-surface), 0.12);
    mdc-data-table-theme.$row-hover-fill-color: rgba(mdc-theme-color.prop-value(on-surface), 0.04);
    mdc-data-table-theme.$header-row-text-color: rgba(mdc-theme-color.prop-value(on-surface), 0.87);
    mdc-data-table-theme.$row-text-color: rgba(mdc-theme-color.prop-value(on-surface), 0.87);
    mdc-data-table-theme.$stroke-color: rgba(mdc-theme-color.prop-value(on-surface), 0.12);

    @include mdc-data-table.table-styles($query: mdc-helpers.$mdc-theme-styles-query);
  }

  // Restore original values of MDC global variables.
  mdc-data-table-theme.$selected-row-fill-color: $orig-selected-row-fill-color;
  mdc-data-table-theme.$divider-color: $orig-divider-color;
  mdc-data-table-theme.$row-hover-fill-color: $orig-row-hover-fill-color;
  mdc-data-table-theme.$header-row-text-color: $orig-header-row-text-color;
  mdc-data-table-theme.$row-text-color: $orig-row-text-color;
  mdc-data-table-theme.$stroke-color: $orig-stroke-color;

  .mat-mdc-table {
    $background: map.get($config, background);
    background: theming.get-color-from-palette($background, 'card');
  }
}

@mixin typography($config-or-theme) {
  $config: typography.private-typography-to-2018-config(
      theming.get-typography-config($config-or-theme));
  @include mdc-helpers.using-mdc-typography($config) {
    @include mdc-data-table.table-styles($query: mdc-helpers.$mdc-typography-styles-query);
  }
}

@mixin density($config-or-theme) {
  $density-scale: theming.get-density-config($config-or-theme);
  .mat-mdc-table {
    @include mdc-data-table-theme.density($density-scale,
      $query: mdc-helpers.$mdc-base-styles-query);
  }
}

@mixin theme($theme-or-color-config) {
  $theme: theming.private-legacy-get-theme($theme-or-color-config);
  @include theming.private-check-duplicate-theme-styles($theme, 'mat-table') {
    $color: theming.get-color-config($theme);
    $density: theming.get-density-config($theme);
    $typography: theming.get-typography-config($theme);

    @if $color != null {
      @include color($color);
    }
    @if $density != null {
      @include density($density);
    }
    @if $typography != null {
      @include typography($typography);
    }
  }
}
