@use 'sass:list';
@use 'sass:math';
@use 'sass:meta';
@use 'sass:string';
@use '../../functions/equals-false' as *;
@use '../../functions/is-alias' as *;
@use '../../functions/is-falsey' as *;
@use '../../functions/is-global-value' as *;
@use '../../functions/is-list' as *;
@use '../../functions/is-number' as *;
@use '../../functions/is-string' as *;
@use '../../functions/list-to-string' as *;
@use '../utilities/only-moz' as *;

/// Generates an animation that flips an element over while scaling it. The
/// effect is slightly different than in `flip-scale`. The element this mixin is
/// applied to must have a front-side and a back-side child element. It is
/// recommended that you use this mixin in conjunction with the
/// `animation-front-face()` and 'animation-back-face()' mixins for card 'face'
/// child elements and the 'animation-stage()` mixin for the parent element.
///
/// @param {String} $direction ['top'] - The direction the animation appears to
/// be fliping from. Can be either `top`, `bottom`, `right`, or `left`.
/// @param {Number} $duration [0.5s] - The duration of the animation (`s` or `ms`)
/// @param {Number} $delay [null] - The optional delay time for the animation.
/// @param {Number | String} $iterations [null] - The number of iterations
/// for the animation to complete. Can be `infinite`, `inherit`, `initial`, or
/// any unitless number. Can also be any CSS global value.
/// @param {String} $anim-dir [null] - Sets the value for the
/// `animation-direction` property. Can be `normal`, `reverse`, `alternate`, or
/// `alternate-reverse`. Can also be any CSS global value. Default of `null`
/// assumes the CSS property default of `normal`.
/// @param {Timing-Function} $timing-func [linear] - The timing function for the
/// animation.
/// @param {Number | String} $anim-num [null] - If the mixin is used more than
/// once with different values in a stylesheet, you can pass a number or string
/// to the mixin here that gets appeneded to end of the animation name so that
/// the animations do not overwrite each other.
///
/// @group Animations
/// @require {function} is-alias
/// @require {function} is-list
/// @require {function} is-global-value
/// @require {function} is-number
/// @require {function} is-string
/// @require {function} list-to-string
/// @require {mixin} only-moz
///
/// @throw Invalid $direction value error.
/// @throw Invalid $duration value error.
/// @throw Invalid $delay value error.
/// @throw Invalid $iterations value error.
/// @throw Invalid $anim-dir value error.
@mixin flip-scale-2(
  $direction: 'top',
  $axis: 'horizontal',
  $duration: 0.5s,
  $delay: null,
  $iterations: null,
  $anim-dir: null,
  $timing-func: linear,
  $anim-num: null
) {
  // Default tranform values for $direction of `top`
  $trans-val-1: translateY(0) rotateX(0) scale(1); // Same for top and bottom
  $trans-origin-val-1: 50% 0%;
  $trans-val-2: translateY(-50%) rotateX(-90deg) scale(2);
  $trans-origin-val-2: 50% 50%; // Same for all $direction values
  $trans-val-3: translateY(-100%) rotateX(-180deg) scale(1);
  $trans-origin-val-3: 50% 100%;
  $anim-suffix: '-towards-top';

  @if $direction {
    @if is-list($direction) {
      $direction: list-to-string($direction);
    }

    @if is-string($direction) {
      $direction: string.to-lower-case($direction);
    }
  }

  @if $delay and is-string($delay) {
    $delay: string.to-lower-case($delay);
  }

  @if $anim-dir {
    @if is-list($anim-dir) {
      $anim-dir: list-to-string($anim-dir);
    }

    @if is-string($anim-dir) {
      $anim-dir: string.to-lower-case($anim-dir);
    }
  }

  @if is-alias('right', $direction) {
    $trans-val-1: translateX(0) rotateY(0) scale(1);
    $trans-origin-val-1: 100% 50%;
    $trans-val-2: translateX(50%) rotateY(-90deg) scale(2);
    $trans-val-3: translateX(100%) rotateY(-180deg) scale(1);
    $trans-origin-val-3: 0% 50%;
    $anim-suffix: '-from-right';
  } @else if is-alias('left', $direction) {
    $trans-val-1: translateX(0) rotateY(0) scale(1);
    $trans-origin-val-1: 0% 50%;
    $trans-val-2: translateX(-50%) rotateY(90deg) scale(2);
    $trans-val-3: translateX(-100%) rotateY(180deg) scale(1);
    $trans-origin-val-3: 100% 50%;
    $anim-suffix: '-from-left';
  } @else if is-alias('bottom', $direction) {
    $trans-origin-val-1: 50% 100%;
    $trans-val-2: translateY(50%) rotateX(90deg) scale(2);
    $trans-val-3: translateY(100%) rotateX(180deg) scale(1);
    $trans-origin-val-3: 50% 0%;
    $anim-suffix: '-towards-bottom';
  } @else if not is-alias('top', $direction) {
    @error 'Invalid direction value of `#{meta.inspect($direction)}` for the ' +
        '[ flip-scale-2() ] mixin.';
  }

  @if not is-number($duration) {
    @error 'Invalid $duration value of `#{$duration}` for the [ flip-scale-2() ] ' +
        'mixin. The duration property must be a number.';
  } @else if math.is-unitless($duration) {
    // If no there is no unit for $duration, unless $duration > 60, assume seconds
    $duration: if($duration > 60, $duration * 1ms, $duration * 1s);
  } @else if not list.index('s' 'ms', math.unit($duration)) {
    @error 'Invalid $duration value of `#{$duration}` for the [ flip-scale-2() ] ' +
        'mixin. The duration property must be in either s or ms units.';
  } @else if $duration % 10 == 0ms {
    // If time in ms would be shorter as seconds, convert it
    $duration: math.div($duration, 1000ms) * 1s;

    // Get rid of possible leading zero in front of decimal place
    @if string.slice($duration + '', 1, 2) == '0.' {
      $duration: #{string.slice($duration + '', 2)};
    }
  }

  @if is-falsey($delay) or
      is-alias('none', $delay) or
      $delay == 0s or $delay == 0ms
  {
    $delay: null;
  }

  @if $delay {
    @if not is-number($delay) {
      @error 'Invalid delay value of `#{meta.inspect($delay)}` for the flip-scale-2` ' +
          'mixin. The delay property must be a number.';
    } @else if math.is-unitless($delay) {
      // If no there is no unit for $delay, unless $delay > 60, assume seconds
      $delay: if($delay > 60, $delay * 1ms, $delay * 1s);
    } @else if not list.index('s' 'ms', math.unit($delay)) {
      @error 'Invalid delay value of `#{meta.inspect($delay)}` for the [ flip-scale-2() ] ' +
          'mixin. The delay property must be in either s or ms units.';
    } @else if $delay % 10 == 0ms {
      // If time in ms would be shorter as seconds, convert it
      $delay: math.div($delay, 1000ms) * 1s;

      @if string.slice($delay + '', 1, 2) == '0.' {
        $delay: #{string.slice($delay + '', 2)};
      }
    }
  }

  @if $iterations {
    @if is-string('string') {
      $iterations: string.to-lower-case($iterations);
    }

    @if is-alias('infinite', $iterations) {
      $iterations: infinite;
    } @else if (
      is-number($iterations) and math.is-unitless($iterations)) or
      is-global-value($iterations)
    {
      $iterations: $iterations;
    } @else {
      @error 'Invalid $iterations value of `#{meta.inspect($iterations)}` for the ' +
          '[ flip-scale-2() ] mixin.';
    }
  } @else {
    $iterations: null;
  }

  @if is-falsey($anim-dir) or
      is-alias('none', $anim-dir) or
      is-alias('normal', $anim-dir)
  {
    $anim-dir: null;
  } @else if is-alias('reverse', $anim-dir) {
    $anim-dir: reverse;
  } @else if is-alias('alternate', $anim-dir) {
    $anim-dir: alternate;
  } @else if is-alias('alternate-reverse', $anim-dir) {
    $anim-dir: alternate-reverse;
  } @else if not is-global-value($anim-dir) {
    @error 'Invalid animation-direction value of `#{$anim-dir}` for the ' +
        '[ flip-scale-2() ] mixin.';
  }

  @if equals-false($anim-num) or $anim-num == '' {
    $anim-num: null;
  } @else {
    $anim-suffix: string.insert(
      $anim-suffix,
      '--#{$anim-num}',
      string.length($anim-suffix) + 1
    );
  }

  transform: translate3d(0, 0, 0);
  transform-style: preserve-3d;
  animation: flip-scale-2#{$anim-suffix} #{$duration} #{$timing-func} #{$delay}
    #{$iterations} #{$anim-dir} both;

  @include only-moz {
    backface-visibility: hidden;
  }

  @at-root {
    @keyframes flip-scale-2#{$anim-suffix} {
      0% {
        transform: $trans-val-1;
        transform-origin: $trans-origin-val-1;
      }

      50% {
        transform: $trans-val-2;
        transform-origin: $trans-origin-val-2;
      }

      100% {
        transform: $trans-val-3;
        transform-origin: $trans-origin-val-3;
      }
    }
  }
}
