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

/// Adds a wobble animation to a given element
///
/// @param {String} $direction ['bottom'] - The base from which the wobble is
/// generated. Takes 'top`, `left`, `right`, or `bottom`.
/// @param {Number} $duration [0.8s] - 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. The default of null will set it to the default timing
/// function of ease.
/// @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 Incorrect keyword value for $direction error.
/// @throw Invalid data type for $duration error.
/// @throw Invalid data type for $delay error.
/// @throw Invalid data type for $iterations error.
/// @throw Invalid keyword value for $anim-dir error.
@mixin wobble(
  $direction: 'bottom',
  $duration: 0.8s,
  $delay: null,
  $iterations: null,
  $anim-dir: null,
  $timing-func: null,
  $anim-num: null
) {
  // Default $direction values for bottom $direction
  $dir-val1: translateX(0);
  $dir-val2: translateX(-30px) rotate(-6deg);
  $dir-val3: translateX(15px) rotate(6deg);
  $dir-val4: translateX(-15px) rotate(-3.6deg);
  $dir-val5: translateX(9px) rotate(2.4deg);
  $dir-val6: translateX(-6px) rotate(-1.2deg);
  $anim-suffix: '-from-bottom';

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

  @if is-alias('top', $direction) {
    $dir-val1: translateX(0);
    $dir-val2: translateX(-30px) rotate(6deg);
    $dir-val3: translateX(15px) rotate(-6deg);
    $dir-val4: translateX(-15px) rotate(3.6deg);
    $dir-val5: translateX(9px) rotate(-2.4deg);
    $dir-val6: translateX(-6px) rotate(1.2deg);
    $anim-suffix: '-from-top';
  } @else if is-alias('left', $direction) {
    $dir-val1: translateY(0) rotate(0);
    $dir-val2: translateY(-30px) rotate(-6deg);
    $dir-val3: translateY(15px) rotate(6deg);
    $dir-val4: translateY(-15px) rotate(-3.6deg);
    $dir-val5: translateY(9px) rotate(2.4deg);
    $dir-val6: translateY(-6px) rotate(-1.2deg);
    $anim-suffix: '-from-left';
  } @else if is-alias('right', $direction) {
    $dir-val1: translateY(0) rotate(0);
    $dir-val2: translateY(-30px) rotate(6deg);
    $dir-val3: translateY(15px) rotate(-6deg);
    $dir-val4: translateY(-15px) rotate(3.6deg);
    $dir-val5: translateY(9px) rotate(-2.4deg);
    $dir-val6: translateY(-6px) rotate(1.2deg);
    $anim-suffix: '-from-right';
  } @else if not is-alias('bottom', $direction) {
    @error 'Invalid $direction of `#{meta.inspect($direction)}` for the ' +
      '[ wobble() ] mixin. Valid directions include the following: ' +
      '`top`, `right`, `bottom`, or `left`.';
  }

  @if not is-number($duration) {
    @error 'Invalid $duration value of `#{meta.inspect($duration)}` for the ' +
        '[ wobble() ] 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 ' +
        '[ wobble() ] mixin. The duration property must be in either ' +
        's (seconds) or ms (milliseconds) 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 [ wobble() ] ' +
          '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 [ wobble() ] ' +
          '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 [ wobble() ] 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 wobble` mixin.';
  }

  @if not
    $timing-func or
    $timing-func ==
    'ease' or
    is-alias('none', $timing-func) or
    is-alias('normal', $timing-func)
  {
    // Take all potential values the user might pass to keep the default setting
    // and ensure that the timing function remains at that default
    $timing-func: null;
  }

  @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
    );
  }

  animation: wobble#{$anim-suffix} #{$duration} #{$timing-func} #{$delay}
    #{$iterations} #{$anim-dir} both;

  @at-root {
    @keyframes wobble#{$anim-suffix} {
      0%,
      100% {
        transform: #{$dir-val1};
        transform-origin: 50% 50%;
      }

      15% {
        transform: #{$dir-val2};
      }

      30% {
        transform: #{$dir-val3};
      }

      45% {
        transform: #{$dir-val4};
      }

      60% {
        transform: #{$dir-val5};
      }

      75% {
        transform: #{$dir-val6};
      }
    }
  }
}

/// Adds a wobble animation to a given element
///
/// @param {String} $direction ['bottom'] - The base from which the wobble is
/// generated. Takes 'top`, `left`, `right`, or `bottom`.
/// @param {Number} $duration [0.8s] - 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. The default of null will set it to the default timing
/// function of ease.
/// @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 Incorrect keyword value for $direction error.
/// @throw Invalid data type for $duration error.
/// @throw Invalid data type for $delay error.
/// @throw Invalid data type for $iterations error.
/// @throw Invalid keyword value for $anim-dir error.
///
/// @alias wobble
@mixin wobbliness(
  $direction: 'bottom',
  $duration: 0.8s,
  $delay: null,
  $iterations: null,
  $anim-dir: null,
  $timing-func: null,
  $anim-num: null
) {
  @include wobble(
    $direction,
    $duration,
    $delay,
    $iterations,
    $anim-dir,
    $timing-func,
    $anim-num
  );
}
