import { useDateFormat } from "@vueuse/core";

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time: number, label: string) {
    if (time === 1) {
        return time + label;
    }
    return time + label + "s";
}

// 时间戳转标准时间
export function timestampToTime(timestamp: number) {
    if (timestamp === 0 || timestamp == null) {
        return "";
    } else {
        if (timestamp.toString().length < 13) {
            var date = new Date(timestamp * 1000);
        } else {
            var date = new Date(timestamp);
        }
        var Y = date.getFullYear() + "-";
        var M =
            (date.getMonth() + 1 < 10
                ? "0" + (date.getMonth() + 1)
                : date.getMonth() + 1) + "-";
        var D =
            date.getDate() < 10
                ? "0" + date.getDate() + " "
                : date.getDate() + " ";
        var H = date.getHours() + ":";
        var M2 =
            (date.getMinutes() < 10
                ? "0" + date.getMinutes()
                : date.getMinutes()) + ":";
        var S =
            date.getSeconds() < 10
                ? "0" + date.getSeconds()
                : date.getSeconds();
        return Y + M + D + H + M2 + S;
    }
}

/**
 * @param {number} time
 */
export function timeAgo(time: number) {
    const between = Date.now() / 1000 - Number(time);
    if (between < 3600) {
        return pluralize(~~(between / 60), " minute");
    } else if (between < 86400) {
        return pluralize(~~(between / 3600), " hour");
    } else {
        return pluralize(~~(between / 86400), " day");
    }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num: number, digits: number) {
    const si = [
        { value: 1e18, symbol: "E" },
        { value: 1e15, symbol: "P" },
        { value: 1e12, symbol: "T" },
        { value: 1e9, symbol: "G" },
        { value: 1e6, symbol: "M" },
        { value: 1e3, symbol: "k" },
    ];
    for (let i = 0; i < si.length; i++) {
        if (num >= si[i].value) {
            return (
                (num / si[i].value)
                    .toFixed(digits)
                    .replace(/\.0+$|(\.[0-9]*[1-9])0+$/, "$1") + si[i].symbol
            );
        }
    }
    return num.toString();
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num: number) {
    return (+num || 0)
        .toString()
        .replace(/^-?\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ","));
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string: string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

/**
 * 金额转换（分->元）
 * 100 => 1
 * @param {number} num
 */
export function moneyFormatter(num: number) {
    return "¥" + (isNaN(num) ? 0.0 : parseFloat((num / 100).toFixed(2)));
}

/**
 * 增加或减少天数，day：0表示今天，1表示明天，2表示后天 --  -1表示昨天
 * @param day
 * @returns
 */
export function formatTime(day: number) {
    return useDateFormat(Date.now() + 24 * 60 * 60000 * day, "YYYY-MM-DD")
        .value;
}

// 保留两位小数不四舍五入

export function truncation(num: any) {
    return (Math.floor(num * 100) / 100).toFixed(2);
}

// 判断后端数据返回为null 前端显示0

export function emptyTransfer(data: any) {
    if (typeof data == "object") {
        return 0;
    } else {
        return data;
    }
}
