/**
 * Print out the animation property calculated with the passed arguments
 * This mixin support all the standard css animation properties like name, duration, delay, ease, direction and fill-mode.
 * The $animations argument will be parsed with the [../functions/_s-parse-properties.scss] function.
 * If you specify multiple animations at a time, this mixin will take care
 * of the delay calculation for each animations and make them play one after another
 *
 * @param 			{List} 				$animations 			The animations wanted like : coco .2s ease-in-out reverse
 *
 * @example
 * .my-cool-element {
 * 		@include s-animation(my-animation 1s ease-in-out, another-animation 4s);
 * 		// animation : my-animation 1s 0s ease-in-out 1 normal forwards, another-animation 4s 1s ease-in-out 1 normal forwards;
 * }
 *
 * @author 			Olivier Bossel <olivier.bossel@gmail.com> (https://olivierbossel.com)
 */
@mixin s-animation($animations...) {
  // loop on animations
  $anims: ();
  $current_delay: 0s;
  $current_duration: 0s;
  $previous_duration: 0s;
  @for $i from 1 through length($animations) {
    $animation: nth($animations, $i);
    $animation: s-parse-properties(
      $animation,
      (
        iteration:
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          infinite
          initial
          inherit,
        direction: normal reverse alternate alternate-reverse initial inherit,
        fill-mode: none forwards backwards both initial inherit,
        name: string,
        duration: second,
        delay: second,
        ease: string,
        parallel: true
      )
    );

    $name: map-get-or($animation, name);
    $duration: map-get-or($animation, duration, 1s);
    $delay: map-get-or($animation, delay, 0s);
    $ease: map-get-or($animation, ease, ease-in-out);
    $iteration: map-get-or($animation, iteration, 1);
    $direction: map-get-or($animation, direction, normal);
    $fill-mode: map-get-or($animation, fill-mode, forwards);
    $parallel: map-get-or($animation, parallel, false);

    // update delay
    $current_delay: $current_delay + $delay;

    // extend the animation if exist
    @extend %#{$name} !optional;

    // create the animations list
    $anims: append(
      $anims,
      unquote(
        "#{$name $duration $current_delay $ease $iteration $direction $fill-mode}"
      ),
      comma
    );

    // manage if animation is parallel with next one
    @if type-of($iteration) == number {
      @if $parallel == false {
        $current_delay: $current_delay + $duration * $iteration;
      }
    }
  }

  // set animation
  animation: $anims;
}
