@use "sass:map";
@use "sass:list";
@use "../settings/breakpoints" as *;

// Breakpoint viewport sizes and media queries.
// Breakpoints are defined as a map of (name: minimum width), order from small to large:
//    (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px)
// The map defined in the `$breakpoints` global variable is used as the `$bps` argument by default.

// Name of the next breakpoint, or null for the last breakpoint.
//    >> breakpoint-next(sm)
//    md
//    >> breakpoint-next(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
//    md
//    >> breakpoint-next(sm, $bps-names: (xs sm md lg xl))
//    md
@function breakpoint-next($name, $bps: $breakpoints, $bps-names: map.keys($bps)) {
	$n: list.index($bps-names, $name);

	@if $n != null and $n < list.length($bps-names) {
		@return list.nth($bps-names, $n + 1);
	} @else {
		@return null;
	}
}

@function bp-next($name, $bps: $breakpoints, $bps-names: map.keys($bps)) {
	@return breakpoint-next($name, $bps, $bps-names);
}

// Minimum breakpoint width. Null for the smallest (first) breakpoint.
//    >> breakpoint-min(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
//    576px
@function breakpoint-min($name, $bps: $breakpoints) {
	$min: map.get($bps, $name);

	@if $min != 0 {
		@return $min;
	} @else {
		@return null;
	}
}

@function bp-min($name, $bps: $breakpoints) {
	@return breakpoint-min($name, $bps);
}

// Maximum breakpoint width. Null for the largest (last) breakpoint.
// The maximum value is calculated as the minimum of the next one less 0.02px
// to work around the limitations of `min-` and `max-` prefixes and viewports with fractional widths.
// See https://www.w3.org/TR/mediaqueries-4/#mq-min-max
// Uses 0.02px rather than 0.01px to work around a current rounding bug in Safari.
// See https://bugs.webkit.org/show_bug.cgi?id=178261
//    >> breakpoint-max(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
//    767.98px
@function breakpoint-max($name, $bps: $breakpoints) {
	$next: breakpoint-next($name, $bps);

	@if $next {
		@return breakpoint-min($next, $bps) - 0.02;
	} @else {
		@return null;
	}
}

@function bp-max($name, $bps: $breakpoints) {
	@return breakpoint-max($name, $bps);
}

// Returns a blank string if smallest breakpoint, otherwise returns the name with a dash in front.
// Useful for making responsive utilities.
//    >> breakpoint-infix(xs, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
//    ""  (Returns a blank string)
//    >> breakpoint-infix(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
//    "-sm"
@function breakpoint-infix($name, $bps: $breakpoints) {
	@if not breakpoint-min($name, $bps) {
		@return "";
	} @else {
		@return "-#{$name}";
	}
}

@function bp-infix($name, $bps: $breakpoints) {
	@return breakpoint-infix($name, $bps);
}

// Media of at least the minimum breakpoint width. No query for the smallest breakpoint.
// Makes the @content apply to the given breakpoint and wider.
@mixin media-breakpoint-up($name, $bps: $breakpoints) {
	$min: breakpoint-min($name, $bps);

	@if $min {
		@media (min-width: $min) {
			@content;
		}
	} @else {
		@content;
	}
}

@mixin bp-up($name, $bps: $breakpoints) {
	@include media-breakpoint-up($name, $bps) {
		@content;
	}
}

// Media of at most the maximum breakpoint width. No query for the largest breakpoint.
// Makes the @content apply to the given breakpoint and narrower.
@mixin media-breakpoint-down($name, $bps: $breakpoints) {
	$max: breakpoint-max($name, $bps);

	@if $max {
		@media (max-width: $max) {
			@content;
		}
	} @else {
		@content;
	}
}

@mixin bp-down($name, $bps: $breakpoints) {
	@include media-breakpoint-down($name, $bps) {
		@content;
	}
}

// Media that spans multiple breakpoint widths.
// Makes the @content apply between the min and max breakpoints
@mixin media-breakpoint-between($lower, $upper, $bps: $breakpoints) {
	$min: breakpoint-min($lower, $bps);
	$max: breakpoint-max($upper, $bps);

	@if $min != null and $max != null {
		@media (min-width: $min) and (max-width: $max) {
			@content;
		}
	} @else if $max == null {
		@include media-breakpoint-up($lower, $bps) {
			@content;
		}
	} @else if $min == null {
		@include media-breakpoint-down($upper, $bps) {
			@content;
		}
	}
}

@mixin bp-between($lower, $upper, $bps: $breakpoints) {
	@include media-breakpoint-between($lower, $upper, $bps) {
		@content;
	}
}

// Media between the breakpoint's minimum and maximum widths.
// No minimum for the smallest breakpoint, and no maximum for the largest one.
// Makes the @content apply only to the given breakpoint, not viewports any wider or narrower.
@mixin media-breakpoint-only($name, $bps: $breakpoints) {
	$min: breakpoint-min($name, $bps);
	$max: breakpoint-max($name, $bps);

	@if $min != null and $max != null {
		@media (min-width: $min) and (max-width: $max) {
			@content;
		}
	} @else if $max == null {
		@include media-breakpoint-up($name, $bps) {
			@content;
		}
	} @else if $min == null {
		@include media-breakpoint-down($name, $bps) {
			@content;
		}
	}
}

@mixin bp-only($name, $bps: $breakpoints) {
	@include media-breakpoint-only($name, $bps) {
		@content;
	}
}
