@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-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 *;

/// Generates an animation that puffs an element in or out. It is recommended
/// that the `animation-stage()` mixin be used on the parent element.
///
/// @param {Bool | String} $in-or-out ['in'] - Determines if the element will
/// puff 'in' (`in`, `i`, or `true`) or 'out' (`out`, `o`, or `false`).
/// @param {String} $direction ['center'] - Will, depending on `$in-or-out`,
/// either puff in from this direction or puff out towards this direction.
/// Accepted values include: `center`, '`top`, `bottom`, `left`, `right`,
/// `top-left`, `top-right`, `bottom-left`, `bottom-right`, `horizontal`,
/// `vertical`, `forwards`, or `backwards`.
/// @param {Number} $duration [0.7s] - 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 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 [null] - The timing function for the
/// animation. A null value passed here will default to the recommended timing
/// function for this animation of `cubic-bezier(0.47, 0, 0.745, 0.715)` if
/// the animation is puffing in, and `cubic-bezier(0.165, 0.84, 0.44, 1)` if
/// the animation is puffing out.
/// @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-global-value
/// @require {function} is-number
/// @require {function} is-string
/// @require {function} is-list
/// @require {function} list-to-string
///
/// @throw Invalid $in-or-out value error.
/// @throw Invalid $duration value error.
/// @throw Invalid $delay value error.
/// @throw Invalid $iterations value error.
/// @throw Invalid $anim-dir value error.
@mixin puff(
  $in-or-out: 'in',
  $direction: 'center',
  $duration: 0.7s,
  $delay: null,
  $iterations: null,
  $anim-dir: null,
  $timing-func: null,
  $anim-num: null
) {
  // Default values for a puff in with center direction
  $opacity-val-1: 0;
  $opacity-val-2: 1;
  $trans-val-1: scale(2);
  $trans-val-2: scale(1);
  $blur-val-1: blur(4px);
  $blur-val-2: blur(0);
  $trans-origin-val: null;
  $temp-swap: null;
  $anim-suffix: '';

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

  @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($in-or-out) or $in-or-out == 'in' or $in-or-out == 'i' {
    $in-or-out: true;
    $anim-suffix: '-in-to';
  } @else if equals-false($in-or-out) or $in-or-out == 'out' {
    $in-or-out: false;
    $anim-suffix: '-out-from';
  } @else {
    @error 'Invalid $in-or-out value of #{meta.inspect($in-or-out)} for the ' +
        '[ puff() ] mixin. Value must be either `in` or `out`';
  }

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

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

    @if is-alias('top', $direction) {
      $direction: 'top';
      $trans-val-1: translateY(-50px);
      $trans-val-2: translateY(0);
      $trans-origin-val: 50% 0%;
    } @else if is-alias('bottom', $direction) {
      $direction: 'bottom';
      $trans-val-1: translateY(50px);
      $trans-val-2: translateY(0);
      $trans-origin-val: 50% 100%;
    } @else if is-alias('backwards', $direction) {
      $direction: 'back';
      $trans-val-1: translateZ(80px);
      $trans-val-2: translateZ(0);
    } @else if is-alias('forwards', $direction) {
      $direction: 'front';
      $trans-val-1: translateZ(-80px);
      $trans-val-2: translateZ(0);
    } @else if is-alias('left', $direction) {
      $direction: 'left';
      $trans-val-1: translateX(-50px);
      $trans-val-2: translateX(0);
      $trans-origin-val: 0% 50%;
    } @else if is-alias('right', $direction) {
      $direction: 'right';
      $trans-val-1: translateX(50px);
      $trans-val-2: translateX(0);
      $trans-origin-val: 100% 50%;
    } @else if is-alias('top left', $direction) {
      $direction: 'top-left';
      $trans-val-1: translateX(-50px) translateY(-50px);
      $trans-val-2: translateX(0) translateY(0);
      $trans-origin-val: 0% 0%;
    } @else if is-alias('top right', $direction) {
      $direction: 'top-right';
      $trans-val-1: translateX(50px) translateY(-50px);
      $trans-val-2: translateX(0) translateY(0);
      $trans-origin-val: 100% 0%;
    } @else if is-alias('bottom right', $direction) {
      $direction: 'bottom-right';
      $trans-val-1: translateX(50px) translateY(50px);
      $trans-val-2: translateX(0) translateY(0);
      $trans-origin-val: 100% 100%;
    } @else if is-alias('bottom left', $direction) {
      $direction: 'bottom-left';
      $trans-val-1: translateX(-50px) translateY(50px);
      $trans-val-2: translateX(0) translateY(0);
      $trans-origin-val: 0% 100%;
    } @else if is-alias('center', $direction) {
      $direction: 'center';
    } @else if is-alias('left-to-right', $direction) {
      $direction: 'horizontally';
      $trans-val-1: scaleX(2);
      $trans-val-2: scaleX(1);
      $anim-suffix: if($in-or-out, '-in', '-out');
    } @else if is-alias('top-to-bottom', $direction) {
      $direction: 'vertically';
      $trans-val-1: scaleY(2);
      $trans-val-2: scaleY(1);
      $anim-suffix: if($in-or-out, '-in', '-out');
    } @else {
      @error 'Invalid direction of `#{meta.inspect($direction)}` chosen for the ' +
          '[ puff() ] mixin. The $direction value must be one of the following: ' +
          '`center`, `top`, `top-right`, `right`, `bottom-right`, `bottom`, ' +
          '`bottom-left`, `left`, `top-left`, `forwards`, `backwards`, ' +
          '`horizontal`, or `vertical';
    }
  } @else {
    $direction: 'center';
  }

  @if not $duration {
    $duration: if($in-or-out, 0.7s, 1s);
  }

  @if not is-number($duration) {
    @error 'Invalid $duration value of `#{$duration}` for the [ puff() ] ' +
        '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 [ puff() ] mixin.' +
        'The duration property must be in either s or ms units.';
  } @else if math.unit($duration) == 'ms' and $duration % 100 == 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 [ puff() ] 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 [ puff() ] mixin.' +
          'The delay property must be in either s or ms units.';
    } @else if math.unit($delay) == 'ms' and $delay % 100 == 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 [ puff() ] mixin.';
    }
  }

  @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 #{meta.inspect($anim-dir)} ' +
        'passed with $anim-dir to the [ puff() ] mixin.';
  }

  @if equals-false($timing-func) or is-alias('normal', $timing-func) {
    $timing-func: if(
      $in-or-out,
      cubic-bezier(0.47, 0, 0.745, 0.715),
      cubic-bezier(0.165, 0.84, 0.44, 1)
    );
  }

  // If puffing out rather than puffing in, reverse the necessary values
  @if not $in-or-out {
    $opacity-val-1: 1;
    $opacity-val-2: 0;
    $blur-val-1: blur(0);
    $blur-val-2: blur(4px);

    $temp-swap: $trans-val-1;
    $trans-val-1: $trans-val-2;
    $trans-val-2: $temp-swap;
  }

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

  @at-root {
    @keyframes puff#{$anim-suffix} {
      0% {
        opacity: $opacity-val-1;
        filter: $blur-val-1;
        transform: $trans-val-1;

        @if $trans-origin-val {
          transform-origin: $trans-origin-val;
        }
      }

      100% {
        opacity: $opacity-val-2;
        filter: $blur-val-2;
        transform: $trans-val-2;

        @if $trans-origin-val {
          transform-origin: $trans-origin-val;
        }
      }
    }
  }
}
