// -防抖
export function debounce(fn: Function, wait: number) {
    let delay = wait || 500
    let timer: any = null;
    return function () {
        let args = arguments;
        if (timer) {
            clearTimeout(timer)
        }
        let callNow = !timer
        timer = setTimeout(() => {
            timer = null
        }, delay)
        if (callNow) fn.apply(null, args)
    }
}

// -节流
export function throttle(fn: Function, wait: number) {
    let delay = wait || 500
    let timer: any = null;
    return function () {
        if (timer) {
            return
        }
        timer = setTimeout(() => {
            fn.apply(null, arguments)
            timer = null
        }, delay)
    }
}

//计算坐标距离
export function calcMapDistance(lat1: number, lng1: number, lat2: number, lng2: number) {
    let radLat1 = (lat1 * Math.PI) / 180.0;
    let radLat2 = (lat2 * Math.PI) / 180.0;
    let a = radLat1 - radLat2;
    let b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
    let s =
        2 *
        Math.asin(
            Math.sqrt(
                Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
            )
        );
    s = s * 6378.137; // EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000;
    return s.toFixed(2);
}

export function groupByKey(elements: Array<any>, key: string) {
    return elements.reduce((groups, item) => {
        const groupKey = item[key];
        if (!groups[groupKey]) {
            groups[groupKey] = [];
        }
        groups[groupKey].push(item);
        return groups;
    }, {});
}

export function fmtDictName(code: string, value: string) {
    let dictStorage: any = uni.getStorageSync('dict');

    if (!dictStorage) {
        return value;
    }

    let elements = dictStorage[code];
    if (elements) {
        let ele = elements.find((k: any) => k.value == value);
        if (ele) {
            return ele.name;
        }
    }
    return value;
}
export function getServicePhone() {
    let dictStorage: any = uni.getStorageSync('dict');

    if (!dictStorage) {
        return "";
    }

    let elements = dictStorage["Service.phone"];
    if (elements) {
        return elements[0].value;
    }
    return "";
}



export function fmtDateTime(date: any) {
    if (!date) {
        return "";
    }
    date = new Date(formatDateForIOS(date));
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
};

function formatDateForIOS(dateString:string) {
    return dateString.replace(/-/g, '/');
}


const formatNumber = n => {
    n = n.toString();
    return n[1] ? n : '0' + n
}

export function fmtDuration(ms: number) {
    let seconds = Math.floor(ms / 1000);
    let minutes = Math.floor(seconds / 60);
    let hours = Math.floor(minutes / 60);
    let days = Math.floor(hours / 24);

    hours %= 24;
    minutes %= 60;
    seconds %= 60;

    // 构建时长字符串
    let durationStr = '';
    if (days > 0) {
        durationStr += `${days}天`;
    }
    if (hours > 0) {
        durationStr += `${hours}时`;
    }
    if (minutes > 0) {
        durationStr += `${minutes}分`;
    }
    if (seconds > 0 || !durationStr) { // 如果没有小时和分钟，至少显示秒
        durationStr += `${seconds}秒`;
    }

    return durationStr.trim(); // 去除可能的前后空格
}


export function fmtMoney(money: number) {
    if (!money) {
        return "0.00";
    }
    return (money / 100).toFixed(2);
}