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

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

// =map-has-deep-key($map, $keys...)
// -----------------------------------------------------------------------------
/// Tests if a map has all given keys nested within each other.
/// @author [Hugo Giraudel](http://hugogiraudel.com)
///
/// @param {map} $map - The map to search in for `$keys...`.
/// @param {arglist} $keys - The keys to search for.
///
/// @return {bool} - Whether `$map` has all `$keys` nested within each other.
/// @throw Error if `$map` is not a map.
///
/// @example scss
///   $foo: map-has-deep-keys( ('a': 'foo', 'b': ('ba': 'bar', 'bb': 'baz')), 'a', 'b');
///     // => true
///   $bar: map-has-deep-keys( 'a': 'foo', 'b': 'bar', 'a', 'b')
///
/// @access public
/// @since 0.1.0

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

  // allow passing a list of keys as second argument
  @if length($keys) == 1 {
    $keys: nth($keys, 1);
  }

  @each $key in $keys
  {
    @if not map-has-key($map, $key) {
      @return false;
    }

    $map: map-get($map, $key);
  }

  @return true;
}

// =map-has-nested-key( $map, $keys... )
// -----------------------------------------------------------------------------
/// @alias map-has-deep-key
///
/// @access public
/// @since 0.1.0

@function map-has-nested-key( $map, $keys... )
{
  @return map-has-deep-key($map, $keys...);
}
