// =============================================================================
// =ALEKSI - ADD
// =============================================================================
////
//// @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";

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

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

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

  $sum: null;

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

    @else {
      $sum: $sum + default-to($term, 0);
    }
  }

  @return $sum;
}

// =sum( $terms... )
// -----------------------------------------------------------------------------
/// @alias add
///
/// @access public
/// @since 0.1.0

@function sum( $terms... )
{
  @return add($terms...);
}