// Bootstrap functions
//
// Utility mixins and functions for evaluating source code across our variables, maps, and mixins.

// Ascending
// Used to evaluate Sass maps like our grid breakpoints.
@use "sass:color";
@use "sass:list";
@use "sass:map";
@use "sass:math";
@use "sass:meta";
@use "sass:string";
@use './functions-variables';
@use "../variables";

@mixin assert-ascending($map, $map-name) {
    $prev-key: null;
    $prev-num: null;
    @each $key, $num in $map {
        @if $prev-num == null or math.unit($num) == '%' or math.unit($prev-num) == '%' {
            // Do nothing
        } @else if not math.compatible($prev-num, $num) {
            @warn "Potentially invalid value for #{$map-name}: This map must be in ascending order, but key '#{$key}' has value #{$num} whose unit makes it incomparable to #{$prev-num}, the value of the previous key '#{$prev-key}' !";
        } @else if $prev-num >= $num {
            @warn "Invalid value for #{$map-name}: This map must be in ascending order, but key '#{$key}' has value #{$num} which isn't greater than #{$prev-num}, the value of the previous key '#{$prev-key}' !";
        }
        $prev-key: $key;
        $prev-num: $num;
    }
}

// Starts at zero
// Used to ensure the min-width of the lowest breakpoint starts at 0.
@mixin assert-starts-at-zero($map, $map-name: '$grid-breakpoints') {
    @if list.length($map) > 0 {
        $values: map.values($map);
        $first-value: list.nth($values, 1);
        @if $first-value != 0 {
            @warn "First breakpoint in #{$map-name} must start at 0, but starts at #{$first-value}.";
        }
    }
}

// Replace `$search` with `$replace` in `$string`
// Used on our SVG icon backgrounds for custom forms.
//
// @author Hugo Giraudel
// @param {String} $string - Initial string
// @param {String} $search - Substring to replace
// @param {String} $replace ('') - New value
// @return {String} - Updated string
@function str-replace($string, $search, $replace: '') {
    $index: string.index($string, $search);

    @if $index {
        @return string.slice($string, 1, $index - 1) + $replace +
            str-replace(string.slice($string, $index + string.length($search)), $search, $replace);
    }

    @return $string;
}

// See https://codepen.io/kevinweber/pen/dXWoRw
// @function escape-svg($string) {
//     @if string.index($string, 'data:image/svg+xml') {
//         @each $char, $encoded in $escaped-characters {
//             // Do not escape the url brackets
//             @if string.index($string, 'url(') == 1 {
//                 $string: url('#{str-replace(string.slice($string, 6, -3), $char, $encoded)}');
//             } @else {
//                 $string: str-replace($string, $char, $encoded);
//             }
//         }
//     }

//     @return $string;
// }

// Color contrast
@function color-yiq($color, $dark: functions-variables.$yiq-text-dark, $light: functions-variables.$yiq-text-light) {
    $r: color.red($color);
    $g: color.green($color);
    $b: color.blue($color);

    $yiq: (($r * 299) + ($g * 587) + ($b * 114)) * 0.001;

    @if ($yiq >= functions-variables.$yiq-contrasted-threshold) {
        @return $dark;
    } @else {
        @return $light;
    }
}

// Retrieve color Sass maps
@function color($key: 'blue') {
    @return map.get(functions-variables.$colors, $key);
}

@function theme-color($key: 'primary') {
    @return map.get(functions-variables.$theme-colors, $key);
}

@function gray($key: '100') {
    @return map.get($grays, $key);
}

// Request a theme color level
@function theme-color-level($color-name: 'primary', $level: 0) {
    $color: theme-color($color-name);
    $color-base: if($level > 0, variables.$black, variables.$white);
    $level: math.abs($level);

    @return color.mix($color-base, $color, $level * $theme-color-interval);
}

// Return valid calc
@function add($value1, $value2, $return-calc: true) {
    @if $value1 == null {
        @return $value2;
    }

    @if $value2 == null {
        @return $value1;
    }

    @if meta.type-of($value1) == number and meta.type-of($value2) == number and math.compatible($value1, $value2) {
        @return $value1 + $value2;
    }

    @return if($return-calc == true, calc(#{$value1} + #{$value2}), $value1 + string.unquote(' + ') + $value2);
}

@function subtract($value1, $value2, $return-calc: true) {
    @if $value1 == null and $value2 == null {
        @return null;
    }

    @if $value1 == null {
        @return -$value2;
    }

    @if $value2 == null {
        @return $value1;
    }

    @if meta.type-of($value1) == number and meta.type-of($value2) == number and math.compatible($value1, $value2) {
        @return $value1 - $value2;
    }

    @return if($return-calc == true, calc(#{$value1} - #{$value2}), $value1 + string.unquote(' - ') + $value2);
}
