@use 'sass:meta';
@use 'sass:string';
@use 'sass:list';
@use 'sass:math';
@use '../../functions/equals-false' as *;
@use '../../functions/equals-true' 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 by flickering it into or out of view.
///
/// @param {Bool | String} $in-or-out ['in'] - Determines whether the text will
/// flicker into or out of view. Can either be `in` or `out`.
/// @param {Number} $duration [3s] - 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 [linear] - The timing function for the
/// 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} equals-true
/// @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 $in-or-out value error.
/// @throw Invalid $duration value error.
/// @throw Invalid $delay value error.
/// @throw Invalid $anim-dir value error.
@mixin text-flicker(
  $in-or-out: 'in',
  $duration: 3s,
  $delay: null,
  $iterations: null,
  $anim-dir: null,
  $timing-func: linear,
  $anim-num: null
) {
  $anim-suffix: '';

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

  @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 equals-true($in-or-out) or $in-or-out == 'in' or $in-or-out == 'i' {
    $in-or-out: true;
    $anim-suffix: '-in';
  } @else if equals-false($in-or-out) or $in-or-out == 'out' {
    $in-or-out: false;
    $anim-suffix: '-out';
  } @else {
    @error 'Invalid in-out value of #{meta.inspect($in-or-out)} for the `text-flicer` mixin.';
  }

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

  @if not
    $timing-func or
    is-alias('none', $timing-func) or
    is-alias('default', $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: linear;
  }

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

  @at-root {
    @keyframes txt-flicker#{$anim-suffix} {
      @if $in-or-out {
        0% {
          opacity: 0;
        }

        10%,
        10.2%,
        20%,
        20.6%,
        30%,
        30.6%,
        45%,
        55.1%,
        57%,
        60.1%,
        65%,
        75.1%,
        77%,
        85.1%,
        86% {
          text-shadow: none;
          opacity: 0;
        }

        10.1% {
          text-shadow: none;
          opacity: 1;
        }

        20.1% {
          text-shadow: 0 0 30px hsla(0, 0%, 100%, 0.25);
          opacity: 1;
        }

        30.1%,
        30.5%,
        45.1%,
        50%,
        55% {
          text-shadow:
            0 0 30px hsla(0, 0%, 100%, 0.45),
            0 0 60px hsla(0, 0%, 100%, 0.25);
          opacity: 1;
        }

        57.1%,
        60% {
          text-shadow:
            0 0 30px hsla(0, 0%, 100%, 0.55),
            0 0 60px hsla(0, 0%, 100%, 0.35);
          opacity: 1;
        }

        65.1%,
        75% {
          text-shadow:
            0 0 30px hsla(0, 0%, 100%, 0.55),
            0 0 60px hsla(0, 0%, 100%, 0.35),
            0 0 100px hsla(0, 0%, 100%, 0.1);
          opacity: 1;
        }

        77.1%,
        85% {
          text-shadow:
            0 0 30px hsla(0, 0%, 100%, 0.25),
            0 0 60px hsla(0, 0%, 100%, 0.4),
            0 0 110px hsla(0, 0%, 100%, 0.2),
            0 0 100px hsla(0, 0%, 100%, 0.1);
          opacity: 1;
        }

        86.1%,
        100% {
          text-shadow:
            0 0 30px hsla(0, 0%, 100%, 0.6),
            0 0 60px hsla(0, 0%, 100%, 0.45),
            0 0 110px hsla(0, 0%, 100%, 0.25),
            0 0 100px hsla(0, 0%, 100%, 0.1);
          opacity: 1;
        }
      } @else {
        0%,
        13.9% {
          text-shadow:
            0 0 30px hsla(0, 0%, 100%, 0.6),
            0 0 60px hsla(0, 0%, 100%, 0.45),
            0 0 110px hsla(0, 0%, 100%, 0.25),
            0 0 100px hsla(0, 0%, 100%, 0.1);
          opacity: 1;
        }

        14%,
        14.9%,
        23%,
        24.9%,
        35%,
        39.9%,
        43%,
        44.9%,
        55%,
        69.4%,
        70%,
        79.4%,
        80%,
        89.8%,
        90% {
          text-shadow: none;
          opacity: 0;
        }

        15%,
        22.9% {
          text-shadow:
            0 0 30px hsla(0, 0%, 100%, 0.55),
            0 0 60px hsla(0, 0%, 100%, 0.4),
            0 0 110px hsla(0, 0%, 100%, 0.2),
            0 0 100px hsla(0, 0%, 100%, 0.1);
          opacity: 1;
        }

        25%,
        34.9% {
          text-shadow:
            0 0 30px hsla(0, 0%, 100%, 0.55),
            0 0 60px hsla(0, 0%, 100%, 0.35),
            0 0 100px hsla(0, 0%, 100%, 0.1);
          opacity: 1;
        }

        40%,
        42.9% {
          text-shadow:
            0 0 30px hsla(0, 0%, 100%, 0.55),
            0 0 60px hsla(0, 0%, 100%, 0.35);
          opacity: 1;
        }

        45%,
        50%,
        54.9%,
        69.5%,
        69.9% {
          text-shadow:
            0 0 30px hsla(0, 0%, 100%, 0.45),
            0 0 60px hsla(0, 0%, 100%, 0.25);
          opacity: 1;
        }

        79.9% {
          text-shadow: 0 0 30px hsla(0, 0%, 100%, 0.25);
          opacity: 1;
        }

        89.9% {
          text-shadow: none;
          opacity: 1;
        }

        100% {
          opacity: 0;
        }
      }
    }
  }
}
