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

@use 'sass:map';
@use '@material/animation/animation' as mdc-animation;
@use '@material/dom/dom' as mdc-dom;
@use '@material/feature-targeting/feature-targeting' as mdc-feature-targeting;
@use '@material/ripple/ripple' as mdc-ripple;
@use '@material/ripple/ripple-theme' as mdc-ripple-theme;
@use '@material/touch-target/variables' as mdc-touch-target-variables;
@use '@material/theme/theme' as mdc-theme;
@use './radio-theme';
@use '../theme/custom-properties';
@use '../theme';
@use '../theme/theme-values';

$ripple-target: '.mdc-radio__ripple';

///
/// Radio core styles.
///
@mixin core-styles($query: mdc-feature-targeting.all()) {
  @include without-ripple($query);
  @include ripple($query);

  // FORGE (new): adding our custom wrapper element styles in the Shadow DOM
  @include wrapper-styles;
}

@mixin wrapper-styles() {
  .forge-radio-wrapper--disabled {
    cursor: not-allowed;

    ::slotted(label) {
      cursor: not-allowed;
    }
  }
}

// This API is intended for use by frameworks that may want to separate the ripple-related styles from the other
// radio styles. It is recommended that most users use `mdc-radio-core-styles` instead.
@mixin without-ripple($query: mdc-feature-targeting.all()) {
  // postcss-bem-linter: define radio

  $feat-animation: mdc-feature-targeting.create-target($query, animation);
  $feat-color: mdc-feature-targeting.create-target($query, color);
  $feat-structure: mdc-feature-targeting.create-target($query, structure);

  // @include mdc-touch-target-mixins.wrapper($query); // COPYBARA_COMMENT_THIS_LINE

  .mdc-radio {
    @include radio-theme.unchecked-stroke-color(radio-theme.$unchecked-color, $query: $query);
    @include radio-theme.checked-stroke-color(radio-theme.$baseline-theme-color, $query: $query);
    @include radio-theme.ink-color(radio-theme.$baseline-theme-color, $query: $query);
    @include radio-theme.disabled-unchecked-stroke-color(radio-theme.$disabled-circle-color, $query: $query);
    @include radio-theme.disabled-checked-stroke-color(radio-theme.$disabled-circle-color, $query: $query);
    @include radio-theme.disabled-ink-color(radio-theme.$disabled-circle-color, $query: $query);
    @include radio-theme.focus-indicator-color(radio-theme.$baseline-theme-color, $query: $query);
    @include radio-theme.density(radio-theme.$density-scale, $query: $query);

    @include mdc-dom.forced-colors-mode {
      @include radio-theme.disabled-unchecked-stroke-color(GrayText, $query: $query);
      @include radio-theme.disabled-checked-stroke-color(GrayText, $query: $query);
      @include radio-theme.disabled-ink-color(GrayText, $query: $query);
    }

    @include mdc-feature-targeting.targets($feat-structure) {
      display: inline-block;
      position: relative;
      flex: 0 0 auto;
      box-sizing: content-box;
      width: radio-theme.$icon-size;
      height: radio-theme.$icon-size;
      cursor: pointer;
      /* @alternate */
      will-change: opacity, transform, border-color, color;
    }

    // Container for radio circles and ripple.
    &__background {
      @include mdc-feature-targeting.targets($feat-structure) {
        display: inline-block;
        position: relative;
        box-sizing: border-box;
        width: radio-theme.$icon-size;
        height: radio-theme.$icon-size;
      }

      &::before {
        @include mdc-feature-targeting.targets($feat-structure) {
          position: absolute;
          transform: scale(0, 0);
          border-radius: 50%;
          opacity: 0;
          pointer-events: none;
          content: '';
        }

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

    &__outer-circle {
      @include mdc-feature-targeting.targets($feat-structure) {
        position: absolute;
        top: 0;
        left: 0;
        box-sizing: border-box;
        width: 100%;
        height: 100%;
        border-width: 2px;
        border-style: solid;
        border-radius: 50%;
      }

      @include mdc-feature-targeting.targets($feat-animation) {
        transition: exit(border-color);
      }
    }

    &__inner-circle {
      @include mdc-feature-targeting.targets($feat-structure) {
        position: absolute;
        top: 0;
        left: 0;
        box-sizing: border-box;
        width: 100%;
        height: 100%;
        transform: scale(0, 0);
        border-width: 10px;
        border-style: solid;
        border-radius: 50%;
      }

      @include mdc-feature-targeting.targets($feat-animation) {
        transition: exit(transform), exit(border-color);
      }
    }

    // FORGE (modify): use slotted selector to target <input>
    ::slotted(input[type=radio]) {
      @include mdc-feature-targeting.targets($feat-structure) {
        position: absolute;
        margin: 0;
        padding: 0;
        opacity: 0;
        cursor: inherit;
        z-index: 1;

        // FORGE (new): make sure to stretch the input for touch target
        top: 0;
        right: 0;
        left: 0;
        width: radio-theme.$maximum-size;
        height: radio-theme.$maximum-size;
      }
    }

    // FORGE (new): we need to set the dense styles on the slotted input
    &.forge-radio--dense {
      @include radio-theme.density(-3);

      ::slotted(input[type='radio']) {
        width: radio-theme.$minimum-size;
        height: radio-theme.$minimum-size;
      }
    }

    // FORGE (new): we need to set the label color theme when disabled
    &--disabled + ::slotted(label) {
      @include theme.property(color, label-disabled-on-background);
    }
  }

  // FORGE (modify): use FORGE  classes since <input> is slotted and not a sibling
  .forge-radio--checked,
  .mdc-radio--disabled {
    .mdc-radio__background {
      @include mdc-feature-targeting.targets($feat-animation) {
        transition: enter(opacity), enter(transform);
      }

      .mdc-radio__outer-circle {
        @include mdc-feature-targeting.targets($feat-animation) {
          transition: enter(border-color);
        }
      }

      .mdc-radio__inner-circle {
        @include mdc-feature-targeting.targets($feat-animation) {
          transition: enter(transform), enter(border-color);
        }
      }
    }
  }

  .mdc-radio--disabled {
    @include mdc-feature-targeting.targets($feat-structure) {
      cursor: default;
      pointer-events: none;
    }
  }

  // FORGE (modify): use FORGE  class since <input> is slotted and not a sibling
  .forge-radio--checked {
    .mdc-radio__background {
      .mdc-radio__inner-circle {
        @include mdc-feature-targeting.targets($feat-structure) {
          transform: scale(0.5);
        }

        @include mdc-feature-targeting.targets($feat-animation) {
          transition: enter(transform), enter(border-color);
        }
      }
    }
  }

  // FORGE (modify): use FORGE  class since <input> is slotted and not a sibling
  .mdc-radio--disabled {
    .mdc-radio__background {
      @include mdc-feature-targeting.targets($feat-structure) {
        cursor: default;
      }
    }
  }

  // FORGE (modify): use FORGE  class since <input> is slotted and not a sibling
  .forge-radio--focused {
    .mdc-radio__background::before {
      @include mdc-feature-targeting.targets($feat-structure) {
        transform: scale(1);
        opacity: map.get(mdc-ripple-theme.$dark-ink-opacities, focus);
      }

      @include mdc-feature-targeting.targets($feat-animation) {
        transition: enter(opacity), enter(transform);
      }
    }
  }

  // postcss-bem-linter: end
}

// This API is intended for use by frameworks that may want to separate the ripple-related styles from the other
// radio styles. It is recommended that most users use `mdc-radio-core-styles` instead.
@mixin ripple($query: mdc-feature-targeting.all()) {
  $feat-structure: mdc-feature-targeting.create-target($query, structure);

  @include mdc-ripple.common($query); // COPYBARA_COMMENT_THIS_LINE

  .mdc-radio {
    @include mdc-ripple.surface($query: $query, $ripple-target: $ripple-target);
    @include mdc-ripple.radius-unbounded($query: $query, $ripple-target: $ripple-target);

    // FORGE (modify): using forge-specific theme style custom property
    $tertiary-prop: custom-properties.create(--forge-theme-tertiary, theme-values.$tertiary);
    @include mdc-ripple-theme.states($color: $tertiary-prop, $query: $query, $ripple-target: $ripple-target);

    &.mdc-ripple-upgraded,
    &.mdc-ripple-upgraded--background-focused {
      .mdc-radio__background::before {
        @include mdc-feature-targeting.targets($feat-structure) {
          content: none;
        }
      }
    }
  }

  #{$ripple-target} {
    @include mdc-ripple.target-common($query: $query);
  }
}

@function enter($name) {
  @return mdc-animation.enter($name, radio-theme.$transition-duration);
}

@function exit($name) {
  @return mdc-animation.exit-temporary($name, radio-theme.$transition-duration);
}
