// Copyright (c) 2016-2019 VMware, Inc. All Rights Reserved.
// This software is released under MIT license.
// The full license information can be found in LICENSE in the root directory of this project.

@function clr-breakpoint-next($name, $breakpoints: $clr-grid-breakpoints, $breakpoint-names: map-keys($breakpoints)) {
  $n: index($breakpoint-names, $name);
  @if not $n {
    @error 'breakpoint `#{$name}` not found in `#{$breakpoints}`';
  }
  @return if($n < length($breakpoint-names), nth($breakpoint-names, $n + 1), null);
}

@function clr-breakpoint-min($name, $breakpoints: $clr-grid-breakpoints) {
  $min: map-get($breakpoints, $name);
  @return if($min != 0, $min, null);
}

@function clr-breakpoint-max($name, $breakpoints: $clr-grid-breakpoints) {
  $next: clr-breakpoint-next($name, $breakpoints);
  @return if($next, clr-breakpoint-min($next, $breakpoints) - 0.02, null);
}

@function clr-breakpoint-infix($name, $breakpoints: $clr-grid-breakpoints) {
  @return if(clr-breakpoint-min($name, $breakpoints) == null, '', '-#{$name}');
}

// 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 clr-media-breakpoint-up($name, $breakpoints: $clr-grid-breakpoints) {
  $min: clr-breakpoint-min($name, $breakpoints);
  @if $min {
    @media (min-width: $min) {
      @content;
    }
  } @else {
    @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 clr-media-breakpoint-down($name, $breakpoints: $clr-grid-breakpoints) {
  $max: clr-breakpoint-max($name, $breakpoints);
  @if $max {
    @media (max-width: $max) {
      @content;
    }
  } @else {
    @content;
  }
}
