import {toColor, clog, getIndex} from "vue-utils-plus"
import {useAppStore} from "~src/store";

const store = useAppStore()

//设置主色调
export const setMainColor = (color) => {
    color = color ?? '#1ECC95'
    const el = document.documentElement
    el.style.setProperty('--el-color-primary', color)
    // 设置 css 渐变 变量
    const numArr = [3, 5, 7, 8, 9]
    numArr.forEach(item => {
        let amount = 0
        if (item === 3) {
            amount = 0.9
        } else if (item === 5) {
            amount = 0.7
        } else if (item >= 7) {
            amount = amount = (10 - item) / 10
        }
        const val = toColor('#FFFFFF', color, amount)
        el.style.setProperty(`--el-color-primary-light-${item}`, val)
    })
    //生成深主色颜色
    const val = toColor('#000000', color, 0.9)
    el.style.setProperty('--el-color-primary-dark-2', val)
}

//控制台打印
export const HcLog = (name, tips, data) => {
    const title = store.barMenuName ?? '';
    clog(name, tips, data, title)
}

//取数组中的值
export const getRowsValue = (arr, key, key2, value) => {
    if (value) {
        const index = getIndex(arr, key, value)
        return arr[index][key2]
    } else {
        return ''
    }
}

//拼接ID
export const rowsToId = (rows) => {
    return rowsToKey(rows, 'id')
}

//拼接字段
export const rowsToKey = (rows, key, join = ',') => {
    return rows.map((obj) => {
        return obj[key];
    }).join(join)
}

//删除提醒
export const delMessage = (cbk) => {
    window?.$messageBox?.alert('请谨慎考虑后，确认是否需要删除？', '删除提醒', {
        showCancelButton: true,
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning',
        callback: (action) => {
            if (action === 'confirm') {
                cbk()
            }
        }
    })
}


//设置复制文本
export const setCopyText = (text) => {
    return new Promise((resolve) => {
        if (window.navigator?.clipboard) {
            window.navigator.clipboard.writeText(text).then(() => {
                resolve(true)
            }).catch(() => {
                resolve(false)
            });
        } else {
            resolve(false)
        }
    })
}

//取剪切板文本
export const getCopyText = () => {
    return new Promise((resolve) => {
        if (window.navigator?.clipboard) {
            window.navigator.clipboard.readText().then((text) => {
                resolve(text)
            }).catch(() => {
                resolve(false)
            });
        } else {
            resolve(false)
        }
    })
}

