////
/// @author Mark Otto
////

/// Name of the next breakpoint, or null for the last breakpoint.
/// Breakpoints are defined as a map of (name: minimum width), order from small to large:
///    (xs: 0, sm: 34rem, md: 45rem)
///
/// The map defined in the `$grid-breakpoints` global variable is used as the `$breakpoints` argument by default.
/// @since 0.1.0 - The O.G.
/// @param {String} $name - the name for the breakpoint
/// @param {Map} $breakpoints - a map of grid breakpoints
/// @param {List} $breakpoint-names [map-keys($breakpoints)] - a list of names for breakpoints when passed a map of breakpoints
/// @return {String} the name of next breakpoint when passed a map of breakpoints
/// @example scss
///    @include breakpoint-next(sm)
///    // md
///    @include breakpoint-next(sm, $breakpoints: (xs: 0, sm: 34rem, md: 45rem))
///    // md
///    @include breakpoint-next(sm, $breakpoint-names: (xs sm md))
///    // md
@function breakpoint-next($name, $breakpoints, $breakpoint-names: map-keys($breakpoints)) {
	$n: index($breakpoint-names, $name);

	@return if($n < length($breakpoint-names), nth($breakpoint-names, $n + 1), null);
}

/// Minimum breakpoint width
/// @since 0.1.0 - The O.G.
/// @param {String} $name - the name for the breakpoint
/// @param {Map} $breakpoints - a map of grid breakpoints
/// @return {CSS Unit | null } the minimum value is calculated as the minimum of the next one or `null` for the smallest (first) breakpoint
/// @example scss
///    @include breakpoint-min(sm, (xs: 0, sm: 34rem, md: 45rem))
///    // 34rem
@function breakpoint-min($name, $breakpoints) {
	$min: map-get($breakpoints, $name);

	@return if($min != 0, $min, null);
}

/// Maximum breakpoint width.
/// @since 0.1.0 - The O.G.
/// @param {String} $name - the name for the breakpoint
/// @param {Map} $breakpoints - a map of grid breakpoints
/// @return {CSS Unit | null } the maximum value is calculated as the minimum of the next one less 0.1 or `null` for the largest (last) breakpoint.
/// @example scss
///    @include breakpoint-max(sm, (xs: 0, sm: 34rem, md: 45rem))
///    // 44.9rem
@function breakpoint-max($name, $breakpoints) {
	$next: breakpoint-next($name, $breakpoints);

	@return if($next, breakpoint-min($next, $breakpoints) - .1, null);
}
