//
// Copyright 2022 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.
//

// Selector '.mdc-*' should only be used in this project.
// stylelint-disable selector-class-pattern --
// NOTE: this is the implementation of the aforementioned classes.

@use 'sass:map';
@use '../notched-outline/mixins' as notched-outline-mixins;
@use '../theme/theme';
@use '../tokens/resolvers';
@use './text-field-theme';
@use './mixins';

$_light-theme: (
  caret-color: null,
  container-height: null,
  container-shape: null,
  disabled-input-text-color: null,
  disabled-input-text-opacity: null,
  disabled-label-text-color: null,
  disabled-label-text-opacity: null,
  disabled-leading-icon-color: null,
  disabled-leading-icon-opacity: null,
  disabled-outline-color: null,
  disabled-outline-opacity: null,
  disabled-outline-width: null,
  disabled-supporting-text-color: null,
  disabled-supporting-text-opacity: null,
  disabled-trailing-icon-color: null,
  disabled-trailing-icon-opacity: null,
  error-caret-color: null,
  error-focus-caret-color: null,
  error-focus-input-text-color: null,
  error-focus-label-text-color: null,
  error-focus-leading-icon-color: null,
  error-focus-outline-color: null,
  error-focus-supporting-text-color: null,
  error-focus-trailing-icon-color: null,
  error-hover-caret-color: null,
  error-hover-input-text-color: null,
  error-hover-label-text-color: null,
  error-hover-leading-icon-color: null,
  error-hover-outline-color: null,
  error-hover-supporting-text-color: null,
  error-hover-trailing-icon-color: null,
  error-input-text-color: null,
  error-label-text-color: null,
  error-leading-icon-color: null,
  error-outline-color: null,
  error-supporting-text-color: null,
  error-trailing-icon-color: null,
  focus-caret-color: null,
  focus-input-text-color: null,
  focus-label-text-color: null,
  focus-leading-icon-color: null,
  focus-outline-color: null,
  focus-outline-width: null,
  focus-supporting-text-color: null,
  focus-trailing-icon-color: null,
  hover-caret-color: null,
  hover-input-text-color: null,
  hover-label-text-color: null,
  hover-leading-icon-color: null,
  hover-outline-color: null,
  hover-outline-width: null,
  hover-supporting-text-color: null,
  hover-trailing-icon-color: null,
  input-text-color: null,
  input-text-font: null,
  input-text-line-height: null,
  input-text-size: null,
  input-text-tracking: null,
  input-text-type: null,
  input-text-weight: null,
  label-text-color: null,
  label-text-font: null,
  label-text-line-height: null,
  label-text-populated-line-height: null,
  label-text-populated-size: null,
  label-text-size: null,
  label-text-tracking: null,
  label-text-type: null,
  label-text-weight: null,
  leading-icon-color: null,
  leading-icon-size: null,
  outline-color: null,
  outline-width: null,
  supporting-text-color: null,
  supporting-text-font: null,
  supporting-text-line-height: null,
  supporting-text-size: null,
  supporting-text-tracking: null,
  supporting-text-type: null,
  supporting-text-weight: null,
  trailing-icon-color: null,
  trailing-icon-size: null,
);

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

  @if map.get($theme, container-height) {
    @include mixins.outlined-height(map.get($theme, container-height));

    &.mdc-text-field--with-leading-icon {
      @include mixins.outlined-with-leading-icon-height(
        map.get($theme, container-height)
      );
    }
  }
  @if map.get($theme, container-shape) {
    @include mixins.outline-shape-radius(map.get($theme, container-shape));
  }

  @include _outline-color(
    (
      default: map.get($theme, outline-color),
      hover: map.get($theme, hover-outline-color),
      focus: map.get($theme, focus-outline-color),
      disabled: map.get($theme, disabled-outline-color),
    )
  );
  @include _error-outline-color(
    (
      default: map.get($theme, error-outline-color),
      hover: map.get($theme, error-hover-outline-color),
      focus: map.get($theme, error-focus-outline-color),
    )
  );
  @include _outline-width(
    (
      default: map.get($theme, outline-width),
      hover: map.get($theme, hover-outline-width),
      focus: map.get($theme, focus-outline-width),
    )
  );
}

@mixin _outline-color($colors) {
  @include text-field-theme.if-enabled {
    @include _set-outline-color(map.get($colors, default));

    @include text-field-theme.if-hovered {
      .mdc-notched-outline {
        @include _set-outline-color(map.get($colors, hover));
      }
    }

    @include text-field-theme.if-focused {
      @include _set-outline-color(map.get($colors, focus));
    }
  }

  @include text-field-theme.if-disabled {
    @include _set-outline-color(map.get($colors, disabled));
  }
}

@mixin _set-outline-color($color) {
  @include notched-outline-mixins.color($color);
}

@mixin _error-outline-color($color) {
  &.mdc-text-field--invalid {
    @include _outline-color($color);
  }
}

@mixin _outline-width($width) {
  @include text-field-theme.if-enabled {
    @include _set-outline-width(map.get($width, default));

    @include text-field-theme.if-hovered {
      @include _set-outline-width(map.get($width, hover));
    }

    @include text-field-theme.if-focused {
      @include _set-outline-width(map.get($width, focus));
    }
  }
}

@mixin _set-outline-width($width) {
  .mdc-notched-outline {
    @include notched-outline-mixins.stroke-width($width);
  }
}
