@use 'sass:list';
@use 'sass:map';
@use 'sass:meta';

@function get-map($map, $key, $children: ()) {
  @if meta.variable-exists('map') and meta.type-of($map) == map {
    @if map.has-key($map, $key) {
      @if length($children) > 0 {
        $map: map.get($map, $key);

        @if length($children) == 1 {
          @return map.get($map, $children);
        } @else {
          $new-list: ();

          @for $i from 1 through length($children) {
            @if $i != 1 {
              $new-list: list.append($new-list, list.nth($children, $i), 'space');
            }
          }

          @return get-map($map, list.nth($children, 1), $new-list);
        }
      } @else {
        @return map.get($map, $key);
      }
    } @else {
      @error "ERROR: the key `#{$key}` does not exist in map.";
    }
  } @else {
    @error "ERROR: map does not exist.";
  }
}

@function merge-maps($maps) {
  $collection: ();

  @for $i from 1 through length($maps) {
    $collection: map.merge($collection, list.nth($maps, $i));
  }

  @return $collection;
}
