"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNextWorkday = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
exports.getNextWorkday = {
    key: 'getNextWorkday',
    title: 'Get Next Workday',
    description: 'Get the next workday',
    category: extensions_core_1.Category.WORKFLOW,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { referenceDate: referenceDateInput, includeReferenceDate } = config_1.FieldsValidationSchema.parse(payload.fields);
        const referenceDate = referenceDateInput !== null && referenceDateInput !== void 0 ? referenceDateInput : new Date();
        // Normalize to start of day to avoid timezone issues
        referenceDate.setUTCHours(0, 0, 0, 0);
        const SUNDAY = 0;
        const SATURDAY = 6;
        const isWeekend = (date) => date.getDay() === SUNDAY || date.getDay() === SATURDAY;
        const referenceDateIsWeekday = !isWeekend(referenceDate);
        // If reference date is a weekday and we should *not* include it, move forward by one day
        if (!includeReferenceDate && !isWeekend(referenceDate)) {
            referenceDate.setDate(referenceDate.getDate() + 1);
        }
        // If resultDate is a weekend, keep moving forward to the next weekday
        while (isWeekend(referenceDate)) {
            referenceDate.setDate(referenceDate.getDate() + 1);
        }
        const nextWorkday = referenceDate.toISOString();
        await onComplete({
            data_points: {
                nextWorkday,
                referenceDateIsWeekday: String(referenceDateIsWeekday),
            },
        });
    },
};
//# sourceMappingURL=getNextWorkday.js.map