@use 'sass:map';
@use '~@angular/material' as mat;
@use '../../../../lib/core/mixins';
@use '../../../../resizable-table/lib/resizable-table-theme' as rs;
@use '../../../../lib/core/ghost-table-theme' as gt;

@mixin color($theme) {
  $config: mat.get-color-config($theme);
  $background: map.get($config, background);
  $foreground: map.get($config, foreground);
  $primary: map.get($config, primary);
  $accent: map.get($config, accent);
  $warn: map.get($config, warn);
  $is-dark-theme: map.get($config, is-dark);
  $row-hover-background: if(
    $is-dark-theme,
    mat.get-color-from-palette($primary, default, 0.08),
    mat.get-color-from-palette($primary, default, 0.04)
  );
  $focusBackgroundDarkTheme: map_get(mat.$grey-palette, 700);
  $focusBackgroundLightTheme: map_get(mat.$grey-palette, 100);
  $focusBackground: if($is-dark-theme, $focusBackgroundDarkTheme, $focusBackgroundLightTheme);

  mtna-results-table {
    background-color: mat.get-color-from-palette($background, card);
    border-color: mat.get-color-from-palette($foreground, divider);

    @include rs.resizable-table-theme($theme);
    @include gt.ghost-table-theme($theme, $focusBackgroundDarkTheme, $focusBackgroundLightTheme);

    .mat-table.cdk-table {
      &:not(.mtna-ghost-table) tbody {
        tr:hover,
        tr:focus {
          background-color: $focusBackground;
        }
      }

      th.mat-header-cell {
        background-color: mat.get-color-from-palette($background, card);
      }
      tr.mtna-search-results-table-row:hover {
        background-color: $row-hover-background;
      }
      tr.mtna-search-results-table-sub-results-row:hover,
      tr.mtna-search-results-table-expanded-row:hover {
        background-color: transparent;
      }
    }

    $stickyBorderColor: mat.get-color-from-palette($foreground, divider);
    .mat-table-sticky-border-elem-right {
      border-left: 1px solid $stickyBorderColor;
    }

    &.mat-primary {
      .mat-table {
        @include mixins.mtna-selectable-outline-table($primary);
        @include _results-table-palette-overrides($primary, $background);
      }
    }
    &.mat-accent {
      .mat-table {
        @include mixins.mtna-selectable-outline-table($accent);
        @include _results-table-palette-overrides($accent, $background);
      }
    }
    &.mat-warn {
      .mat-table {
        @include mixins.mtna-selectable-outline-table($warn);
        @include _results-table-palette-overrides($warn, $background);
      }
    }
  }
}

@mixin typography($theme) {
  $typography-config: mat.get-typography-config($theme);
  mtna-results-table {
    .mtna-search-results-table-expanded-row {
      .mat-list .mat-list-item .mat-line {
        &:first-of-type {
          @include mat.typography-level($typography-config, 'body-2');
        }
        &:nth-child(n + 2) {
          @include mat.typography-level($typography-config, 'body-1');
        }
      }
    }
  }
}

@mixin theme($theme) {
  $config: mat.get-color-config($theme);
  @if $config != null {
    @include color($theme);
  }

  $typography-config: mat.get-typography-config($theme);
  @if $typography-config != null {
    @include typography($theme);
  }
}

@mixin _results-table-palette-overrides($palette, $background) {
  .mat-row .mat-cell.mat-column-navigate:last-of-type {
    border-right-color: mat.get-color-from-palette($background, card);
  }
  .mat-row.selected-row .mat-cell.mat-column-navigate:last-of-type {
    border-right-color: mat.get-color-from-palette($palette);
  }

  // Highlight navigate cell on hover/focus with theme color
  .mat-row .mat-cell.mat-column-navigate {
    &:hover,
    &:focus,
    &:focus-visible,
    &:focus-within {
      background-color: mat.get-color-from-palette($palette, default);
      color: mat.get-color-from-palette($palette, default-contrast);
    }
  }

  .mtna-search-results-table-sub-results-row,
  .mtna-search-results-table-expanded-row {
    .mat-cell {
      border-top-width: 0;
    }
  }
  .mtna-search-results-table-sub-results-row {
    .mat-cell {
      &:first-of-type {
        border-left: 1px solid transparent;
      }
      &:last-of-type {
        border-right: 1px solid transparent;
      }
    }
    &.mtna-search-results-table-sub-results-row--selected {
      background: mat.get-color-from-palette($palette, 0.08);
      &:hover {
        background: mat.get-color-from-palette($palette, 0.08);
      }
      .mat-cell {
        &:first-of-type {
          border-left-color: mat.get-color-from-palette($palette);
        }
        &:last-of-type {
          border-right-color: mat.get-color-from-palette($palette);
        }
      }
    }
  }

  .mtna-search-results-table-expanded-row.selected-expansion-row:hover {
    background: mat.get-color-from-palette($palette, 0.08);
  }
}
