@use "sass:list";
@use "sass:map";
@use "sass:meta";
/// Base on -zf-map-next https://github.com/zurb/foundation-sites/blob/develop/scss/util/_breakpoint.scss
/// Find the next key in a map.
/// @access public
///
/// @param {Map} $map - Map to traverse.
/// @param {Mixed} $key - Key to use as a starting point.
///
/// @returns {Mixed} The value for the key after `$key`, if `$key` was found. If `$key` was not found, or `$key` was the last value in the map, returns `null`.
@function next($map, $key, $return: "value") {
  // Store the keys of the map as a list
  $values: map.keys($map);

  $i: 0;

  // If the Key Exists, Get the index of the key within the map and add 1 to it for the next breakpoint in the map
  @if (map.has-key($map, $key)) {
    $i: list.index($values, $key) + 1;
  }

  // If the key doesn't exist, or it's the last key in the map, return null
  @if ($i > list.length($map) or $i == 0) {
    @return null;
  }
  // Otherwise, return the value
  @else {
    @if $return == "value" {
      @return map.get($map, list.nth($values, $i));
    } @else {
      @return list.nth($values, $i);
    }
  }
}

/// Get last map key
/// @param {Map} $map - Map to extract last key from
/// @return {String} - The last key in provided map
@function get-last-key($map) {
  $keys: map.keys($map);

  @return list.nth($keys, list.length($map));
}

/// Fetch nested keys
/// @param {Map} $map - Map
/// @param {Arglist} $keys - Keys to fetch
/// @return {*}
///
@function map-deep-get($map, $keys...) {
  @each $key in $keys {
    @if (meta.type-of($map) == map) {
      $map: map.get($map, $key);
    }
  }

  @return $map;
}
