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

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

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

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

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

  $diff: null;

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

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

  @return $diff;
}

// =diff( $terms... )
// -----------------------------------------------------------------------------
/// @alias subtract
///
/// @access public
/// @since 0.1.0

@function diff( $terms... )
{
  @return subtract($terms...);
}