// =============================================================================
// =ALEKSI - MULTIPLY
// =============================================================================
////
//// @group aleksi-math
//// @author [Yoannis Jamar](http://yoannis.me)

@import "aleksi/general/throw";
@import "aleksi/general/is-of-type";
@import "aleksi/general/default-to";
@import "aleksi/lists/every";
@import "aleksi/lists/slice";

// =multiply( $terms... )
// -----------------------------------------------------------------------------
/// Calculates the product between two or more numbers. Usefull when relying on
/// `call()`, `walk()` or `apply()` for mathematical operations.
/// **Note**: passing more then 2 arguments will multiply them successively.
/// **Note**: if one of the terms is not a number, it will return `null`.
///
/// @param {arglist} $terms - The operators in the multiplication.
///
/// @return {number|null} - The product resulting from multiplying `$terms`.
/// @throw Warning if one of the terms is not a number.
///
/// @example scss
///   $foo: multiply(10, 5);
///     // => 50
///   $bar: multiply(10, 'hello')
///     // => null
///   $baz: multiply(10 5, 3)
///     // => null
///   $wiz: multiply(10, 5, 3)
///     // => 150
///
/// @access public
/// @since 0.1.0

@function multiply( $terms... )
{
  @if not every($terms, 'is-of-type', 'number' 'null') {
    @return throw-warning('multiply():: all terms in `#{inspect($terms)}` must be numbers — returning null.');
  }

  @if length($terms) < 2 {
    @return thow-error('multiply():: wrong number of arguments, 1 instead of 2 or more.');
  }

  $prod: null;

  @each $term in $terms
  {
    @if type-of($prod) == 'null' {
      $prod: $term;
    }

    @else {
      $prod: $prod * default-to($term, 1);
    }
  }

  @return $prod;
}

// =mult( $terms... )
// -----------------------------------------------------------------------------
/// @alias multiply
///
/// @access public
/// @since 0.1.0

@function mult( $terms... )
{
  @return multiply( $terms... );
}