import { PharmacyOrderPrescription } from './PharmacyOrderPrescription';
const updateInvoiceQuantityForPrescription = async (instance)=>{
    const { models } = instance.sequelize;
    // Use eagerly loaded association if available (bulk hooks), otherwise fetch
    const pharmacyOrder = instance.pharmacyOrder ?? await models.PharmacyOrder.findByPk(instance.pharmacyOrderId);
    if (!pharmacyOrder?.isDischargePrescription) return;
    const prescription = await models.Prescription.findByPk(instance.prescriptionId, {
        include: [
            {
                model: models.EncounterPrescription,
                as: 'encounterPrescription',
                required: true,
                include: [
                    {
                        model: models.Encounter,
                        as: 'encounter',
                        required: true
                    }
                ]
            }
        ]
    });
    if (prescription) {
        await prescription.recalculateAndApplyInvoiceQuantity();
    }
};
const destroyPharmacyOrder = async (instance)=>{
    const pharmacyOrder = await instance.sequelize.models.PharmacyOrder.findByPk(instance.pharmacyOrderId, {
        include: [
            {
                association: 'pharmacyOrderPrescriptions'
            }
        ]
    });
    if (pharmacyOrder && (!pharmacyOrder?.pharmacyOrderPrescriptions || !pharmacyOrder?.pharmacyOrderPrescriptions.length)) {
        await pharmacyOrder.destroy();
    }
};
export const afterCreateHook = async (instance)=>{
    await updateInvoiceQuantityForPrescription(instance);
};
export const afterUpdateHook = async (instance)=>{
    await updateInvoiceQuantityForPrescription(instance);
};
export const afterDestroyHook = async (instance)=>{
    await updateInvoiceQuantityForPrescription(instance);
    await destroyPharmacyOrder(instance);
};
export const afterBulkCreateHook = async (instances)=>{
    for (const instance of instances){
        await updateInvoiceQuantityForPrescription(instance);
    }
};
export const afterBulkUpdateHook = async (options)=>{
    const { where } = options;
    const instances = await PharmacyOrderPrescription.findAll({
        where
    });
    for (const instance of instances){
        await updateInvoiceQuantityForPrescription(instance);
    }
};
export const afterBulkDestroyHook = async (options)=>{
    const { where } = options;
    const instances = await PharmacyOrderPrescription.findAll({
        where,
        paranoid: false
    });
    for (const instance of instances){
        await updateInvoiceQuantityForPrescription(instance);
    }
};

//# sourceMappingURL=hooks.js.map