@use 'sass:math';
@use 'sass:meta';
@use 'sass:map';
@use 'sass:list';

@mixin mq($from: none, $to: none, $from-operator: none, $to-operator: none) {
    @if $from != none and meta.type-of($from) == 'string' {
        $from: get-bp-width($from);
    }

    @if $to != none and meta.type-of($to) == 'string' {
        $to: get-bp-width($to);
    }

    @if ($from == none or $from <= list.nth(list.nth($grid-bp, 1), 2)) and $to == none {
        @content;
    } @else if $from == none or $from <= list.nth(list.nth($grid-bp, 1), 2) {
        @if $to-operator != none and $to-operator == '<' {
            $to: $to - 1;
        }

        @media (max-width: $to) {
            @content;
        }
    } @else if $to == none {
        @if $from-operator != none and $from-operator == '<' {
            $from: $from + 1;
        }

        @media (min-width: $from) {
            @content;
        }
    } @else {
        @if $from-operator != none and $from-operator == '<' {
            $from: $from + 1;
        }

        @if $to-operator != none and $to-operator == '<' {
            $to: $to - 1;
        }

        @media (min-width: $from) and (max-width: $to) {
            @content;
        }
    }
}

@function get-bp-width($name, $breakpoints: $grid-bp) {
    @if map.has-key($breakpoints, $name) {
        @return map.get($breakpoints, $name);
    } @else {
        @warn 'Breakpoint #{$name} wasn\'t found in $breakpoints.';
    }
}

/// @param {Number} $number - Number to remove unit from
/// @return {Number} - Unitless number
@function strip-unit($number) {
    @if meta.type-of($number) == 'number' and not math.is-unitless($number) {
        @return math.div($number, ($number * 0 + 1));
    }

    @return $number;
}

@mixin bp-suffix($class, $original: true, $breakpoints: $grid-bp, $create-between-breakpoints: false) {
    @if $original {
        .#{$class} {
            @content;
        }
    }

    @each $breakpoint-min, $size in $breakpoints {
        @include mq($breakpoint-min) {
            .#{$class}\@#{$breakpoint-min} {
                @content;
            }
        }

        @if $create-between-breakpoints {
            @each $breakpoint-max, $size in $grid-bp {
                @include mq($breakpoint-min, $breakpoint-max, $to-operator: '<') {
                    .#{$class}\@#{$breakpoint-min}\@#{$breakpoint-max} {
                        @content;
                    }
                }
            }
        }
    }
}
