@use 'sass:meta';
@use 'sass:string';
@use 'sass:list';
@use 'sass:math';
@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. The element this mixin
/// is applied to must have a front-face and a back-face child element.
/// It is recommended that you use this mixin in conjunction with the
/// `animation-front-face()` and 'animation-back-face()' mixins for element
/// `face` child elements and the 'animation-stage()` mixin for the parent
/// element.
///
/// @param {String} $direction ['top'] - The direction the scale effect causes
/// the element to seem to be moving towards. Accepted values are: `top`,
/// `top-right`, `right`, `bottom-right`, `bottom`, `bottom-left`, `left`,
/// `top-left`, `forwards`, `backwards`, and all valid aliases for these
/// $direction values.
/// @param {String} $axis ['horizontal'] - The axis the element will flip over.
/// Can be one of the following values: `horizontal`, `vertical`, `diagonal`, or
/// `diagonal2`. There are many alias values for these axis values. See the
/// `$map-alias-resolutions` variable in the `variables/_maps.scss` file for
/// all passable values.
/// @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
/// 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 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 [null] - The timing function for the
/// animation. A null value passed here will default to the recommended timing
/// function for this 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} 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
/// @require {variable} $map-alias-resolutions
///
/// @throw Invalid $direction value error.
/// @throw Invalid $axis 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(
  $direction: 'top',
  $axis: 'horizontal',
  $duration: 0.5s,
  $delay: null,
  $iterations: null,
  $anim-dir: null,
  $timing-func: null,
  $anim-num: null
) {
  // Default tranform values for $direction of `top` and $axis of `horizontal`
  $trans-val-1: rotateX(0);
  $trans-val-2: rotateX(180deg);
  $anim-suffix: '';

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

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

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

    @if is-alias('bottom', $direction) {
      $direction: 'bottom';
    } @else if is-alias('forwards', $direction) {
      $direction: 'forwards';
    } @else if is-alias('backwards', $direction) {
      $direction: 'backwards';
    } @else if is-alias('top', $direction) {
      $direction: 'top';
    } @else if is-alias('left', $direction) {
      $direction: 'left';
    } @else if is-alias('right', $direction) {
      $direction: 'right';
    } @else if is-alias('top left', $direction) {
      $direction: 'top-left';
    } @else if is-alias('top right', $direction) {
      $direction: 'top-right';
    } @else if is-alias('bottom right', $direction) {
      $direction: 'bottom-right';
    } @else if is-alias('bottom left', $direction) {
      $direction: 'bottom-left';
    } @else {
      @error 'Invalid direction of `#{meta.inspect($direction)}` chosen for ' +
          'the [ flip() ] mixin.';
    }
  } @else {
    @error 'You must provide a direction for the [ flip() ] mixin.';
  }

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

    @if is-alias('left-to-right', $axis) {
      $axis: 'horizontal';
      $anim-suffix: string.insert(
        $anim-suffix,
        '-horizontally',
        string.length($anim-suffix) + 1
      );
    } @else if is-alias('top-to-bottom', $axis) {
      $axis: 'vertical';
      $anim-suffix: string.insert(
        $anim-suffix,
        '-vertically',
        string.length($anim-suffix) + 1
      );
    } @else if is-alias('bottom-left-to-top-right', $axis) {
      $axis: 'diagonal1';
    } @else if is-alias('bottom-right-to-top-left', $axis) {
      $axis: 'diagonal2';
    } @else {
      @error 'Invalid axis of [ #{meta.inspect($axis)} ] chosen for the [ flip() ] mixin.';
    }
  } @else {
    @error 'You must choose an axis for the [ flip() ] mixin.';
  }

  @if $axis == 'diagonal1' or $axis == 'diagonal2' {
    $anim-suffix: string.insert(
      $anim-suffix,
      '-diagonally',
      string.length($anim-suffix) + 1
    );
  }

  @if $axis == 'horizontal' {
    @if $direction == 'bottom' {
      $trans-val-2: rotateX(-180deg);
    } @else if $direction == 'forwards' {
      $trans-val-1: translateZ(0) rotateX(0);
      $trans-val-2: translateZ(160px) rotateX(-180deg);
    } @else if $direction == 'backwards' {
      $trans-val-1: translateZ(0) rotateX(0);
      $trans-val-2: translateZ(-260px) rotateX(180deg);
    } @else if $direction != 'top' {
      @error 'Invalid $direction value of `#{meta.inspect($direction)}` with an ' +
          '$axis of `horizontal`. For a horizontal axis, you must choose a ' +
          'direction of either `top`, `bottom`, `backwards`, or `forwards`.';
    }
  } @else if $axis == 'vertical' {
    $trans-val-1: rotateY(0);
    $trans-val-2: rotateY(180deg);

    @if $direction == 'left' {
      $trans-val-2: rotateY(-180deg);
    } @else if $direction == 'forwards' {
      $trans-val-1: translateZ(0) rotateY(0);
      $trans-val-2: translateZ(160px) rotateY(180deg);
    } @else if $direction == 'backwards' {
      $trans-val-1: translateZ(0) rotateY(0);
      $trans-val-2: translateZ(160px) rotateY(180deg);
    } @else if $direction != 'right' {
      @error 'Invalid $direction value of #{meta.inspect($direction)} with ' +
          'an $axis of \'vertical\'. For a vertical axis, you must choose ' +
          'a $direction of either `left`, `right`, `backwards`, or `forwards`.';
    }
  } @else if $axis == 'diagonal1' {
    $trans-val-1: rotate3d(1, 1, 0, 0);
    $trans-val-2: rotate3d(1, 1, 0, 180deg);

    @if $direction == 'bottom-left' {
      $trans-val-2: rotate3d(1, 1, 0, -180deg);
    } @else if $direction == 'forwards' {
      $trans-val-1: translateZ(0) rotate3d(1, 1, 0, 0);
      $trans-val-2: translateZ(160px) rotate3d(1, 1, 0, 180deg);
    } @else if $direction == 'backwards' {
      $trans-val-1: translateZ(0) rotate3d(1, 1, 0, 0);
      $trans-val-2: translateZ(-260px) rotate3d(1, 1, 0, -180deg);
    } @else if $direction != 'top-right' {
      @error 'Invalid $direction value of `#{meta.inspect($direction)}` with ' +
      'an $axis of `diagonal1`. For the diagonal1 axis, you must choose a ' +
      'direction of either `top-right`, `bottom-left`, `backwards`, or `forwards`.';
    }
  } @else if $axis == 'diagonal2' {
    $trans-val-1: rotate3d(-1, 1, 0, 0);
    $trans-val-2: rotate3d(-1, 1, 0, -180deg);

    @if $direction == 'bottom-right' {
      $trans-val-2: rotate3d(-1, 1, 0, 180deg);
    } @else if $direction == 'forwards' {
      $trans-val-1: translateZ(0) rotate3d(-1, 1, 0, 0);
      $trans-val-2: translateZ(160px) rotate3d(-1, 1, 0, 180deg);
    } @else if $direction == 'backwards' {
      $trans-val-1: translateZ(0) rotate3d(-1, 1, 0, 0);
      $trans-val-2: translateZ(-260px) rotate3d(-1, 1, 0, -180deg);
    } @else if $direction != 'top-left' {
      @error 'Invalid direction value of `#{meta.inspect($direction)}` with ' +
          'an $axis of [ diagonal2 ]. For the diagonal2 axis, you must choose ' +
          'a direction of either `top-left`, `bottom-right`, `backwards`, or `forwards`.';
    }
  }

  @if not is-number($duration) {
    @error 'Invalid $duration value of `#{meta.inspect($duration)}` for the ' +
        '[ flip() ] 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 ' +
        '[ flip() ] 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;
  }

  @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() ] 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() ] 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 $iterations {
    @if is-string($iterations) {
      $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() ] 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() ] mixin.';
  }

  @if equals-false($timing-func) or is-alias('normal', $timing-func) {
    $timing-func: cubic-bezier(0.455, 0.03, 0.515, 0.955);
  }

  $anim-suffix: string.insert(
    $anim-suffix,
    '-#{$direction}',
    string.length($anim-suffix) + 1
  );

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

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

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

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