"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalDateTimeTransformer = void 0;
exports.parseDate = parseDate;
exports.serializeDate = serializeDate;
const dayjs_1 = __importDefault(require("dayjs"));
const utc_1 = __importDefault(require("dayjs/plugin/utc"));
const timezone_1 = __importDefault(require("dayjs/plugin/timezone"));
dayjs_1.default.extend(utc_1.default);
dayjs_1.default.extend(timezone_1.default);
function dateToUtcComponentString(d) {
    const pad = (n, w = 2) => String(n).padStart(w, "0");
    const yyyy = d.getUTCFullYear();
    const mm = pad(d.getUTCMonth() + 1);
    const dd = pad(d.getUTCDate());
    const hh = pad(d.getUTCHours());
    const mi = pad(d.getUTCMinutes());
    const ss = pad(d.getUTCSeconds());
    const ms = pad(d.getUTCMilliseconds(), 3);
    return `${yyyy}-${mm}-${dd} ${hh}:${mi}:${ss}.${ms}`;
}
function getWallClockConfig() {
    return {
        tz: process.env.SOLIDX_WALL_TIME_TIMEZONE || process.env.SOLIDX_TIMEZONE || "UTC",
        wallTimeMode: (process.env.SOLIDX_TIME_STORED_AS_WALL_TIME || "").toLowerCase() === "true",
    };
}
function parseDate(date) {
    const { tz, wallTimeMode } = getWallClockConfig();
    if (!wallTimeMode)
        return dayjs_1.default.utc(date);
    return (0, dayjs_1.default)(date).tz(tz);
}
function serializeDate(date) {
    const { wallTimeMode } = getWallClockConfig();
    if (!wallTimeMode)
        return date.toISOString();
    return parseDate(date).format("YYYY-MM-DD HH:mm:ss.SSS");
}
exports.LocalDateTimeTransformer = {
    from(value) {
        if (value === undefined)
            return undefined;
        if (value === null)
            return null;
        const { tz, wallTimeMode } = getWallClockConfig();
        if (!wallTimeMode) {
            return (0, dayjs_1.default)(value).toDate();
        }
        const naive = value instanceof Date ? dateToUtcComponentString(value) : String(value);
        return dayjs_1.default.tz(naive, tz).utc().toDate();
    },
    to(value) {
        if (value === undefined)
            return undefined;
        if (value === null)
            return null;
        const wallTimeStr = serializeDate(value);
        return dayjs_1.default.utc(wallTimeStr).toDate();
    },
};
//# sourceMappingURL=local-date-time-transformer.js.map