// RIVER Design-System: System - Functions / Responsive //

// Responsive functions for working with breakpoints.
// These functions are primarily used by our responsive mixins.

// Retrieve the map of design breakpoints
@function design-breakpoints() {
    @return design-setting('responsive.breakpoints', ());
}

// Retrieve the map of design breakpoints, excluding the smallest breakpoint (0 width)
@function design-breakpoints-upwards($customBreakpoints: null) {
    $breakpoints: design-breakpoints();
    $min: design-breakpoint-min(); // eg, '2xs'
    $breakpoints: map-remove($breakpoints, $min);
    @return $breakpoints;
}

// Retrieve the key (or value) for the smallest breakpoint
@function design-breakpoint-min($returnValue: false) {
    $breakpoints: design-breakpoints();
    $keys: map-keys($breakpoints);
    $index: 1;
    $key: nth($keys, $index); // eg, '2xs'
    @return if($returnValue, map-get($breakpoints, $key), $key);
}

// Retrieve the key (or value) for the largest breakpoint
@function design-breakpoint-max($returnValue: false) {
    $breakpoints: design-breakpoints();
    $keys: map-keys($breakpoints);
    $index: length($breakpoints);
    $key: nth($keys, $index); // eg, '2xl'
    @return if($returnValue, map-get($breakpoints, $key), $key);
}

// Retrieve the key for the previous breakpoint
@function design-breakpoint-prev($breakpoint: null) {
    $breakpoints: design-breakpoints();
    $breakpointNames: map-keys($breakpoints);
    @if(not(map-has-key($breakpoints, $breakpoint))) {
        @error('design-breakpoint-prev() unable to match the design breakpoint "#{$breakpoint}"');
    }
    $index: index($breakpointNames, $breakpoint);
    $breakpointNext: if($index > 1, nth($breakpointNames, $index - 1), null);
    @return $breakpointNext;
}

// Retrieve the key for the next breakpoint
@function design-breakpoint-next($breakpoint: null) {
    $breakpoints: design-breakpoints();
    $breakpointNames: map-keys($breakpoints);
    @if(not(map-has-key($breakpoints, $breakpoint))) {
        @error('design-breakpoint-next() unable to match the design breakpoint "#{$breakpoint}"');
    }
    $index: index($breakpointNames, $breakpoint);
    $breakpointNext: if($index < length($breakpointNames), nth($breakpointNames, $index + 1), null);
    @return $breakpointNext;
}

// Retrieve the value (width) for a given breakpoint key
@function design-breakpoint-value($breakpoint: null) {
    $breakpoints: design-breakpoints();
    @return map-get($breakpoints, $breakpoint);
}
