@use "sass:math";

@mixin transition($transition...) {
    @if $enable-transitions {
        transition: $transition;
    }
}

// Functions

@function const($const) {
    // Workarounds for node-sass color bug
    @if $const == white { $const: "white"; }
    @if $const == black { $const: "black"; }
    @if $const == transparent { $const: "transparent"; }

    @return map-get($constants, $const);
}

@function color($color) {
    @return map-get($colors, $color);
}

@function light($color) {
    @return map-get($light-colors, $color);
}

@function dark($color) {
    @return map-get($dark-colors, $color);
}

@function theme-is($type) {
    @if $type == "kendo" {
        @return str-index($theme-variant, '#{$type}-') == 1;
    }

    @return $type == $theme-variant;
}

@function mode-get($mode, $color) {
    @if $mode == light {
        @return map-get($light-colors, $color);
    }

    @return map-get($dark-colors, $color);
}

@function rem-to-dip($size) {
    @if unit($size) != 'em' and unit($size) != 'rem' {
        @return $size / ($size * 0 + 1);
    }

    $value: $size * $font-size;

    @return round($value);
}

@function filter-by-text($list, $starts, $excludes, $recursive: false) {
    $result: ();

    @for $i from 1 through length($list) {
        @if type-of(nth($list, $i)) == list and $recursive {
            $result: append($result, filter-by-text(nth($list, $i), $starts, $recursive));
        } @else if str-index(nth($list, $i), $starts) == 1 {
            $result: append($result, nth($list, $i));
        }
    }

    @return $result;
}

@function filter-selectors($selectors, $compat: false) {
    @return filter-by-text($selectors, \., \.nt-);
}

@function check-contrast($color) {
    $color-brightness: round((red($color) * 299) + (green($color) * 587) + math.div(blue($color) * 114, 1000));
    $light-color: round((red(#ffffff) * 299) + (green(#ffffff) * 587) + math.div(blue(#ffffff) * 114, 1000));

    @return abs($color-brightness) < math.div($light-color, 1.7);
}

@function alternate($color, $lighten: 60%, $darken: null) {
    $darken: if($darken != null, $darken, $lighten);

    @return adjust-color($color, $lightness: if(check-contrast($color), $lighten, -$darken));
}

@function scale-alternate($color, $lighten: 60%, $darken: null) {
    $darken: if($darken != null, $darken, $lighten);

    @return scale-color($color, $lightness: if(check-contrast($color), $lighten, -$darken));
}

@function contrasted($color, $to-color, $lighten: 60%, $darken: null) {
    $darken: if($darken != null, $darken, $lighten);

    @return adjust-color($color, $lightness: if(check-contrast($to-color), $lighten, -$darken));
}

@function scale-contrasted($color, $to-color, $lighten: 60%, $darken: null) {
    $darken: if($darken != null, $darken, $lighten);

    @return scale-color($color, $lightness: if(check-contrast($to-color), $lighten, -$darken));
}

@function slice($list, $start: 1, $end: length($list)) {
    $result: ();

    @if ($start > $end) {
        @return $result;
    }

    @for $i from $start through $end {
        $result: append($result, nth($list, $i));
    }

    @return $result;
}
