"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatDate = formatDate;
exports.formatDateTime = formatDateTime;
exports.getTwoNumber = getTwoNumber;
const dayjs_1 = __importDefault(require("dayjs"));
const cn_utils_1 = require("../../../cn-utils");
const constants_1 = require("../../constants");
/**
 * 格式化日期
 * @param val 日期值
 * @param interv 分隔符
 * @param dateFormat 日期格式
 * @param adapterLocale 本地化适配
 */
function formatDate(val = '0', interv = constants_1.DEFAULT_DATE_INTERV, dateFormat = constants_1.DEFAULT_DATE_FORMAT, adapterLocale) {
    if (adapterLocale) {
        return (0, cn_utils_1.dateFormatStr)(val, dateFormat, adapterLocale);
    }
    if (interv !== constants_1.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 (0, dayjs_1.default)(val).format(dateFormat);
}
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())}` : ''}`;
}
function getTwoNumber(val) {
    const num = parseInt(String(val), 10);
    return num < 10 && num >= 0 ? `0${num}` : num;
}
