import dayjs from 'dayjs';
import { dateFormatStr } from '../../../cn-utils';
import { DEFAULT_DATE_FORMAT, DEFAULT_DATE_INTERV } from '../../constants';
/**
 * 格式化日期
 * @param val 日期值
 * @param interv 分隔符
 * @param dateFormat 日期格式
 * @param adapterLocale 本地化适配
 */
export function formatDate(val = '0', interv = DEFAULT_DATE_INTERV, dateFormat = DEFAULT_DATE_FORMAT, adapterLocale) {
    if (adapterLocale) {
        return dateFormatStr(val, dateFormat, adapterLocale);
    }
    if (interv !== DEFAULT_DATE_INTERV) {
        const date = new Date(val !== null && val !== void 0 ? val : 0);
        return `${date.getFullYear()}${interv}${date.getMonth() + 1}${interv}${date.getDate()}`;
    }
    return dayjs(val).format(dateFormat);
}
export function formatDateTime(val, { interv = '-', hasSecond = true, adapterLocale }) {
    const date = new Date(val !== null && val !== void 0 ? val : 0);
    return `${formatDate(val, interv, undefined, adapterLocale)} ${getTwoNumber(date.getHours())}:${getTwoNumber(date.getMinutes())}${hasSecond ? `:${getTwoNumber(date.getSeconds())}` : ''}`;
}
export function getTwoNumber(val) {
    const num = parseInt(String(val), 10);
    return num < 10 && num >= 0 ? `0${num}` : num;
}
