@use 'sass:meta';
@use 'sass:list';
@use 'sass:math';
@use 'sass:string';
@use '../../functions/equals-false' 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 *;

/// Animates an element by scaling it down.
///
/// @param {String} $direction ['center'] - The direction from which the element
/// will appear to be scaling down. Accepted values are `center`, `top`,
/// `top-right,` `right`, `bottom-right`, `bottom`, `bottom-left`, `left,
/// `top-left`, and all valid aliases for these $direction values.
/// @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 start 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`). Along
/// the horizontal axis, only `left`, `right`, or `center` $direction values are
/// valid. Along the vertical axis, only `top`, `bottom`, and `center`
/// $direction values are valid.
/// @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. Default value of null will apply the recommended function for
/// this animation of `cubic-bezier(0.25, 0.46, 0.45, 0.94)`.
/// @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-falsey
/// @require {function} is-global-value
/// @require {function} is-list
/// @require {function} is-number
/// @require {function} is-string
/// @require {function} list-to-string
///
/// @throw Invalid $direction value error.
/// @throw Invalid $axis value error.
/// @throw Invalid $duration value error.
/// @throw Invalid $delay value error.
/// @throw Invalid $anim-dir value error.
@mixin scale-down(
  $direction: 'center',
  $duration: 0.4s,
  $delay: null,
  $axis: null,
  $iterations: null,
  $anim-dir: null,
  $timing-func: null,
  $anim-num: null
) {
  // Default value for the 'center' direction
  $trans-val-1: scale(1);
  $trans-val-2: scale(0.5);
  $trans-origin-val: null;
  $anim-suffix: '-to';

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

  @if is-alias('top', $direction) {
    $trans-origin-val: 50% 0%;
    $direction: 'top';
  } @else if is-alias('top right', $direction) {
    $trans-origin-val: 100% 0;
    $direction: 'top-right';
  } @else if is-alias('right', $direction) {
    $trans-origin-val: 100% 50%;
    $direction: 'right';
  } @else if is-alias('bottom right', $direction) {
    $trans-origin-val: 100% 100%;
    $direction: 'bottom-right';
  } @else if is-alias('bottom', $direction) {
    $trans-origin-val: 50% 100%;
    $direction: 'bottom';
  } @else if is-alias('bottom left', $direction) {
    $trans-origin-val: 0% 100%;
    $direction: 'bottom-left';
  } @else if is-alias('left', $direction) {
    $trans-origin-val: 0% 50%;
    $direction: 'left';
  } @else if is-alias('top left', $direction) {
    $trans-origin-val: 0% 0%;
    $direction: 'top-left';
  } @else if is-alias('center', $direction) {
    $direction: 'center';
  } @else {
    @error 'Invalid $direction value of #{meta.inspect($direction)} for the ' +
        '[ scale-down() ] mixin. The value of $direction must be one of the ' +
        'following: `top`, `top-right`, `right`, `bottom-right`, `bottom`' +
        '`bottom-left`, `left`, `top-left`, or `center`.';
  }

  @if $axis and not is-alias('none', $axis) {
    @if is-alias('top-to-bottom', $axis) {
      $trans-val-1: scaleY(1);
      $trans-val-2: scaleY(0.3);
      $anim-suffix: '-vertically-to';

      @if $direction !=
        'top' and
        $direction !=
        'bottom' and
        $direction !=
        'center'
      {
        @error 'You have chosen an axis value of `vertical` that cannot be ' +
            'used with the direction value of `#{meta.inspect($direction)}` ' +
            'for the [ scale-down() ] mixin. A vertical axis must have direction ' +
            'value of either `top`, `bottom`, or `center`.';
      } @else if $direction == 'top' {
        $trans-origin-val: 100% 0%;
      } @else if $direction == 'bottom' {
        $trans-origin-val: 0% 100%;
      }
    } @else if is-alias('left-to-right', $axis) {
      $trans-val-1: scaleX(1);
      $trans-val-2: scaleX(0.3);
      $anim-suffix: '-horizontally-to';

      @if $direction !=
        'left' and
        $direction !=
        'right' and
        $direction !=
        'center'
      {
        @error 'You have chosen an axis value of `horizontal` that cannot be ' +
            'used with the direction value of `#{meta.inspect($direction)}` for the ' +
            '[ scale-down() ] mixin. A horizontal axis must have direction value of ' +
            'either `left`, `right`, or `center`.';
      } @else if $direction == 'left' {
        $trans-origin-val: 0% 0%;
      } @else if $direction == 'right' {
        $trans-origin-val: 100% 100%;
      }
    } @else {
      @error 'Invalid $axis value of `#{meta.inspect($axis)} for the ' +
          '[ scale-down() ] mixin.';
    }
  }

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

  @if equals-false($timing-func) or is-alias('normal', $timing-func) {
    $timing-func: cubic-bezier(0.25, 0.46, 0.45, 0.94);
  }

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

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

        @if $direction != 'center' {
          transform-origin: $trans-origin-val;
        }
      }

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

        @if $direction != 'center' {
          transform-origin: $trans-origin-val;
        }
      }
    }
  }
}
