@use 'sass:map';
@use '@angular/material' as mat;
@use '../core/theming/inspection';
@use '../core/theming/validation';
@use '../core/tokens/token-utils';
@use '../core/tokens/m2/mtx/select' as tokens-mtx-select;

@mixin base($theme) {
  @if mat.get-theme-version($theme) == 1 {
    @include _theme-from-tokens(inspection.get-theme-tokens($theme, base));
  }
  @else {
    @include mat.private-current-selector-or-root() {
      @include token-utils.create-token-values(tokens-mtx-select.$prefix,
        tokens-mtx-select.get-unthemable-tokens());
    }
  }
}

@mixin color($theme, $options...) {
  @if mat.get-theme-version($theme) == 1 {
    @include _theme-from-tokens(inspection.get-theme-tokens($theme, color), $options...);
  }
  @else {
    @include mat.private-current-selector-or-root() {
      @include token-utils.create-token-values(tokens-mtx-select.$prefix,
        tokens-mtx-select.get-color-tokens($theme));
    }

    .ng-dropdown-panel {
      &.mat-accent {
        $accent-tokens: tokens-mtx-select.private-get-color-palette-color-tokens($theme, accent);
        @include token-utils.create-token-values(tokens-mtx-select.$prefix, $accent-tokens);
      }

      &.mat-warn {
        $warn-tokens: tokens-mtx-select.private-get-color-palette-color-tokens($theme, warn);
        @include token-utils.create-token-values(tokens-mtx-select.$prefix, $warn-tokens);
      }
    }
  }
}

@mixin typography($theme) {
  @if mat.get-theme-version($theme) == 1 {
    @include _theme-from-tokens(inspection.get-theme-tokens($theme, typography));
  }
  @else {}
}

@mixin density($theme) {
  @if mat.get-theme-version($theme) == 1 {
    @include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
  }
  @else {
    @include mat.private-current-selector-or-root() {
      @include token-utils.create-token-values(tokens-mtx-select.$prefix,
        tokens-mtx-select.get-density-tokens($theme));
    }
  }
}

@mixin theme($theme, $options...) {
  @include mat.private-check-duplicate-theme-styles($theme, 'mtx-select') {
    @if mat.get-theme-version($theme) == 1 {
      @include _theme-from-tokens(inspection.get-theme-tokens($theme), $options...);
    }
    @else {
      @include base($theme);
      @if mat.theme-has($theme, color) {
        @include color($theme);
      }
      @if mat.theme-has($theme, density) {
        @include density($theme);
      }
      @if mat.theme-has($theme, typography) {
        @include typography($theme);
      }
    }
  }
}

@mixin _theme-from-tokens($tokens, $options...) {
  @include validation.selector-defined(
    'Calls to Angular Material theme mixins with an M3 theme must be wrapped in a selector');
  $mtx-select-tokens: token-utils.get-tokens-for($tokens, tokens-mtx-select.$prefix, $options...);
  @include token-utils.create-token-values(tokens-mtx-select.$prefix, $mtx-select-tokens);
}
