/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
//
// Copyright 2018 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 '.forge-*' should only be used in this project.
// stylelint-disable selector-class-pattern

@use '@material/rtl' as mdc-rtl-mixins;
@use '@material/theme/theme' as mdc-theme;
@use '@material/feature-targeting/feature-targeting' as mdc-feature-targeting;
@use '@material/animation/variables' as mdc-animation-variables;
@use '@material/typography/typography' as mdc-typography;
@use './variables';

@mixin core-styles($query: mdc-feature-targeting.all()) {
  .forge-floating-label {
    @include base($query: $query);

    &--float-above {
      @include float($query: $query);
    }

    &--float-above-end-keyframe {
      transition: none;
      font-size: variables.$font-size * variables.$float-scale;
      @include float(variables.$position-y, 0%, 1);
    }

    &--unfloat-above-start-keyframe {
      transition: none;
      font-size: variables.$font-size;
      @include float(variables.$position-y, 0%, variables.$float-scale);
    }

    &--required {
      @include required($query: $query);
    }

    &--shake {
      @include shake-animation(standard, $query: $query);
    }
  }

  @include shake-keyframes(standard, variables.$position-y, $query: $query);
}

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

  @include mdc-typography.typography(subtitle1, $exclude-props: (line-height, font-size), $query: $query);
  font-size: var(--mdc-typography-subtitle1-font-size, variables.$font-size);

  @include mdc-feature-targeting.targets($feat-structure) {
    position: absolute;
    /* @noflip */
    left: 0;
    /* @noflip */
    -webkit-transform-origin: left top;
    /* @noflip */
    transform-origin: left top;
    line-height: 1.1rem;

    text-align: left;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: text;
    overflow: hidden;

    /* @alternate */
    // Force the label into its own layer to prevent visible layer promotion adjustments
    // when the ripple is activated behind it.
    will-change: transform;

    @include mdc-rtl-mixins.rtl {
      /* @noflip */
      right: 0;
      /* @noflip */
      left: auto;
      /* @noflip */
      -webkit-transform-origin: right top;
      /* @noflip */
      transform-origin: right top;
      /* @noflip */
      text-align: right;
    }
  }

  @include mdc-feature-targeting.targets($feat-animation) {
    transition: transform variables.$transition-duration mdc-animation-variables.$standard-curve-timing-function,
                color variables.$transition-duration mdc-animation-variables.$standard-curve-timing-function;
  }
}

@mixin float($positionY: variables.$position-y, $positionX: 0%, $scale: variables.$float-scale, $query: mdc-feature-targeting.all()) {
  $feat-structure: mdc-feature-targeting.create-target($query, structure);

  @include float-position($positionY, $positionX, $scale, $query: $query);

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

@mixin required($query: mdc-feature-targeting.all()) {
  &::after {
    @include _required-content($query);
  }

  @include mdc-rtl-mixins.rtl {
    &::after {
      @include _required-content-rtl($query);
    }
  }
}

@mixin ink-color($color, $query: mdc-feature-targeting.all()) {
  $feat-color: mdc-feature-targeting.create-target($query, color);

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

// Used for textarea in case of scrolling
@mixin fill-color($color, $query: mdc-feature-targeting.all()) {
  $feat-color: mdc-feature-targeting.create-target($query, color);

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

@mixin shake-keyframes(
  $modifier,
  $positionY,
  $positionX: 0%,
  $scale: variables.$float-scale,
  $query: mdc-feature-targeting.all()
) {
  $feat-animation: mdc-feature-targeting.create-target($query, animation);

  @include mdc-feature-targeting.targets($feat-animation) {
    @keyframes mdc-floating-label-shake-float-above-#{$modifier} {
      0% {
        @include mdc-rtl-mixins.ignore-next-line();
        transform: translateX(calc(0 - #{$positionX}))
          translateY(-#{$positionY}) 
          scale(#{$scale});
      }

      33% {
        animation-timing-function: cubic-bezier(0.5, 0, 0.701732, 0.495819);
        @include mdc-rtl-mixins.ignore-next-line();
        transform: translateX(calc(4% - #{$positionX}))
          translateY(-#{$positionY}) 
          scale(#{$scale});
      }

      66% {
        animation-timing-function: cubic-bezier(
          0.302435,
          0.381352,
          0.55,
          0.956352
        );
        @include mdc-rtl-mixins.ignore-next-line();
        transform: translateX(calc(-4% - #{$positionX}))
          translateY(-#{$positionY})
          scale(#{$scale});
      }

      100% {
        @include mdc-rtl-mixins.ignore-next-line();
        transform: translateX(calc(0 - #{$positionX}))
          translateY(-#{$positionY})
          scale(#{$scale});
      }
    }
  }
}

@mixin float-position(
  $positionY,
  $positionX: 0%,
  $scale: variables.$float-scale,
  $query: mdc-feature-targeting.all()
) {
  $feat-structure: mdc-feature-targeting.create-target($query, structure);

  @include mdc-feature-targeting.targets($feat-structure) {
    @if $positionX > 0 or $positionX < 0 {
      @include mdc-rtl-mixins.ignore-next-line();
      transform: translateY(-1 * $positionY)
        translateX(-1 * $positionX)
        scale($scale);

      @include mdc-rtl-mixins.rtl {
        @include mdc-rtl-mixins.ignore-next-line();
        transform: translateY(-1 * $positionY)
          translateX($positionX)
          scale($scale);
      }
    } @else {
      transform: translateY(-1 * $positionY) scale($scale);
    }
  }
}

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

  @include mdc-feature-targeting.targets($feat-animation) {
    animation: mdc-floating-label-shake-float-above-#{$modifier} 250ms 1;
  }
}

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

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

///
/// Sets the CSS transition for the floating animation.
///
/// @param {Number} $duration-ms - Duration (in ms) of the animation.
/// @param {String} $timing-function - Optionally overrides the default animation timing function.
///
@mixin float-transition(
  $duration-ms,
  $timing-function: mdc-animation-variables.$standard-curve-timing-function,
  $query: mdc-feature-targeting.all()
) {
  $feat-animation: mdc-feature-targeting.create-target($query, animation);

  @include mdc-feature-targeting.targets($feat-animation) {
    transition: color $duration-ms $timing-function,
      transform $duration-ms $timing-function;
  }
}

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

  @include mdc-feature-targeting.targets($feat-structure) {
    @include mdc-rtl-mixins.ignore-next-line();
    margin-left: 1px;
    @include mdc-rtl-mixins.ignore-next-line();
    margin-right: 0;
    content: '*';
  }
}

// Need to specify LTR/RTL manually since rtl mixins will add ::after[dir=rtl]
// selector and that breaks some browsers
@mixin _required-content-rtl($query: mdc-feature-targeting.all()) {
  $feat-structure: mdc-feature-targeting.create-target($query, structure);

  @include mdc-feature-targeting.targets($feat-structure) {
    @include mdc-rtl-mixins.ignore-next-line();
    margin-left: 0;
    @include mdc-rtl-mixins.ignore-next-line();
    margin-right: 1px;
  }
}
