/**
 * @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 'sass:math';
@use '@material/feature-targeting/feature-targeting' as mdc-feature-targeting;
@use '@material/theme/state' as mdc-theme-state;
@use '@material/theme/theme' as mdc-theme;
@use '@material/ripple/ripple-theme'as mdc-ripple-theme;
@use './button-base';
@use './button-shared-theme';
@use './button-ripple';

$outlined-border-width: 1px !default;
$outline-color: primary !default; // FORGE(modify): use primary theme instead of static color

$light-theme: (
  container-color: transparent,
  container-hover-color: null,
  container-focus-color: null,
  container-pressed-color: null,
  container-disabled-color: transparent,
  density: button-shared-theme.$density-scale,
  icon-color: null,
  icon-hover-color: null,
  icon-focus-color: null,
  icon-pressed-color: null,
  icon-disabled-color: null,
  label-color: primary,
  label-hover-color: null,
  label-focus-color: null,
  label-pressed-color: null,
  label-disabled-color: button-shared-theme.$disabled-ink-color,
  outline-width: $outlined-border-width,
  outline-color: $outline-color,
  outline-hover-color: null,
  outline-focus-color: null,
  outline-pressed-color: null,
  outline-disabled-color: button-shared-theme.$disabled-container-color,
  ripple-color: primary,
  ripple-opacity: null,
  shape: button-shared-theme.$shape-radius,
);

/// Sets theme based on provided theme configuration.
/// Only emits theme related styles.
/// @param {Map} $theme - Theme configuration to use.
@mixin theme($theme, $query: mdc-feature-targeting.all()) {
  @include mdc-theme.validate-theme($light-theme, $theme);
  @include button-shared-theme.theme($theme, $query: $query);
  @include outline-color(
    (
      default: map.get($theme, outline-color),
      hover: map.get($theme, outline-hover-color),
      focus: map.get($theme, outline-focus-color),
      pressed: map.get($theme, outline-pressed-color),
      disabled: map.get($theme, outline-disabled-color),
    ),
    $query: $query
  );
  @include outline-width(map.get($theme, outline-width), $query: $query);
}

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

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

    @include mdc-ripple-theme.focus() {
      @include _outline-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 _outline-color(
        mdc-theme-state.get-pressed-state($color-or-map),
        $query: $query
      );
    }
  }

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

@mixin outline-width(
  $outline-width,
  $padding: button-shared-theme.$contained-horizontal-padding,
  $query: mdc-feature-targeting.all()
) {
  @if $outline-width != null {
    $feat-structure: mdc-feature-targeting.create-target($query, structure);
    // Note: Adjust padding to maintain consistent width with non-outlined buttons
    $padding-value: math.max($padding - $outline-width, 0);

    @include button-shared-theme.horizontal-padding($padding-value, $query);

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

    #{button-ripple.$ripple-target} {
      @include mdc-feature-targeting.targets($feat-structure) {
        top: -$outline-width;
        left: -$outline-width;
        border: $outline-width solid transparent;
      }
    }
  }
}

///
/// Sets the outline color to the given color. This mixin should be
/// wrapped in a selector that qualifies button state.
/// @access private
///
@mixin _outline-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(border-color, $color);
    }
  }
}
