@use "sass:map";
@use "sass:list";

@function map-deep-get($map, $keys...) {
    $value: $map;

    @each $key in $keys {
        $value: map.get($value, $key);
    }

    @return $value;
}

@function map-deep-set($map, $keys...) {
    $map-list: ($map);
    $result: null;

    @if list.length($keys) == 2 {
        @return map-merge(
            $map,
            (
                list.nth($keys, 1): list.nth($keys, -1)
            )
        );
    }

    @for $i from 1 through list.length($keys) - 2 {
        $map-list: append($map-list, map.get(list.nth($map-list, -1), list.nth($keys, $i)));
    }

    @for $i from list.length($map-list) through 1 {
        $result: map-merge(
            list.nth($map-list, $i),
            (
                list.nth($keys, $i): if($i ==list.length($map-list), list.nth($keys, -1), $result)
            )
        );
    }

    @return $result;
}

@function darken-till-contrast($color, $primary) {
    @while (contrast-checker($color, $primary) == "false") {
        $color: darken($color, 1%);
        @if (contrast-checker($color, $primary) == "true") {
            @return $color;
        }
    }
    @return $color;
}

@function lighten-till-contrast($color, $primary) {
    @while (contrast-checker($color, $primary) == "false") {
        $color: lighten($color, 1%);
        @if (contrast-checker($color, $primary) == "true") {
            @return $color;
        }
    }
    @return $color;
}
