import store from '@/store'
import {stringIsBlank} from "@/utils/blank-utils";

export function needRule(...val) {
    const userRoles = store.state.user.info.roles
    if (userRoles.includes(1)) {
        return true;
    }
    for (let i = 0; i < val.length; i++) {
        if (userRoles.includes(val[i])) {
            return true;
        }
    }
    return false;
}

export function noNeedRule(...val) {
    return !needRule(val)
}

export let isDev = import.meta.env.VITE_BASE_URL !== 'http://172.16.32.160:8706'

export const nullToEmpty = (val) => {
    if (stringIsBlank(val)) {
        return ''
    } else {
        return val
    }
}

export const elementAddBody = (val) => {
    const body = document.querySelector("body");
    if (body.append) {
        body.append(val);
    } else {
        body.appendChild(val);
    }
}

/**
 * 对比版本号
 * @param version1 版本1
 * @param version2 版本 2
 * @returns {number} 1 大于 -1小于 0等于
 */
export function compareVersion(version1, version2) {
    const arr1 = version1.split('.')
    const arr2 = version2.split('.')
    const length1 = arr1.length
    const length2 = arr2.length
    const minlength = Math.min(length1, length2)
    let i = 0
    for (i; i < minlength; i++) {
        let a = parseInt(arr1[i])
        let b = parseInt(arr2[i])
        if (a > b) {
            return 1
        } else if (a < b) {
            return -1
        }
    }
    if (length1 > length2) {
        for (let j = i; j < length1; j++) {
            if (parseInt(arr1[j]) !== 0) {
                return 1
            }
        }
        return 0
    } else if (length1 < length2) {
        for (let j = i; j < length2; j++) {
            if (parseInt(arr2[j]) !== 0) {
                return -1
            }
        }
        return 0
    }
    return 0
}
