@use 'sass:meta';
@use 'sass:string';
@use 'sass:list';
@use 'sass:color';
@use 'config';

$default-theme: #4d80f0 !default; // 正常色

/* 转换成字符串 */
@function selectorToString($selector) {
    $selector: meta.inspect($selector);
    $selector: string.slice($selector, 2, -2);

    @return $selector;
}

/* 判断是否存在 Modifier */
@function containsModifier($selector) {
    $selector: selectorToString($selector);

    @if string.index($selector, config.$modifier-separator) {
        @return true;
    } @else {
        @return false;
    }
}

/* 判断是否存在伪类 */
@function containsPseudo($selector) {
    $selector: selectorToString($selector);

    @if string.index($selector, ':') {
        @return true;
    } @else {
        @return false;
    }
}

/**
 * 主题色切换
 * @params $theme-color 主题色
 * @params $type 变暗’dark‘ 变亮 'light'
 * @params $mix-color 自己设置的混色
 */
@function themeColor($theme-color, $type: '', $mix-color: '') {
    @if $type == 'dark' {
        // lighten/darken 原理是直接加减亮度，
        // color.scale($color, $lightness: -10%) 表示亮度减少 10%
        @return color.scale($theme-color, $lightness: -10%);
    } @else if $type == 'light' {
        // lighten($theme-color, 30%) 对应 color.scale($theme-color, $lightness: 30%)
        @return color.scale($theme-color, $lightness: 30%);
    } @else {
        @return $theme-color;
    }
}

/**
 * 颜色结果切换， 如果开启线性渐变色 使用渐变色，如果没有开启，那么使用主题色
 * @params $open-linear 是否开启线性渐变色
 * @params $deg 渐变色角度
 * @params $theme-color 当前配色
 * @params [Array] $set 主题色明暗设置，与 $color-list 数量对应
 * @params [Array] $color-list 渐变色顺序， $color-list 和 $per-list 数量相同
 * @params [Array] $per-list 渐变色比例
 */
@function resultColor($deg, $theme-color, $set, $color-list, $per-list) {
    // 确保长度一致性检查
    @if list.length($color-list) != list.length($per-list) {
        @error "颜色列表和百分比列表长度不匹配";
    }

    $arg: '#{$deg}';

    @for $i from 1 through list.length($color-list) {
        $c: themeColor($theme-color, list.nth($set, $i), list.nth($color-list, $i));
        $p: list.nth($per-list, $i);

        $arg: $arg + ', ' + $c + ' ' + $p;
    }

    @return linear-gradient(string.unquote($arg));
}
