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

/// Animates text from a blurred to a focused state. If a `forwards` or
/// `backwards` $direction is chosen to augment the animation, it is recommended
/// that the `animation-stage()` mixin be used on the parent element.
///
/// @param {String| Bool} $style ['default'] - The style of the focus-in effect.
/// Can pass nothing, `null`, `false`, `none`, `normal`, or `default` for the
/// default text-focus-in animation. Available styles to modify the animation
/// are: `expand` (or `grow`), `forwards` (or `forward`, `fwd`), `contract` (or
/// `shrink`), and `backwards` (or `backwards`, `back`, `bck`).
/// @param {Number} $duration [0.9s] - 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. If no specific function is passed, the mixin will choose the
/// best custom timing function for the given style.
/// @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-color
/// @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 $style value error.
/// @throw Invalid $duration value error.
/// @throw Invalid $delay value error.
/// @throw Invalid $anim-dir value error.
@mixin text-focus-in(
  $style: 'default',
  $duration: 0.9s,
  $delay: null,
  $iterations: null,
  $anim-dir: null,
  $timing-func: null,
  $anim-num: null
) {
  $anim-suffix: '';

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

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

  @if is-alias('contract', $style) {
    $style: 'contract';
    $anim-suffix: '-contract';
  } @else if is-alias('expand', $style) {
    $style: 'expand';
    $anim-suffix: '-expand';
  } @else if is-alias('forwards', $style) {
    $style: 'forwards';
    $anim-suffix: '-expand-forwards';
  } @else if is-alias('backwards', $style) {
    $style: 'backwards';
    $anim-suffix: '-contract-backwards';
  } @else if equals-false($style) or
      is-alias('normal', $style) or
      is-alias('none', $style)
  {
    $style: null;
  } @else {
    @error 'Invalid $style value of `#{meta.inspect($style)}` used for ' +
            'the [ text-focus-in() ] mixin. Available $style keywords ' +
            'for this mixin are `default` (or pass `none` `null` or nothing), ' +
            '`expand`, `forwards`, `contract`, and `backwards`';
  }

  @if not $timing-func {
    $timing-func: if(
      $style,
      cubic-bezier(0.25, 0.46, 0.45, 0.94),
      cubic-bezier(0.55, 0.085, 0.68, 0.53)
    );
  }

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

  @if $style == 'forwards' or $style == 'backwards' {
    transform: translate3d(0, 0, 0);
    transform-style: preserve-3d;
  }

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

  @at-root {
    @keyframes txt-focus-in#{$anim-suffix} {
      0% {
        opacity: 0;
        filter: blur(12px);

        @if $style == 'expand' {
          letter-spacing: -0.5em;
        } @else if $style == 'forwards' {
          letter-spacing: -0.5em;
          transform: translateZ(-800px);
        } @else if $style == 'contract' {
          letter-spacing: 1em;
        } @else if $style == 'backwards' {
          letter-spacing: 1em;
          transform: translateZ(300px);
        }
      }

      100% {
        opacity: 1;
        filter: blur(0);

        @if $style == 'forwards' {
          transform: translateZ(0);
        } @else if $style == 'backwards' {
          transform: translateZ(12px);
        }
      }
    }
  }
}
