"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.combineDateAndTime = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const zod_1 = require("zod");
const date_fns_1 = require("date-fns");
exports.combineDateAndTime = {
    key: 'combineDateAndTime',
    title: 'Combine date and time',
    description: 'Combine a reference date with a time string to create an ISO8601 datetime in UTC.',
    category: extensions_core_1.Category.DATA,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { fields: { referenceDate, timeString }, } = (0, extensions_core_1.validate)({
            schema: zod_1.z.object({
                fields: config_1.FieldsValidationSchema,
            }),
            payload,
        });
        let combinedDate;
        const parsedTime = (0, date_fns_1.parse)(timeString, 'HH:mm:ss', new Date());
        if ((0, date_fns_1.isValid)(parsedTime)) {
            const baseDate = new Date(referenceDate);
            combinedDate = new Date(baseDate);
            combinedDate.setHours(parsedTime.getHours());
            combinedDate.setMinutes(parsedTime.getMinutes());
            combinedDate.setSeconds(parsedTime.getSeconds());
            combinedDate.setMilliseconds(0);
        }
        else {
            const parsedDateTime = (0, date_fns_1.parseISO)(timeString);
            if (!(0, date_fns_1.isValid)(parsedDateTime)) {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: 'Invalid time string format' },
                            error: {
                                category: 'WRONG_INPUT',
                                message: 'Time string must be in ISO format HH:mm:ss (e.g., "14:30:00") or valid ISO8601 datetime with timezone (e.g., "2025-09-06T15:34:44+02:00")',
                            },
                        },
                    ],
                });
                return;
            }
            const baseDate = new Date(referenceDate);
            combinedDate = new Date(baseDate);
            combinedDate.setHours(parsedDateTime.getHours());
            combinedDate.setMinutes(parsedDateTime.getMinutes());
            combinedDate.setSeconds(parsedDateTime.getSeconds());
            combinedDate.setMilliseconds(0);
        }
        const isoDateTime = (0, date_fns_1.formatISO)(combinedDate);
        await onComplete({
            data_points: {
                combinedDateTime: isoDateTime,
            },
        });
    },
};
//# sourceMappingURL=combineDateAndTime.js.map