@use 'sass:meta';
@use 'sass:list';
@use 'sass:math';
@use 'sass:string';
@use '../../functions/equals-false' as *;
@use '../../functions/equals-true' as *;
@use '../../functions/is-alias' as *;
@use '../../functions/is-global-value' as *;
@use '../../functions/is-falsey' 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 *;

/// Animates an element by rotating it along a point and/or an axis while either
/// scaling it up or down. For optimal effect, use the `animation-stage()` mixin
/// on the parent element, and a front and back child element within the rotated
/// element, with the `animation-front-face()` and `animation-back-face()` mixins
/// applied, respectively. This way, the element will have two distinct sides.
///
/// @param {Bool | String} $up-or-down ['up'] - Whether the elelment is
/// scaling 'up' (pass `up`, `top` or `true`) or 'down' (pass `down`. `bottom`,
/// or `false`).
/// @param {Number} $duration [0.4s] - The duration of the animation (`s` or `ms`).
/// @param {Number} $delay [null] - The optional delay time for the animation.
/// @param {String} $axis [null] - The animation can optionally rotate along an
/// axis line. This value can be `null` (or `false`, `no`, `n`), `horizontal` (or
/// `horiz`, `horz`, `hor`, `h`) or `vertical` (or `vert`, `ver`, `v`). It can
/// also be either `diagonal` (or `diag` `diagonal1`, `diag1`, `d`, `d1`), or
/// `diagonal2` (or `diag2`, `d2`).
/// @param {Number | String} $iterations [null] - The number of iterations
/// to complete. Sets the value for the `animation-iteration-count` property.
/// Can be `infinite`, any unitless number, or 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} equals-false
/// @require {function} equals-true
/// @require {function} is-alias
/// @require {function} is-falsey
/// @require {function} is-global-value
/// @require {function} is-list
/// @require {function} is-number
/// @require {function} is-string
/// @require {function} list-to-string
/// @require {mixin} only-moz
///
/// @throw Invalid $up-or-down value error.
/// @throw Invalid $axis value error.
/// @throw Invalid $duration value error.
/// @throw Invalid $delay value error.
/// @throw Invalid $anim-dir value error.
@mixin rotate-scale(
  $up-or-down: 'up',
  $duration: 0.7s,
  $delay: null,
  $axis: null,
  $iterations: null,
  $anim-dir: null,
  $timing-func: linear,
  $anim-num: null
) {
  // Values for the default $up-or-down value of 'up' with $axis of `null`
  $trans-val-1: scale(1) rotateZ(0);
  $trans-val-2: scale(2) rotateZ(180deg);
  $trans-val-3: scale(1) rotateZ(360deg);
  $is-on-axis: false;
  $is-diagonal: false;
  $anim-suffix: '';

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

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

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

  @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 $delay and is-string($delay) {
    $delay: string.to-lower-case($delay);
  }

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

  @if equals-true($up-or-down) or is-alias('top', $up-or-down) {
    $up-or-down: true;
    $anim-suffix: '-up';
  } @else if is-falsey($up-or-down) or is-alias('bottom', $up-or-down) {
    $up-or-down: false;
    $trans-val-2: scale(0.5) rotateZ(180deg);
    $anim-suffix: '-down';
  } @else {
    @error 'Invalid $up-or-down value of [#{meta.inspect($up-or-down)}] for ' +
        'the [ rotate-scale() ] mixin.';
  }

  @if $axis and not is-alias('none', $axis) and not is-falsey($axis) {
    $is-on-axis: true;

    @if is-alias('top-to-bottom', $axis) {
      $trans-val-1: if($up-or-down, scale(1) rotateY(0), scale(1) rotateY(0));
      $trans-val-2: if(
        $up-or-down,
        scale(2) rotateY(180deg),
        scale(0.5) rotateY(180deg)
      );
      $trans-val-3: if(
        $up-or-down,
        scale(1) rotateY(360deg),
        scale(1) rotateY(360deg)
      );
      $anim-suffix: string.insert(
        $anim-suffix,
        '-vertically',
        string.length($anim-suffix) + 1
      );
    } @else if is-alias('left-to-right', $axis) {
      $trans-val-1: if($up-or-down, scale(1) rotateX(0), scale(1) rotateX(0));
      $trans-val-2: if(
        $up-or-down,
        scale(2) rotateX(-180deg),
        scale(0.5) rotateX(-180deg)
      );
      $trans-val-3: if(
        $up-or-down,
        scale(1) rotateX(-360deg),
        scale(1) rotateX(-360deg)
      );
      $anim-suffix: string.insert(
        $anim-suffix,
        '-horizontally',
        string.length($anim-suffix) + 1
      );
    } @else if
      is-alias('bottom-left-to-top-right', $axis) or
      is-alias('bottom-right-to-top-left', $axis)
    {
      $is-diagonal: true;
      $anim-suffix: string.insert(
        $anim-suffix,
        '-diagonally',
        string.length($anim-suffix) + 1
      );

      @if is-alias('bottom-right-to-top-left', $axis) {
        // diagonal-2 values
        $trans-val-1: if(
          $up-or-down,
          scale(1) rotate3d(-1, 1, 0, 0),
          scale(1) rotate3d(-1, 1, 0, 0)
        );
        $trans-val-2: if(
          $up-or-down,
          scale(2) rotate3d(-1, 1, 0, 180deg),
          scale(0.5) rotate3d(-1, 1, 0, 180deg)
        );
        $trans-val-3: if(
          $up-or-down,
          scale(1) rotate3d(-1, 1, 0, 360deg),
          scale(1) rotate3d(-1, 1, 0, 360deg)
        );
      } @else {
        // diagonal-1 values
        $trans-val-1: if(
          $up-or-down,
          scale(1) rotate3d(1, 1, 0, 0),
          scale(1) rotate3d(1, 1, 0, 0)
        );
        $trans-val-2: if(
          $up-or-down,
          scale(2) rotate3d(1, 1, 0, -180deg),
          scale(0.5) rotate3d(1, 1, 0, -180deg)
        );
        $trans-val-3: if(
          $up-or-down,
          scale(1) rotate3d(1, 1, 0, -360deg),
          scale(1) rotate3d(1, 1, 0, -360deg)
        );
      }
    } @else {
      @error 'Invalid $axis value of `#{meta.inspect($axis)}` for the ' +
          '[ rotate-scale() ] mixin. Value of $axis must be one of the following: ' +
          '`horizontal`, `vertical`, `diagonal1`, or `diagonal2`';
    }
  }

  @if not is-number($duration) {
    @error 'Invalid $duration value of `#{meta.inspect($duration)}` for the ' +
        '[ rotate-scale() ] 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 `#{meta.inspect($duration)}` for the ' +
        '[ rotate-scale() ] 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
      $delay == 0s or
      $delay == 0ms or
      is-alias('none', $delay)
  {
    $delay: null;
  }

  @if $delay {
    @if not is-number($delay) {
      @error 'Invalid delay value of `#{meta.inspect($delay)}` for the ' +
          '[ rotate-scale() ] 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 ' +
          '[ rotate-scale() ] mixin. The delay property must be in either ' +
          's or ms units.';
    } @else if $delay % 10 == 0ms {
      $delay: math.div($delay, 1000ms) * 1s;

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

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

    @if is-alias('infinite', $iterations) {
      $iterations: infinite;
    } @else if not (is-number($iterations) and math.is-unitless($iterations)) and
        not is-global-value($iterations)
    {
      @error 'Invalid $iterations value of `#{meta.inspect($iterations)}` ' +
          'for the [ rotate-scale() ] 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 $anim-dir value of #{meta.inspect($anim-dir)} for the ' +
        '[ rotate-scale() ] mixin.';
  }

  @if equals-false($timing-func) or is-alias('normal', $timing-func) {
    $timing-func: linear;
  }

  @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);
  animation: rotate-scale#{$anim-suffix} #{$duration} #{$timing-func} #{$delay}
    #{$iterations} #{$anim-dir} both;

  @if $is-on-axis {
    transform-style: preserve-3d;

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

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

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

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