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

@use 'sass:math';
@use '@material/animation/functions' as mdc-animation-functions;
@use '@material/feature-targeting/feature-targeting' as mdc-feature-targeting;
@use '@material/theme/gss' as mdc-gss;
@use '@material/theme/theme' as mdc-theme;
@use '@material/typography/typography' as mdc-typography;
@use './variables';
@use './keyframes';
@use '../theme';
@use '../theme/custom-properties';

@mixin core-styles($query: mdc-feature-targeting.all()) {
  $feat-structure: mdc-feature-targeting.create-target($query, structure);
  $feat-animation: mdc-feature-targeting.create-target($query, animation);

  @include color(variables.$color, $query);

  // FORGE (modify): use custom property for determinate track color
  $track-color-prop: custom-properties.create(--forge-circular-progress-track-color, variables.$track-color);
  @include track-color($track-color-prop, $query: $query);

  @include mdc-feature-targeting.targets($feat-animation) {
    @include keyframes.container-rotate-keyframes_;
    @include keyframes.spinner-layer-rotate-keyframes_;
    @include keyframes.color-1-fade-in-out-keyframes_;
    @include keyframes.color-2-fade-in-out-keyframes_;
    @include keyframes.color-3-fade-in-out-keyframes_;
    @include keyframes.color-4-fade-in-out-keyframes_;
    @include keyframes.left-spin-keyframes_;
    @include keyframes.right-spin-keyframes_;
  }

  .mdc-circular-progress {
    // FORGE (new): added a custom property for setting the height and width together
    @include theme.css-custom-property(height, --forge-circular-progress-size, variables.$size);
    @include theme.css-custom-property(width, --forge-circular-progress-size, variables.$size);

    @include mdc-feature-targeting.targets($feat-structure) {
      display: inline-flex;
      justify-content: center; // FORGE (new): centering slotted content
      align-items: center; // FORGE (new): centering slotted content
      position: relative;
      /* @noflip */
      direction: ltr;
    }

    @include mdc-feature-targeting.targets($feat-animation) {
      transition: mdc-animation-functions.exit-temporary(opacity, 250ms);
    }

    // FORGE (new): added custom properties for setting stroke-width and radius
    circle {
      @include theme.css-custom-property(stroke-width, --forge-circular-progress-stroke-width, variables.$stroke-width);
    }

    ::slotted(*) {
      @include mdc-typography.typography(body1);
    }
  }

  .mdc-circular-progress__determinate-container,
  .mdc-circular-progress__indeterminate-circle-graphic,
  .mdc-circular-progress__indeterminate-container,
  .mdc-circular-progress__spinner-layer {
    @include mdc-feature-targeting.targets($feat-structure) {
      position: absolute;
      width: 100%;
      height: 100%;
    }
  }

  .mdc-circular-progress__determinate-container {
    @include mdc-feature-targeting.targets($feat-structure) {
      transform: rotate(-90deg);
    }
  }

  .mdc-circular-progress__indeterminate-container {
    @include mdc-feature-targeting.targets($feat-structure) {
      // Font size, letter spacing and nowrap are required in order to ignore
      // whitespaces between HTML elements.
      font-size: 0;
      letter-spacing: 0;
      white-space: nowrap;
      opacity: 0;
    }
  }

  .mdc-circular-progress__determinate-circle-graphic,
  .mdc-circular-progress__indeterminate-circle-graphic {
    @include mdc-feature-targeting.targets($feat-structure) {
      fill: transparent;
    }
  }

  .mdc-circular-progress__determinate-circle {
    @include mdc-feature-targeting.targets($feat-animation) {
      transition: mdc-animation-functions.enter(stroke-dashoffset, 500ms);
    }
  }

  .mdc-circular-progress__gap-patch {
    @include mdc-feature-targeting.targets($feat-structure) {
      position: absolute;
      top: 0;
      /* @noflip */
      left: 47.5%;
      box-sizing: border-box;
      width: 5%;
      height: 100%;
      overflow: hidden;
    }

    .mdc-circular-progress__indeterminate-circle-graphic {
      @include mdc-feature-targeting.targets($feat-structure) {
        /* @noflip */
        left: -900%;
        width: 2000%;
        transform: rotate(180deg);
      }
    }
  }

  .mdc-circular-progress__circle-clipper {
    @include mdc-feature-targeting.targets($feat-structure) {
      display: inline-flex;
      position: relative;
      width: 50%;
      height: 100%;
      overflow: hidden;
    }

    .mdc-circular-progress__indeterminate-circle-graphic {
      @include mdc-feature-targeting.targets($feat-structure) {
        width: 200%;
      }
    }
  }

  .mdc-circular-progress__circle-right {
    .mdc-circular-progress__indeterminate-circle-graphic {
      @include mdc-feature-targeting.targets($feat-structure) {
        /* @noflip */
        left: -100%;
      }
    }
  }

  .mdc-circular-progress--indeterminate {
    .mdc-circular-progress__determinate-container {
      @include mdc-feature-targeting.targets($feat-structure) {
        opacity: 0;
      }
    }

    .mdc-circular-progress__indeterminate-container {
      @include mdc-feature-targeting.targets($feat-structure) {
        opacity: 1;
      }
    }

    @include indeterminate-active-animations_($query);
  }

  .mdc-circular-progress--closed {
    @include mdc-feature-targeting.targets($feat-structure) {
      opacity: 0;
    }
  }
}

// Public mixins

/// Customizes the stroke-color of the indicator. Applies to the
/// determinate variant, and also the indeterminate variant unless the
/// four-color mixin is applied.
/// @param {Color} $color - The desired stroke color.
/// @see {mixin} indeterminate-colors
@mixin color($color, $query: mdc-feature-targeting.all()) {
  $feat-color: mdc-feature-targeting.create-target($query, color);

  .mdc-circular-progress__determinate-circle,
  .mdc-circular-progress__indeterminate-circle-graphic {
    @include mdc-feature-targeting.targets($feat-color) {
      @include theme.property(stroke, $color);
    }
  }
}

/// Customizes the track color of the indicator. Applies to the
/// determinate variant only.
/// @param {Color} $color - The desired track color.
@mixin track-color($color, $query: mdc-feature-targeting.all()) {
  $feat-color: mdc-feature-targeting.create-target($query, color);

  .mdc-circular-progress__determinate-track {
    @include mdc-feature-targeting.targets($feat-color) {
      @include mdc-theme.property(stroke, $color);
    }
  }
}

/// Applies four animated stroke-colors to the indeterminate indicator.
/// Applicable to the indeterminate variant only and overrides any single color
/// currently set.
/// @param {List} $colors - A list of four desired colors.
/// @see {mixin} color
@mixin indeterminate-colors($colors, $query: mdc-feature-targeting.all()) {
  $feat-color: mdc-feature-targeting.create-target($query, color);

  @if length($colors) != 4 {
    @error '`mdc-circular-progress-colors` accepts exactly 4 colors';
  }

  @for $i from 1 through 4 {
    .mdc-circular-progress__color-#{$i}
      .mdc-circular-progress__indeterminate-circle-graphic {
      @include mdc-feature-targeting.targets($feat-color) {
        @include mdc-theme.property(stroke, nth($colors, $i));
      }
    }
  }
}

// Private mixins

/// Sets the animations for the indicator in indeterminate mode.
/// @access private
@mixin indeterminate-active-animations_($query: mdc-feature-targeting.all()) {
  $feat-animation: mdc-feature-targeting.create-target($query, animation);

  .mdc-circular-progress__indeterminate-container {
    @include mdc-feature-targeting.targets($feat-animation) {
      $duration: math.div(360deg * variables.$arc-time, variables.$arc-start-rotation-interval + (360 - variables.$arc-size));

      animation: mdc-circular-progress-container-rotate $duration linear
        infinite;
    }
  }

  $spinner-rotate-animation: mdc-circular-progress-spinner-layer-rotate 4 *
    variables.$arc-time variables.$timing-function infinite both;

  .mdc-circular-progress__spinner-layer {
    @include mdc-feature-targeting.targets($feat-animation) {
      animation: $spinner-rotate-animation;
    }
  }

  @for $i from 1 through 4 {
    $color-fade-in-out-animation: mdc-circular-progress-color-#{$i}-fade-in-out
      4 *
      variables.$arc-time
      variables.$timing-function
      infinite
      both;

    .mdc-circular-progress__color-#{$i} {
      @include mdc-feature-targeting.targets($feat-animation) {
        animation: $spinner-rotate-animation, $color-fade-in-out-animation;
      }
    }
  }

  .mdc-circular-progress__circle-left
    .mdc-circular-progress__indeterminate-circle-graphic {
    @include mdc-feature-targeting.targets($feat-animation) {
      @include mdc-gss.annotate(
        (
          noflip: true,
        )
      );
      animation: mdc-circular-progress-left-spin variables.$arc-time
        variables.$timing-function infinite both;
    }
  }

  .mdc-circular-progress__circle-right
    .mdc-circular-progress__indeterminate-circle-graphic {
    @include mdc-feature-targeting.targets($feat-animation) {
      @include mdc-gss.annotate(
        (
          noflip: true,
        )
      );
      animation: mdc-circular-progress-right-spin variables.$arc-time
        variables.$timing-function infinite both;
    }
  }
}
