@use 'sass:map';
@use 'sass:string';
@use '../../string/split/string-split';
@use '../map-deep-check/map-deep-check';

//
// Return a the value of a nested key in a map if it exists.
// If the key doesn't exist, it throw an error.
//
// Note: This function doesn't support the use
// of the search character in your map key names.
//
// @usage daff-map-deep-get($map, "nested.map.key");
// @returns any
// @throws error
@function daff-map-deep-get($map, $key, $search-character: '.') {
	$keys: string-split.daff-string-split($key, $search-character);

	@if not map-deep-check.daff-map-deep-check($map, $keys) {
		@error 'The map doesn\'t contain the $key: `#{$key}`\'';
	}

	@each $key in $keys {
		$map: map.get($map, $key);
	}

	@return $map;
}
