import timerUtil, { TimeoutTimerNames } from '@/utils/timer/timerUtil';
import moment, { DurationInputArg1, Moment, unitOfTime } from "moment";

/**
 * 获取number类型时间戳
 * @returns
 */
export function getTimeValue(): number {
    return moment().valueOf()
}

type Time = string | Moment | Date
/**
 * 获取两个时间范围内的所有时间
 * @param val1 时间1
 * @param val2 时间2
 * @param type 时间格式化类型
 * @param unit 时间单位
 * @param amount 时间区间间隔数
 * @returns string[]类型
 */
export function getRangeTime(val1: Time, val2: Time, type = 'YYYYMMDD', unit: unitOfTime.DurationConstructor = 'd', amount: DurationInputArg1 = 1): string[] {
    const fn = (val: Moment) => val.format(type);
    // 处理开始时间和结束时间
    let startTime = moment(val1);
    let endTime = moment(val2);

    const result: string[] = [];
    const isSame = () => startTime.isSame(endTime, unit);

    if (isSame()) {
        return [fn(startTime), fn(startTime)];
    } else {
        if (startTime.isAfter(endTime)) {
            [startTime, endTime] = [endTime, startTime]
        }
        while (!isSame()) {
            result.push(fn(startTime));
            startTime = startTime.add(amount, unit);
        }
        result.push(fn(endTime));
        return result;
    }
}

/**
 * ------------------------------------------------------------------待优化后期废除
 * 防抖(debounce)
 * @param fn 需要防抖的函数
 * @param wait 毫秒，防抖期限值
 * @returns
 */
export function _debounce(fn: () => void, wait: number, timer: keyof TimeoutTimerNames = 'debounce'): void {
    return (function () {
        timerUtil.clearTimeout(timer);
        timerUtil.setTimeout(fn, wait, timer);
    })();
}

/**
 * 时间排序
 * @param arr
 * @param key
 * @param isUp true: 升序，false: 倒序
 * @returns
 */
export function sortTime<T extends object>(arr: T[], key: keyof T, isUp = true) {
    const result = arr.sort((a, b) => {
        const time1 = new Date((a[key] as unknown) as string).getTime();
        const time2 = new Date((b[key] as unknown) as string).getTime();
        return isUp ? time1 - time2 : time2 - time1;
    });
    return result;
}

const debounceMap = new Map<string, number>();
const throttleMap = new Map<string, number>();

/**
 * 函数防抖 (等待触发)
 * @param callback 到期时间执行的回调
 * @param ms 延迟毫秒数，默认100毫秒
 * @param timeoutId 防抖ID
 * @returns 
 */
export function debounce(callback: () => void, ms = 100, timeoutId = 'timer') {
    if (debounceMap.has(timeoutId)) {
        const t = debounceMap.get(timeoutId);
        clearTimeout(t);
    }

    const timer = window.setTimeout(() => {
        debounceMap.delete(timeoutId);
        callback();
    }, ms)

    debounceMap.set(timeoutId, timer);
}

/**
 * 函数节流 (间隔触发)
 * @param callback 到期时间执行的回调
 * @param ms 间隔毫秒数，默认100毫秒
 * @param timeoutId 节流ID
 * @returns 
 */
export function throttle(callback: () => void, ms = 100, timeoutId = 'timer') {
    if (throttleMap.has(timeoutId)) {
        const t = throttleMap.get(timeoutId);
        if (t) return;
    }

    let timer = window.setTimeout(() => {
        timer = 0;
        throttleMap.delete(timeoutId);
        callback();
    }, ms);

    throttleMap.set(timeoutId, timer);
}