//
// Bootstrap functions
//

///
/// usage:
///   $primary: map-deep-get($palette, "primary", "main");
///
@function map-deep-get($map, $keys...) {
    @each $key in $keys {
        $map: map-get($map, $key);
    }
    @return $map;
}

///
/// Deep set function to set a value in nested maps
///
/// usage:
///   map-deep-set($palette, "primary" "main", #00f);
///
@function map-deep-set($map, $keys, $value) {
    $maps: ($map);
    $result: null;

    // If the last key is a map already
    // Warn the user we will be overriding it with $value
    @if type-of(nth($keys, -1)) == "map" {
        @warn "The last key you specified is a map; it will be overrided with `#{$value}`.";
    }

    // If $keys is a single key
    // Just merge and return
    @if length($keys) == 1 {
        @return map-merge(
            $map,
            (
                $keys: $value,
            )
        );
    }

    // Loop from the first to the second to last key from $keys
    // Store the associated map to this key in the $maps list
    // If the key doesn't exist, throw an error
    @for $i from 1 through length($keys) - 1 {
        $current-key: nth($keys, $i);
        $current-map: nth($maps, -1);
        $current-get: map-get($current-map, $current-key);
        @if $current-get == null {
            @error "Key `#{$key}` doesn't exist at current level in map.";
        }
        $maps: append($maps, $current-get);
    }

    // Loop from the last map to the first one
    // Merge it with the previous one
    @for $i from length($maps) through 1 {
        $current-map: nth($maps, $i);
        $current-key: nth($keys, $i);
        $current-val: if($i == length($maps), $value, $result);
        $result: map-merge(
            $current-map,
            (
                $current-key: $current-val,
            )
        );
    }

    // Return result
    @return $result;
}

//
//
// A list of pre-calculated numbers of pow(($value / 255 + .055) / 1.055, 2.4). (from 0 to 255)
// stylelint-disable-next-line scss/dollar-variable-default, scss/dollar-variable-pattern
//
$_luminance-list: 0.0008 0.001 0.0011 0.0013 0.0015 0.0017 0.002 0.0022 0.0025 0.0027 0.003 0.0033 0.0037 0.004 0.0044 0.0048 0.0052 0.0056
    0.006 0.0065 0.007 0.0075 0.008 0.0086 0.0091 0.0097 0.0103 0.011 0.0116 0.0123 0.013 0.0137 0.0144 0.0152 0.016 0.0168 0.0176 0.0185
    0.0194 0.0203 0.0212 0.0222 0.0232 0.0242 0.0252 0.0262 0.0273 0.0284 0.0296 0.0307 0.0319 0.0331 0.0343 0.0356 0.0369 0.0382 0.0395
    0.0409 0.0423 0.0437 0.0452 0.0467 0.0482 0.0497 0.0513 0.0529 0.0545 0.0561 0.0578 0.0595 0.0612 0.063 0.0648 0.0666 0.0685 0.0704
    0.0723 0.0742 0.0762 0.0782 0.0802 0.0823 0.0844 0.0865 0.0887 0.0908 0.0931 0.0953 0.0976 0.0999 0.1022 0.1046 0.107 0.1095 0.1119
    0.1144 0.117 0.1195 0.1221 0.1248 0.1274 0.1301 0.1329 0.1356 0.1384 0.1413 0.1441 0.147 0.15 0.1529 0.1559 0.159 0.162 0.1651 0.1683
    0.1714 0.1746 0.1779 0.1812 0.1845 0.1878 0.1912 0.1946 0.1981 0.2016 0.2051 0.2086 0.2122 0.2159 0.2195 0.2232 0.227 0.2307 0.2346
    0.2384 0.2423 0.2462 0.2502 0.2542 0.2582 0.2623 0.2664 0.2705 0.2747 0.2789 0.2831 0.2874 0.2918 0.2961 0.3005 0.305 0.3095 0.314
    0.3185 0.3231 0.3278 0.3325 0.3372 0.3419 0.3467 0.3515 0.3564 0.3613 0.3663 0.3712 0.3763 0.3813 0.3864 0.3916 0.3968 0.402 0.4072
    0.4125 0.4179 0.4233 0.4287 0.4342 0.4397 0.4452 0.4508 0.4564 0.4621 0.4678 0.4735 0.4793 0.4851 0.491 0.4969 0.5029 0.5089 0.5149
    0.521 0.5271 0.5333 0.5395 0.5457 0.552 0.5583 0.5647 0.5711 0.5776 0.5841 0.5906 0.5972 0.6038 0.6105 0.6172 0.624 0.6308 0.6376 0.6445
    0.6514 0.6584 0.6654 0.6724 0.6795 0.6867 0.6939 0.7011 0.7084 0.7157 0.7231 0.7305 0.7379 0.7454 0.7529 0.7605 0.7682 0.7758 0.7835
    0.7913 0.7991 0.807 0.8148 0.8228 0.8308 0.8388 0.8469 0.855 0.8632 0.8714 0.8796 0.8879 0.8963 0.9047 0.9131 0.9216 0.9301 0.9387
    0.9473 0.956 0.9647 0.9734 0.9823 0.9911 1;

//------------------------
// luminance(#BADA55) => 0.6123778773
//------------------------
@function luminance($color) {
    $rgb: (
        "r": red($color),
        "g": green($color),
        "b": blue($color),
    );

    @each $name, $value in $rgb {
        $value_div_255: $value * 0.003921568627451;
        $value_div_255_1292: $value_div_255 * 0.0773993808049536;
        $value: if($value_div_255 < 0.03928, $value_div_255_1292, nth($_luminance-list, $value + 1));
        $rgb: map-merge(
            $rgb,
            (
                $name: $value,
            )
        );
    }

    @return (map-get($rgb, "r") * 0.2126) + (map-get($rgb, "g") * 0.7152) + (map-get($rgb, "b") * 0.0722);
}

//
// opaque(#fff, rgba(0, 0, 0, .5)) => #808080
//
@function opaque($background, $foreground) {
    @return mix(rgba($foreground, 1), $background, opacity($foreground) * 100);
}

//
// Tint a color: mix a color with white
//
@function tint-color($color, $weight) {
    @return mix(white, $color, $weight);
}

//
// Shade a color: mix a color with black
//
@function shade-color($color, $weight) {
    @return mix(black, $color, $weight);
}

//
// Shade the color if the weight is positive, else tint it
//
@function shift-color($color, $weight) {
    @return if($weight > 0, shade-color($color, $weight), tint-color($color, -$weight));
}

//
// mui light-color
//
@function light-color($color, $coef: 0.2) {
    $r: red($color);
    $g: green($color);
    $b: blue($color);
    $r: $r + (255 - $r) * $coef;
    $g: $g + (255 - $g) * $coef;
    $b: $b + (255 - $b) * $coef;
    @return rgb($r, $g, $b);
}

//
// mui dark-color
//
@function dark-color($color, $coef: 0.2) {
    $r: red($color);
    $g: green($color);
    $b: blue($color);
    $r: $r * (1 - $coef);
    $g: $g * (1 - $coef);
    $b: $b * (1 - $coef);
    @return rgb($r, $g, $b);
}

//
// scrollbar
//

@mixin scrollbar($width, $thumb-color) {
    &::-webkit-scrollbar {
        width: $width;
        border-radius: 3px;
    }

    &::-webkit-scrollbar-thumb {
        background-color: $thumb-color;
        border-radius: 3px;
        //outline: 1px solid slategrey;
    }
}

//
// str-replace
//

@function str-replace($string, $search, $replace: "") {
    $index: str-index($string, $search);

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

    @return $string;
}
