@use 'sass:map';
@use 'sass:list';
@use '../variables/breakpoints';

/**
  Gets a breakpoint from the map based on the key
*/
@function min-width($key) {
  @if (map.has-key(breakpoints.$device-to-grid-breakpoints, $key)) {
    $device-breakpoints: map.get(breakpoints.$device-to-grid-breakpoints, $key);
    $key: list.nth($device-breakpoints, 1);
  }

  @return map.get(breakpoints.$grid-breakpoints, $key);
}

/**
  Gets a breakpoint infix
*/
@function infix($key) {
  @if (map.has-key(breakpoints.$device-to-grid-breakpoints, $key)) {
    @error 'No infix defined for device "#{$key}". Use a breakpoint key instead.';
  }

  @if min-width($key) == 0 {
    @return '';
  }

  @return '-#{$key}';
}

/**
* Gets the next device or breakpoint key
*/
@function next($key) {
  $keys: map.keys(breakpoints.$grid-breakpoints);
  @if map.has-key(breakpoints.$device-to-grid-breakpoints, $key) {
    $keys: map.keys(breakpoints.$device-to-grid-breakpoints);
  }
  $n: list.index($keys, $key);
  @if $n < list.length($keys) {
    @return list.nth($keys, $n + 1);
  }

  @return null;
}
