/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
//
// 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.
//

// stylelint-disable selector-class-pattern --
// Selector '.forge-*' should only be used in this project.

@use 'sass:map';
@use '@material/density/functions' as mdc-density-functions;
@use '@material/density/variables' as mdc-density-variables;
@use '@material/dom/mixins' as mdc-dom-mixins;
@use '@material/feature-targeting/feature-targeting' as mdc-feature-targeting;
@use '@material/ripple/ripple-theme' as mdc-ripple-theme;
@use '@material/shape/mixins' as mdc-shape-mixins;
@use '@material/theme/state' as mdc-theme-state;
@use '@material/theme/theme' as mdc-theme;
@use '@material/theme/theme-color' as mdc-theme-color;
@use '@material/typography/typography' as mdc-typography;
@use './button-ripple';

$height: 36px !default;
$horizontal-padding: 8px !default;
$contained-horizontal-padding: 16px !default;

$minimum-height: 24px !default;
$maximum-height: $height !default;
$density-scale: mdc-density-variables.$default-scale !default;
$density-config: (
  height: (
    default: $height,
    maximum: $maximum-height,
    minimum: $minimum-height,
  ),
) !default;

$shape-radius: small !default;

$disabled-ink-color: text-disabled-on-light !default; // FORGE(modify): use theme styles instead of static color values
$disabled-container-color: text-disabled-on-background !default; // FORGE(modify): use theme styles instead of static color values

@mixin theme($theme, $query: mdc-feature-targeting.all()) {
  @include container-fill-color(
    (
      default: map.get($theme, container-color),
      hover: map.get($theme, container-hover-color),
      focus: map.get($theme, container-focus-color),
      pressed: map.get($theme, container-pressed-color),
      disabled: map.get($theme, container-disabled-color),
    ),
    $query: $query
  );

  @include ink-color(
    (
      default: map.get($theme, label-color),
      hover: map.get($theme, label-hover-color),
      focus: map.get($theme, label-focus-color),
      pressed: map.get($theme, label-pressed-color),
      disabled: map.get($theme, label-disabled-color),
    ),
    $query: $query
  );

  @include icon-color(
    (
      default: map.get($theme, icon-color),
      hover: map.get($theme, icon-hover-color),
      focus: map.get($theme, icon-focus-color),
      pressed: map.get($theme, icon-pressed-color),
      disabled: map.get($theme, icon-disabled-color),
    ),
    $query: $query
  );

  $ripple-color: map.get($theme, ripple-color);
  $ripple-opacity: map.get($theme, ripple-opacity);
  @if $ripple-color {
    @include ripple-states(
      $color: $ripple-color,
      $opacity-map: $ripple-opacity,
      $query: $query
    );
  }

  $density: map.get($theme, density);
  @if $density != null {
    @include density($density-scale: $density, $query: $query);
  }

  $shape: map.get($theme, shape);
  @if $density == null {
    $density: $density-scale;
  }
  @if $shape {
    @include shape-radius($shape, $density-scale: $density, $query: $query);
  }
}

///
/// Sets ripple color for button.
///
@mixin ripple-states(
  $color,
  $opacity-map: null,
  $query: mdc-feature-targeting.all()
) {
  @include mdc-ripple-theme.states(
    $color: $color,
    $opacity-map: $opacity-map,
    $query: $query,
    $ripple-target: button-ripple.$ripple-target
  );
}

@mixin filled-accessible(
  $container-fill-color,
  $query: mdc-feature-targeting.all()
) {
  $fill-tone: mdc-theme-color.tone($container-fill-color);

  @include container-fill-color($container-fill-color, $query);

  @if ($fill-tone == 'dark') {
    @include ink-color(text-primary-on-dark, $query);
    @include ripple-states($color: text-primary-on-dark, $query: $query);
  } @else {
    @include ink-color(text-primary-on-light, $query);
    @include ripple-states($color: text-primary-on-light, $query: $query);
  }
}

///
/// Sets the container fill color to the given color for an enabled button.
/// @param {Color|map} $color-or-map - The desired container fill color,
///     specified either as a flat value or a map of colors with states
///     {default, hover, focused, pressed, disabled} as keys.
///
@mixin container-fill-color($color-or-map, $query: mdc-feature-targeting.all()) {
  // :not(:disabled) is used to support link styled as button
  // as link does not support :enabled style
  &:not(:disabled) {
    @include _container-fill-color(
      mdc-theme-state.get-default-state($color-or-map),
      $query: $query
    );

    &:hover {
      @include _container-fill-color(
        mdc-theme-state.get-hover-state($color-or-map),
        $query: $query
      );
    }

    @include mdc-ripple-theme.focus() {
      @include _container-fill-color(
        mdc-theme-state.get-focus-state($color-or-map),
        $query: $query
      );
    }

    // Increase active state specificity due to mdc-ripple-theme.focus().
    &:active,
    &:focus:active {
      @include _container-fill-color(
        mdc-theme-state.get-pressed-state($color-or-map),
        $query: $query
      );
    }
  }

  &:disabled {
    @include _container-fill-color(
      mdc-theme-state.get-disabled-state($color-or-map),
      $query: $query
    );
  }
}

///
/// Sets the icon color to the given color for an enabled button.
/// @param {Color} $color-or-map - The desired icon color, specified either
///     as a flat value or a map of colors with states
///     {default, hover, focused, pressed, disabled} as keys.
///
@mixin icon-color($color-or-map, $query: mdc-feature-targeting.all()) {
  &:not(:disabled) {
    @include _icon-color(
      mdc-theme-state.get-default-state($color-or-map),
      $query: $query
    );

    &:hover {
      @include _icon-color(
        mdc-theme-state.get-hover-state($color-or-map),
        $query: $query
      );
    }

    @include mdc-ripple-theme.focus() {
      @include _icon-color(
        mdc-theme-state.get-focus-state($color-or-map),
        $query: $query
      );
    }

    // Increase active state specificity due to mdc-ripple-theme.focus().
    &:active,
    &:focus:active {
      @include _icon-color(
        mdc-theme-state.get-pressed-state($color-or-map),
        $query: $query
      );
    }
  }

  &:disabled {
    @include _icon-color(
      mdc-theme-state.get-disabled-state($color-or-map),
      $query: $query
    );
  }
}

///
/// Sets the ink color to the given color for an enabled button,
/// and sets the icon color to the given color unless `mdc-button-icon-color`
/// is also used.
/// @param {Color} $color-or-map - The desired ink color, specified either
///     as a flat value or a map of colors with states
///     {default, hover, focused, pressed, disabled} as keys.
///
@mixin ink-color($color-or-map, $query: mdc-feature-targeting.all()) {
  &:not(:disabled) {
    @include _ink-color(mdc-theme-state.get-default-state($color-or-map), $query: $query);

    &:hover {
      @include _ink-color(mdc-theme-state.get-hover-state($color-or-map), $query: $query);
    }

    @include mdc-ripple-theme.focus() {
      @include _ink-color(mdc-theme-state.get-focus-state($color-or-map), $query: $query);
    }

    // Increase active state specificity due to mdc-ripple-theme.focus().
    &:active,
    &:focus:active {
      @include _ink-color(
        mdc-theme-state.get-pressed-state($color-or-map),
        $query: $query
      );
    }
  }

  &:disabled {
    @include _ink-color(
      mdc-theme-state.get-disabled-state($color-or-map),
      $query: $query
    );
  }
}

///
/// Sets density scale for button.
///
/// @param {Number | String} $density-scale - Density scale value for component. Supported density scale values `-3`,
///     `-2`, `-1`, `0`.
///
@mixin density($density-scale, $query: mdc-feature-targeting.all()) {
  $height: mdc-density-functions.prop-value(
    $density-config: $density-config,
    $density-scale: $density-scale,
    $property-name: height,
  );

  @include height($height, $query: $query);

  @if $density-scale != 0 {
    @include _touch-target-reset($query: $query);
  }
}

///
/// Resets touch target-related styles. This is called from the density mixin to
/// automatically remove the increased touch target, since dense components
/// don't have the same default a11y requirements.
/// @access private
///
@mixin _touch-target-reset($query: mdc-feature-targeting.all()) {
  $feat-structure: mdc-feature-targeting.create-target($query, structure);

  @include mdc-feature-targeting.targets($feat-structure) {
    margin-top: 0;
    margin-bottom: 0;
  }
}

///
/// Sets custom height for button.
/// @param {Number} $height - Height of button in `px`.
///
@mixin height($height, $query: mdc-feature-targeting.all()) {
  $feat-structure: mdc-feature-targeting.create-target($query, structure);

  @include mdc-feature-targeting.targets($feat-structure) {
    height: $height;
  }
}

@mixin shape-radius(
  $radius,
  $rtl-reflexive: false,
  $density-scale: $density-scale,
  $query: mdc-feature-targeting.all()
) {
  $height: mdc-density-functions.prop-value(
    $density-config: $density-config,
    $density-scale: $density-scale,
    $property-name: height,
  );

  @include mdc-shape-mixins.radius(
    $radius,
    $rtl-reflexive,
    $component-height: $height,
    $query: $query
  );

  #{button-ripple.$ripple-target} {
    @include mdc-shape-mixins.radius(
      $radius,
      $rtl-reflexive,
      $component-height: $height,
      $query: $query
    );
  }
}

///
/// Sets horizontal padding to the given number.
/// @param {Number} $padding
///
@mixin horizontal-padding($padding, $query: mdc-feature-targeting.all()) {
  $feat-structure: mdc-feature-targeting.create-target($query, structure);

  @include mdc-feature-targeting.targets($feat-structure) {
    // $padding should be a single value; enforce it by specifying all 4 sides in the output
    padding: 0 $padding 0 $padding;
  }
}

///
/// Sets the button label to overflow as ellipsis
///
@mixin label-overflow-ellipsis($query: mdc-feature-targeting.all()) {
  .forge-button__label {
    @include mdc-typography.overflow-ellipsis($query: $query);
  }
}

///
/// Includes ad-hoc high contrast mode support.
///
@mixin high-contrast-mode-shim($query: mdc-feature-targeting.all()) {
  &::before {
    @include mdc-dom-mixins.transparent-border($query: $query);
  }

  &:focus {
    &::before {
      @include mdc-dom-mixins.transparent-border(
        $border-width: 5px,
        $border-style: double,
        $query: $query
      );
    }
  }
}

///
/// Sets the container fill color to the given color. This mixin should be
/// wrapped in a selector that qualifies button state.
/// @access private
///
@mixin _container-fill-color($color, $query: mdc-feature-targeting.all()) {
  $feat-color: mdc-feature-targeting.create-target($query, color);

  @if $color {
    @include mdc-feature-targeting.targets($feat-color) {
      @include mdc-theme.property(background-color, $color);
    }
  }
}

///
/// Sets the icon color to the given color. This mixin should be
/// wrapped in a selector that qualifies button state.
/// @access private
///
@mixin _icon-color($color, $query: mdc-feature-targeting.all()) {
  $feat-color: mdc-feature-targeting.create-target($query, color);

  @if $color {
    .forge-button__icon {
      @include mdc-feature-targeting.targets($feat-color) {
        @include mdc-theme.property(color, $color);
      }
    }
  }
}

///
/// Sets the ink color to the given color. This mixin should be
/// wrapped in a selector that qualifies button state.
/// @access private
///
@mixin _ink-color($color, $query: mdc-feature-targeting.all()) {
  $feat-color: mdc-feature-targeting.create-target($query, color);

  @if $color {
    @include mdc-feature-targeting.targets($feat-color) {
      @include mdc-theme.property(color, $color);
    }
  }
}
