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

@import "aleksi/general/throw";
@import "aleksi/lists/prepend";
@import "aleksi/maps/is-map";

// =map-filter-keys( $map, $test, $args... )
// -----------------------------------------------------------------------------
/// Removes items in a map with a key that doesn't pass a given test function.
/// **Note**: to include a key in the result, the test function must return a
/// truethy value — not per-se the boolean `true`.
///
/// @param {map} $map - The map with the item keys to test.
/// @param {string} $test - The name of the test function to run on each item's key.
/// @param {arglist} $args... - Additional arguments for `$test`.
///
/// @return {map} - `$map` without the items that didn't pass the `$test` function.
///
/// @example scss
///   $foo: map-filter( ('foo': 10, 'bar': 5px, 'baz': 7, 'wiz' 3), 'str-index', 'b');
///     // => ('bar': 5px, 'baz': 7)
///
/// @access public
/// @since 0.1.0

@function map-filter-keys( $map, $test, $args... )
{
  @if not is-map($map) {
    @return throw-error('map-filter():: $map must be a map, was #{inspect($map)}.');
  }

  $res: ();

  @if type-of($test) == 'string' {
    $test: get-function($test);
  }

  @each $key, $val in $map
  {
    $test-args: prepend($args, $key);

    @if call($test, $test-args...) {
      $res: map-merge($res, ($key: $val));
    }
  }

  @return $res;
}