"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNextDateOccurrence = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
exports.getNextDateOccurrence = {
    key: 'getNextDateOccurrence',
    title: 'Get next date occurrence',
    description: 'Get the next occurrence of a given date.',
    category: extensions_core_1.Category.WORKFLOW,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { referenceDate: referenceDateInput } = config_1.FieldsValidationSchema.parse(payload.fields);
        const now = new Date();
        const referenceDate = referenceDateInput !== null && referenceDateInput !== void 0 ? referenceDateInput : now;
        const targetMonth = referenceDate.getMonth(); // 0–11
        const targetDay = referenceDate.getDate(); // 1–31
        /**
         * Find the first occurrence of (targetMonth, targetDay)
         * that is strictly in the future compared to `afterDate`.
         */
        const findNextOccurrence = (afterDate) => {
            let candidateYear = afterDate.getFullYear();
            // Loop until we get a valid candidate strictly > afterDate
            // and with the same month/day (handles Feb 29 properly).
            // eslint-disable-next-line no-constant-condition
            while (true) {
                const candidate = new Date(candidateYear, targetMonth, targetDay, 0, 0, 0, 0);
                const isSameMonthDay = candidate.getMonth() === targetMonth &&
                    candidate.getDate() === targetDay;
                if (isSameMonthDay && candidate.getTime() > afterDate.getTime()) {
                    return candidate;
                }
                candidateYear += 1;
            }
        };
        // If the reference date is in the future, compare against it.
        // Otherwise, compare against "now".
        const anchorDate = referenceDate.getTime() > now.getTime() ? referenceDate : now;
        const nextOccurrence = findNextOccurrence(anchorDate);
        await onComplete({
            data_points: {
                nextDateOccurrence: nextOccurrence.toISOString(),
            },
        });
    },
};
//# sourceMappingURL=getNextDateOccurrence.js.map