"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseFlexibleDate = parseFlexibleDate;
const dayjs_1 = __importDefault(require("dayjs"));
const customParseFormat_1 = __importDefault(require("dayjs/plugin/customParseFormat"));
dayjs_1.default.extend(customParseFormat_1.default);
function parseFlexibleDate(value) {
    if (!value)
        return null;
    if (value instanceof Date && !isNaN(value.getTime())) {
        return value;
    }
    if (typeof value === 'number') {
        const excelEpoch = new Date(Date.UTC(1899, 11, 30));
        const d = new Date(excelEpoch.getTime() + value * 86400000);
        return isNaN(d.getTime()) ? null : d;
    }
    let str = value.toString().trim();
    str = str.replace(/\s*\(.*\)$/, '');
    const formats = [
        'DD-MM-YYYY',
        'YYYY-MM-DD',
        'DD/MM/YYYY',
        'MM/DD/YYYY',
        'D-M-YYYY',
        'YYYY/MM/DD',
        'ddd MMM D YYYY HH:mm:ss [GMT]ZZ',
        'ddd MMM DD YYYY HH:mm:ss [GMT]ZZ',
        'ddd MMM D YYYY HH:mm:ss [GMT]Z',
        'ddd MMM DD YYYY HH:mm:ss [GMT]Z',
        'YYYY-MM-DD HH:mm:ss',
        'YYYY-MM-DDTHH:mm:ss',
        'YYYY-MM-DDTHH:mm:ss.SSS',
        'YYYY-MM-DDTHH:mm:ssZ',
    ];
    const parsed = (0, dayjs_1.default)(str, formats, true);
    if (parsed.isValid()) {
        return parsed.toDate();
    }
    const native = new Date(str);
    if (!isNaN(native.getTime())) {
        return native;
    }
    return null;
}
//# sourceMappingURL=date.helper.js.map