"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyDayOffAttendance = applyDayOffAttendance;
exports.revertDayOffAttendance = revertDayOffAttendance;
const date_fns_tz_1 = require("date-fns-tz");
const typeorm_1 = require("typeorm");
const entities_1 = require("../config/database/entities");
const helpers_1 = require("./helpers");
const workedStatuses = new Set([
    entities_1.AttendanceStatusEnum.present,
    entities_1.AttendanceStatusEnum.late,
    entities_1.AttendanceStatusEnum.half_day,
    entities_1.AttendanceStatusEnum.check_in,
    entities_1.AttendanceStatusEnum.on_break,
]);
async function applyDayOffAttendance(db, params) {
    const notePrefix = params.notePrefix ?? 'Day off approved';
    const dateKey = (0, date_fns_tz_1.formatInTimeZone)(params.date, params.timeZone, 'yyyy-MM-dd');
    const { startUtc, endUtc } = (0, helpers_1.getUtcStartEndOfDate)(dateKey, params.timeZone);
    const note = `${notePrefix}: ${params.reason}`;
    const existing = await db.attendances.findFirst({
        where: {
            member_id: params.member_id,
            owner_id: params.owner_id,
            date: (0, typeorm_1.Between)(startUtc, endUtc),
        },
    });
    if (existing) {
        const updateData = { note };
        if (!workedStatuses.has(existing.status)) {
            updateData.status = entities_1.AttendanceStatusEnum.weekend;
        }
        const updated = await db.attendances.update({
            where: { id: existing.id },
            data: updateData,
        });
        return updated.id;
    }
    const created = await db.attendances.create({
        data: {
            member_id: params.member_id,
            owner_id: params.owner_id,
            date: startUtc,
            status: entities_1.AttendanceStatusEnum.weekend,
            type: entities_1.AttendanceTypeEnum.in_office,
            note,
        },
    });
    return created.id;
}
async function revertDayOffAttendance(db, params) {
    const notePrefix = params.notePrefix ?? 'Day off approved';
    const dateKey = (0, date_fns_tz_1.formatInTimeZone)(params.date, params.timeZone, 'yyyy-MM-dd');
    const { startUtc, endUtc } = (0, helpers_1.getUtcStartEndOfDate)(dateKey, params.timeZone);
    const existing = await db.attendances.findFirst({
        where: {
            member_id: params.member_id,
            owner_id: params.owner_id,
            date: (0, typeorm_1.Between)(startUtc, endUtc),
        },
    });
    if (!existing)
        return;
    const noteMatches = existing.note?.startsWith(`${notePrefix}:`) ||
        existing.note?.startsWith('Off-day work request approved:');
    if (!noteMatches)
        return;
    if (workedStatuses.has(existing.status)) {
        await db.attendances.update({
            where: { id: existing.id },
            data: { note: '' },
        });
        return;
    }
    await db.attendances.update({
        where: { id: existing.id },
        data: {
            status: entities_1.AttendanceStatusEnum.absent,
            note: '',
        },
    });
}
//# sourceMappingURL=day-off-attendance.js.map