//
// Copyright 2021 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

@use 'sass:map';
@use '../theme/theme';
@use '../theme/keys';
@use '../tokens/resolvers';
@use './chip-theme';

$_light-theme: (
  container-shape: null,
  container-height: null,
  elevated-container-shadow-color: null,
  flat-container-elevation: null,
  flat-unselected-outline-color: null,
  flat-unselected-outline-width: null,
  flat-selected-container-color: null,
  flat-selected-outline-color: null,
  flat-selected-outline-width: null,
  elevated-container-elevation: null,
  elevated-unselected-container-color: null,
  elevated-selected-container-color: null,
  label-text-font: null,
  label-text-line-height: null,
  label-text-size: null,
  label-text-weight: null,
  label-text-tracking: null,
  unselected-label-text-color: null,
  selected-label-text-color: null,
  with-icon-icon-size: null,
  with-icon-unselected-icon-color: null,
  with-icon-selected-icon-color: null,
  disabled-label-text-color: null,
  disabled-label-text-opacity: null,
  flat-disabled-unselected-outline-color: null,
  flat-disabled-unselected-outline-opacity: null,
  flat-disabled-selected-outline-color: null,
  flat-disabled-selected-outline-opacity: null,
  with-icon-disabled-icon-color: null,
  with-icon-disabled-icon-opacity: null,
  elevated-disabled-container-elevation: null,
  elevated-disabled-container-color: null,
  elevated-disabled-container-opacity: null,
  unselected-hover-state-layer-color: null,
  unselected-hover-state-layer-opacity: null,
  unselected-hover-label-text-color: null,
  selected-hover-state-layer-color: null,
  selected-hover-state-layer-opacity: null,
  selected-hover-label-text-color: null,
  with-icon-unselected-hover-icon-color: null,
  with-icon-selected-hover-icon-color: null,
  elevated-hover-container-elevation: null,
  flat-selected-hover-container-elevation: null,
  flat-unselected-hover-container-elevation: null,
  unselected-focus-state-layer-color: null,
  unselected-focus-state-layer-opacity: null,
  unselected-focus-label-text-color: null,
  selected-focus-state-layer-color: null,
  selected-focus-state-layer-opacity: null,
  selected-focus-label-text-color: null,
  flat-unselected-focus-outline-color: null,
  with-icon-unselected-focus-icon-color: null,
  with-icon-selected-focus-icon-color: null,
  elevated-focus-container-elevation: null,
  elevated-pressed-container-elevation: null,
  flat-selected-focus-container-elevation: null,
  flat-unselected-focus-container-elevation: null,
  unselected-pressed-state-layer-color: null,
  unselected-pressed-state-layer-opacity: null,
  unselected-pressed-label-text-color: null,
  selected-pressed-state-layer-color: null,
  selected-pressed-state-layer-opacity: null,
  selected-pressed-label-text-color: null,
  with-icon-unselected-pressed-icon-color: null,
  with-icon-selected-pressed-icon-color: null,
  elevated-selected-container-elevation: null,
  flat-selected-pressed-container-elevation: null,
  flat-unselected-pressed-container-elevation: null,
  container-surface-tint-layer-color: null,
);

$_custom-property-prefix: 'filter-chip';

@mixin theme($theme, $resolvers: resolvers.$material) {
  @include theme.validate-theme($_light-theme, $theme);

  // Resolve elevation.
  @each $key
    in (
      flat-container-elevation,
      elevated-container-elevation,
      elevated-disabled-container-elevation,
      elevated-hover-container-elevation,
      elevated-focus-container-elevation,
      flat-selected-focus-container-elevation,
      flat-unselected-focus-container-elevation,
      elevated-selected-container-elevation,
      flat-selected-pressed-container-elevation,
      flat-unselected-pressed-container-elevation
    )
  {
    $value: map.get($theme, $key);
    @if $value {
      $theme: map.set(
        $theme,
        $key,
        meta.call(
          $elevation-resolver: map.get($resolvers, elevation),
          $elevation: $value,
          $shadow-color: map.get($theme, elevated-container-shadow-color),
        )
      );
    }
  }

  @include keys.declare-custom-properties(
    $theme,
    $prefix: $_custom-property-prefix
  );
}

@mixin theme-styles($theme, $resolvers: resolvers.$material) {
  @include theme.validate-theme($_light-theme, $theme);
  $theme: keys.create-theme-properties(
    $theme,
    $prefix: $_custom-property-prefix
  );
  @include chip-theme.theme-styles($theme, $resolvers: $resolvers);
}
