"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.combineDateAndTime = combineDateAndTime;
exports.calculateAttendanceHours = calculateAttendanceHours;
exports.determineAttendanceStatus = determineAttendanceStatus;
const date_fns_tz_1 = require("date-fns-tz");
function combineDateAndTime(baseDate, timeStr, timeZone) {
    const dateStr = typeof baseDate === 'string'
        ? baseDate.substring(0, 10)
        : (0, date_fns_tz_1.formatInTimeZone)(baseDate, timeZone, 'yyyy-MM-dd');
    const [hours, minutes] = timeStr.split(':').map(Number);
    const formattedTime = `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:00`;
    return (0, date_fns_tz_1.fromZonedTime)(`${dateStr} ${formattedTime}`, timeZone);
}
function calculateAttendanceHours(checkIn, checkOut, schedule, timeZone) {
    const totalWorkMs = checkOut.getTime() - checkIn.getTime();
    const work_hours = Math.max(0, Number((totalWorkMs / (1000 * 60 * 60)).toFixed(2)));
    let break_hours = 0;
    if (schedule.break_start && schedule.break_end) {
        const breakStart = combineDateAndTime(checkIn, schedule.break_start, timeZone);
        const breakEnd = combineDateAndTime(checkIn, schedule.break_end, timeZone);
        if (breakEnd > breakStart) {
            if (checkOut <= breakStart) {
                break_hours = 0;
            }
            else if (checkOut > breakStart && checkOut < breakEnd) {
                const elapsedBreakMs = checkOut.getTime() - breakStart.getTime();
                break_hours = Math.max(0, Number((elapsedBreakMs / (1000 * 60 * 60)).toFixed(2)));
            }
            else {
                const fullBreakMs = breakEnd.getTime() - breakStart.getTime();
                break_hours = Math.max(0, Number((fullBreakMs / (1000 * 60 * 60)).toFixed(2)));
            }
        }
    }
    const net_hours = Math.max(0, Number((work_hours - break_hours).toFixed(2)));
    let overtime_hours = 0;
    if (schedule.end_time) {
        const scheduledEnd = combineDateAndTime(checkIn, schedule.end_time, timeZone);
        if (checkOut > scheduledEnd) {
            const overtimeMs = checkOut.getTime() - scheduledEnd.getTime();
            overtime_hours = Math.max(0, Number((overtimeMs / (1000 * 60 * 60)).toFixed(2)));
        }
    }
    return {
        work_hours,
        break_hours,
        net_hours,
        overtime_hours,
    };
}
function determineAttendanceStatus(checkIn, startTimeStr, gracePeriodMinutes, timeZone) {
    const scheduledStart = combineDateAndTime(checkIn, startTimeStr, timeZone);
    const lateThreshold = new Date(scheduledStart.getTime() + (gracePeriodMinutes || 0) * 60 * 1000);
    return checkIn <= lateThreshold ? 'present' : 'late';
}
//# sourceMappingURL=attendance-hours.helper.js.map