// -----------------------------------------------------------------------------
// Breakpoints
// -----------------------------------------------------------------------------
// Table of contents
// 1. Breakpoint mixin
// 2. Each breakpoint mixin

// -----------------------------------------------------------------------------
// 1. Breakpoint config
// -----------------------------------------------------------------------------
// The following code is over-engineered.
// It basically allows you to use the mixin bk($breakpoint) without
// setting any variables for each breakpoint. It uses the $breakpoints map in
// settings and takes care of setting a max-width for the first breakpoint,
// a min-width for the last one and min/max for the in-between breakpoints.

$breakpoint-to-ems: true;
$breakpoint-list: map-keys($breakpoints);

@mixin bk($breakpoint) {
    @if (not map-has-key($breakpoints, $breakpoint)) {
        @error '`#{$breakpoint}` is not a valid breakpoint name.';
    }

    @include breakpoint(_bk($breakpoint)) {
        @content;
    }
}

@function _bk($breakpoint) {
    $br-pos: index($breakpoint-list, $breakpoint);
    $br: null;

    @if ($br-pos == 1) {
        $x: map-get($breakpoints, nth($breakpoint-list, 2));
        $br: max-width (map-get($x, size) - 1);
    } @elseif ($br-pos == length($breakpoint-list)) {
        $x: map-get($breakpoints, $breakpoint);
        $br: map-get($x, size);
    } @else {
        $x: map-get($breakpoints, $breakpoint);
        $y: map-get($breakpoints, nth($breakpoint-list, ($br-pos + 1)));
        $br: map-get($x, size) (map-get($y, size) - 1);
    }

    @return $br;
}

// -----------------------------------------------------------------------------
// 2. Each breakpoint mixin
// -----------------------------------------------------------------------------

@mixin each-breakpoint($class-name) {
    @if (type-of($class-name) != 'string') {
        @error '`#{$class-name}` is not a valid String for `$class-name`.';
    }

    %#{$class-name},
    .#{$class-name} {
        @content;
    }

    @each $breakpoint in $breakpoint-list {
        @include bk($breakpoint) {
            %#{$breakpoint}-#{$class-name},
            .#{$breakpoint}-#{$class-name} {
                @content;
            }
        }
    }
}
