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

/// Casts a Map into a List.
///
/// @param {Map} $map - The map being cast into a list.
/// @param {String} $keep ['both'] - Whether to keep just the 'keys', just the
/// 'values' or 'both' from the $map.
/// @param {String} $separator [comma] - The separator to use in the new List.
/// Can be `comma`, `space`, or `slash`.
///
/// @return {List} Depending on the flag, returns either keys, values or both.
///
/// @access public
/// @group Utilities
@function map-to-list($map, $keep: 'both', $separator: comma) {
  $keep: if(list.index('keys' 'values', $keep), $keep, 'both');

  @if meta.type-of($map) == 'map' {
    $keys: ();
    $values: ();

    @each $key, $val in $map {
      $keys: list.append($keys, $key, $separator);
      $values: list.append($values, $val, $separator);
    }

    @if $keep == 'keys' {
      @return $keys;
    } @else if $keep == 'values' {
      @return $values;
    } @else {
      @return list.zip($keys, $values);
    }
  }

  @return if(meta.type-of($map) == 'list', $map, ($value));
}
