// =============================================================================
// =ALEKSI - MAP-REMOVE-NTH
// =============================================================================
////
//// @group aleksi-maps
//// @author [Yoannis Jamar](https://yoannis.com)

@import "aleksi/general/throw";

// =map-remove-nth( $map, $index )
// -----------------------------------------------------------------------------
/// Removes the nth tuple from a given map.
///
/// @param {map} $map - The map from which to remove the tuple
/// @param {number} $index - The index of the tuple to remove
///
/// @return {map} - The map without the tuple that was at $index
///
/// @throw Error if `$map` is not a map
/// @throw Error if `$index` is out of bounds for $map
///
/// @example scss
///   $foo: map-remove-nth(('a': 'foo', 'b': 'bar'), 2);
///     // => ('a': 'foo')
///   $foo: map-remove-nth(('a': 'foo', 'b': 'bar'), 3);
///     // => ('a': 'foo', 'b': 'bar')
///
/// @access public
/// @since 0.4.0

@function map-remove-nth($map, $index)
{
  @if type-of($map) != 'map' {
    @return throw-error('map-remove-nth(): $map must be a map – was #{inspect($map)}');
  }

  @if type-of($index) != 'number' {
    @return throw-error('map-remove-nth(): $index must be a number – was #{inspect($index)}');
  }

  $res: ();
  $i: 0;

  @each $key, $val in $map
  {
    $i: $i + 1;
  
    @if $i != $index {
      $res: map-merge($res, ('#{$key}': $val));
    }
  }

  @return $res;
}