"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkWorkingHours = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const date_fns_1 = require("date-fns");
const date_fns_tz_1 = require("date-fns-tz");
const config_1 = require("./config");
exports.checkWorkingHours = {
    key: 'checkWorkingHours',
    title: 'Check Working Hours',
    description: 'Check if the current time is within working hours and calculate minutes to next working hours as well as the datetime of the next working hours if not',
    category: extensions_core_1.Category.WORKFLOW,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { workingHoursStart, workingHoursEnd, timezone } = config_1.FieldsValidationSchema.parse(payload.fields);
        // Get current UTC time
        const now = new Date();
        // Convert current UTC time to the specified timezone
        const currentTimeInTimezone = (0, date_fns_tz_1.utcToZonedTime)(now, timezone);
        // Parse working hours
        const [startHour, startMinute] = workingHoursStart.split(':').map(Number);
        const [endHour, endMinute] = workingHoursEnd.split(':').map(Number);
        // Validate that end time is after start time
        const startTotalMinutes = startHour * 60 + startMinute;
        const endTotalMinutes = endHour * 60 + endMinute;
        if (endTotalMinutes <= startTotalMinutes) {
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: {
                            en: 'Working hours end time must be after start time. Cross-midnight hours are not supported.',
                        },
                        error: {
                            category: 'BAD_REQUEST',
                            message: 'Working hours end time must be after start time',
                        },
                    },
                ],
            });
            return;
        }
        // Get current time in minutes since midnight using date-fns
        const currentHour = (0, date_fns_1.getHours)(currentTimeInTimezone);
        const currentMinute = (0, date_fns_1.getMinutes)(currentTimeInTimezone);
        const currentTotalMinutes = currentHour * 60 + currentMinute;
        // Check if within working hours
        const isWithinWorkingHours = currentTotalMinutes >= startTotalMinutes &&
            currentTotalMinutes < endTotalMinutes;
        let minutesToNextWorkingHours;
        let nextWorkingHoursDatetime;
        // Calculate minutes to next working hours only if not currently within working hours
        if (!isWithinWorkingHours) {
            let nextWorkingHoursDate;
            if (currentTotalMinutes < startTotalMinutes) {
                // Before working hours today
                minutesToNextWorkingHours = startTotalMinutes - currentTotalMinutes;
                // Set the datetime to today at working hours start time
                nextWorkingHoursDate = (0, date_fns_1.setMinutes)((0, date_fns_1.setHours)(currentTimeInTimezone, startHour), startMinute);
            }
            else {
                // After working hours today, so next working hours is tomorrow
                const minutesUntilMidnight = 24 * 60 - currentTotalMinutes;
                minutesToNextWorkingHours = minutesUntilMidnight + startTotalMinutes;
                // Set the datetime to tomorrow at working hours start time
                const tomorrow = (0, date_fns_1.addDays)(currentTimeInTimezone, 1);
                nextWorkingHoursDate = (0, date_fns_1.setMinutes)((0, date_fns_1.setHours)(tomorrow, startHour), startMinute);
            }
            // Convert the zoned time back to UTC for consistent output
            const nextWorkingHoursUtc = (0, date_fns_tz_1.zonedTimeToUtc)(nextWorkingHoursDate, timezone);
            nextWorkingHoursDatetime = nextWorkingHoursUtc.toISOString();
        }
        await onComplete({
            data_points: {
                isWithinWorkingHours: String(isWithinWorkingHours),
                minutesToNextWorkingHours: minutesToNextWorkingHours !== undefined
                    ? String(minutesToNextWorkingHours)
                    : undefined,
                nextWorkingHoursDatetime,
            },
        });
    },
};
//# sourceMappingURL=checkWorkingHours.js.map