export const ArrayToMap = (arr, key) => {
    let result = {};
    for (let index = 0; index < arr.length; index++) {
        const element = arr[index];
        result[element[key]] = element;
    }

    return result;
}

export const getTimeFrame = (starttime, endtime) => {
    if (!starttime && !endtime) return '永久';

    if (starttime && !endtime) return `${starttime}以后`;

    if (!starttime && endtime) return `截止到${endtime}`;

    if (starttime && endtime) return `${starttime}至${endtime}`;
}

// 手机号中间四位加星号
export const hidePhone = (phone) => {
    if (!phone) return '';
    return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
}

export const stripHtmlByRegex = (html) => {
    return html.replace(/<[^>]*>/g, '').replace(/&[^;]+;/g, '');
}

export const formatMilliseconds = (milliseconds) => {
    const totalSeconds = Math.floor(milliseconds / 1000);
    const hours = Math.floor(totalSeconds / 3600).toString().padStart(2, '0');
    const minutes = Math.floor((totalSeconds % 3600) / 60).toString().padStart(2, '0');
    const seconds = (totalSeconds % 60).toString().padStart(2, '0');
    return `${minutes}:${seconds}`;
}
