"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "DischargeSummaryPrintout", {
    enumerable: true,
    get: function() {
        return DischargeSummaryPrintout;
    }
});
const _react = /*#__PURE__*/ _interop_require_default(require("react"));
const _renderer = require("@react-pdf/renderer");
const _constants = require("@tamanu/constants");
const _Layout = require("./Layout");
const _LetterheadSection = require("./LetterheadSection");
const _Typography = require("./Typography");
const _Table = require("./Table");
const _EncounterDetailsExtended = require("./printComponents/EncounterDetailsExtended");
const _PatientDetailsWithAddress = require("./printComponents/PatientDetailsWithAddress");
const _Page = require("../pdf/Page");
const _Text = require("../pdf/Text");
const _languageContext = require("../pdf/languageContext");
const _withDateTimeContext = require("../pdf/withDateTimeContext");
const _medication = require("../medication");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const borderStyle = '1 solid black';
const tableLabelWidth = 150;
const tablePadding = 10;
const generalStyles = _renderer.StyleSheet.create({
    tableContainer: {
        marginVertical: 5
    },
    sectionContainer: {
        marginVertical: 7
    }
});
const TableContainer = (props)=>/*#__PURE__*/ _react.default.createElement(_renderer.View, {
        style: generalStyles.tableContainer,
        ...props
    });
const SectionContainer = (props)=>/*#__PURE__*/ _react.default.createElement(_renderer.View, {
        style: generalStyles.sectionContainer,
        ...props
    });
const infoBoxStyles = _renderer.StyleSheet.create({
    row: {
        flexDirection: 'row',
        border: borderStyle
    },
    labelCol: {
        borderRight: borderStyle,
        width: tableLabelWidth,
        padding: tablePadding
    },
    dataCol: {
        flex: 1,
        padding: tablePadding
    },
    labelText: {
        fontSize: 10
    },
    infoText: {
        fontSize: 10
    }
});
const InfoBoxRow = (props)=>/*#__PURE__*/ _react.default.createElement(_renderer.View, {
        style: infoBoxStyles.row,
        ...props
    });
const InfoBoxLabelCol = (props)=>/*#__PURE__*/ _react.default.createElement(_renderer.View, {
        style: infoBoxStyles.labelCol,
        ...props
    });
const InfoBoxDataCol = (props)=>/*#__PURE__*/ _react.default.createElement(_renderer.View, {
        style: infoBoxStyles.dataCol,
        ...props
    });
const notesSectionStyles = _renderer.StyleSheet.create({
    notesBox: {
        border: borderStyle,
        minHeight: 76,
        padding: 10
    }
});
const extractOngoingConditions = (patientConditions)=>patientConditions.map((item)=>item?.diagnosis?.name);
const extractDiagnosesInfo = ({ diagnoses, getSetting })=>{
    const displayIcd10Codes = getSetting('features.displayIcd10CodesInDischargeSummary');
    if (!displayIcd10Codes) {
        return diagnoses.map((item)=>item?.diagnosis?.name);
    } else {
        return diagnoses.map((item)=>`${item?.diagnosis?.name} (${item?.diagnosis?.code})`);
    }
};
const extractProceduresInfo = ({ procedures, getSetting })=>{
    const displayProcedureCodes = getSetting('features.displayProcedureCodesInDischargeSummary');
    if (!displayProcedureCodes) {
        return procedures.map((item)=>item?.procedureType?.name);
    } else {
        return procedures.map((item)=>`${item?.procedureType?.name} (${item?.procedureType?.code})`);
    }
};
const InfoBox = ({ label, info })=>/*#__PURE__*/ _react.default.createElement(InfoBoxRow, null, /*#__PURE__*/ _react.default.createElement(InfoBoxLabelCol, null, /*#__PURE__*/ _react.default.createElement(_Text.Text, {
        bold: true,
        style: infoBoxStyles.labelText
    }, label)), /*#__PURE__*/ _react.default.createElement(InfoBoxDataCol, null, info.map((item, index)=>{
        return /*#__PURE__*/ _react.default.createElement(_Text.Text, {
            style: infoBoxStyles.infoText,
            key: index
        }, item);
    })));
const DiagnosesTable = ({ title, diagnoses, getSetting })=>/*#__PURE__*/ _react.default.createElement(InfoBox, {
        label: title,
        info: extractDiagnosesInfo({
            diagnoses,
            getSetting
        })
    });
const ProceduresTable = ({ procedures, getSetting })=>/*#__PURE__*/ _react.default.createElement(InfoBox, {
        label: "Procedures",
        info: extractProceduresInfo({
            procedures,
            getSetting
        })
    });
const NotesSection = ({ notes })=>/*#__PURE__*/ _react.default.createElement(_renderer.View, null, /*#__PURE__*/ _react.default.createElement(_Typography.P, {
        bold: true,
        fontSize: 11,
        mb: 3
    }, "Discharge planning notes"), /*#__PURE__*/ _react.default.createElement(_renderer.View, {
        style: notesSectionStyles.notesBox
    }, /*#__PURE__*/ _react.default.createElement(_Text.Text, {
        style: infoBoxStyles.infoText
    }, notes)));
const columns = (getTranslation, getEnumTranslation)=>[
        {
            key: 'medication',
            title: getTranslation('pdf.table.column.medication', 'Medication'),
            accessor: ({ medication, notes })=>/*#__PURE__*/ _react.default.createElement(_renderer.View, null, /*#__PURE__*/ _react.default.createElement(_Text.Text, null, medication?.name + `\n`), /*#__PURE__*/ _react.default.createElement(_Text.Text, {
                    style: {
                        fontFamily: 'Helvetica-Oblique'
                    }
                }, notes)),
            customStyles: {
                minWidth: 180
            }
        },
        {
            key: 'dose',
            title: getTranslation('pdf.table.column.dose', 'Dose'),
            accessor: (medication)=>{
                return /*#__PURE__*/ _react.default.createElement(_Text.Text, null, (0, _medication.getMedicationDoseDisplay)(medication, getTranslation, getEnumTranslation), medication?.isPrn && ` ${getTranslation('medication.table.prn', 'PRN')}`);
            }
        },
        {
            key: 'frequency',
            title: getTranslation('pdf.table.column.frequency', 'Frequency'),
            accessor: ({ frequency })=>(0, _medication.getTranslatedFrequency)(frequency, getTranslation),
            customStyles: {
                minWidth: 30
            }
        },
        {
            key: 'route',
            title: getTranslation('pdf.table.column.route', 'Route'),
            accessor: ({ route })=>getEnumTranslation(_constants.DRUG_ROUTE_LABELS, route)
        },
        {
            key: 'quantity',
            title: getTranslation('pdf.table.column.quantity', 'Quantity'),
            accessor: ({ quantity })=>quantity
        },
        {
            key: 'repeats',
            title: getTranslation('pdf.table.column.repeats', 'Repeats'),
            accessor: ({ repeats })=>repeats || 0
        }
    ];
const DischargeSummaryPrintoutComponent = ({ patientData, encounter, discharge, patientConditions, certificateData, getSetting })=>{
    const { getTranslation, getEnumTranslation } = (0, _languageContext.useLanguageContext)();
    const { logo } = certificateData;
    const { diagnoses, procedures, medications } = encounter;
    const visibleMedications = medications.filter((m)=>m.encounterPrescription?.isSelectedForDischarge && !m.medication.referenceDrug.isSensitive).sort((a, b)=>a.medication.name.localeCompare(b.medication.name));
    const visibleDiagnoses = diagnoses.filter(({ certainty })=>!_constants.DIAGNOSIS_CERTAINTIES_TO_HIDE.includes(certainty));
    const primaryDiagnoses = visibleDiagnoses.filter((d)=>d.isPrimary);
    const secondaryDiagnoses = visibleDiagnoses.filter((d)=>!d.isPrimary);
    const notes = discharge?.note;
    const { name: facilityName, address: facilityAddress, town: facilityTown } = discharge.address;
    // change header if facility details are present in discharge
    if (facilityName && facilityAddress && certificateData?.title) {
        certificateData = {
            ...certificateData,
            title: facilityName,
            subTitle: facilityTown ? `${facilityAddress}, ${facilityTown}` : facilityAddress
        };
    }
    return /*#__PURE__*/ _react.default.createElement(_renderer.Document, null, /*#__PURE__*/ _react.default.createElement(_Page.Page, {
        size: "A4",
        style: _Layout.styles.page
    }, /*#__PURE__*/ _react.default.createElement(_Layout.CertificateHeader, null, /*#__PURE__*/ _react.default.createElement(_LetterheadSection.LetterheadSection, {
        certificateTitle: "Patient discharge summary",
        letterheadConfig: certificateData,
        logoSrc: logo
    })), /*#__PURE__*/ _react.default.createElement(SectionContainer, null, /*#__PURE__*/ _react.default.createElement(_PatientDetailsWithAddress.PatientDetailsWithAddress, {
        patient: patientData,
        getSetting: getSetting
    })), /*#__PURE__*/ _react.default.createElement(SectionContainer, null, /*#__PURE__*/ _react.default.createElement(_EncounterDetailsExtended.EncounterDetailsExtended, {
        encounter: encounter,
        discharge: discharge
    })), /*#__PURE__*/ _react.default.createElement(SectionContainer, null, patientConditions.length > 0 && /*#__PURE__*/ _react.default.createElement(TableContainer, null, /*#__PURE__*/ _react.default.createElement(InfoBox, {
        label: "Ongoing conditions",
        info: extractOngoingConditions(patientConditions)
    })), primaryDiagnoses.length > 0 && /*#__PURE__*/ _react.default.createElement(TableContainer, null, /*#__PURE__*/ _react.default.createElement(DiagnosesTable, {
        title: "Primary diagnoses",
        diagnoses: primaryDiagnoses,
        getSetting: getSetting
    })), secondaryDiagnoses.length > 0 && /*#__PURE__*/ _react.default.createElement(TableContainer, null, /*#__PURE__*/ _react.default.createElement(DiagnosesTable, {
        title: "Secondary diagnoses",
        diagnoses: secondaryDiagnoses,
        getSetting: getSetting
    })), procedures.length > 0 && /*#__PURE__*/ _react.default.createElement(TableContainer, null, /*#__PURE__*/ _react.default.createElement(ProceduresTable, {
        procedures: procedures,
        getSetting: getSetting
    })), visibleMedications.length > 0 && /*#__PURE__*/ _react.default.createElement(TableContainer, null, /*#__PURE__*/ _react.default.createElement(_Table.Table, {
        data: visibleMedications,
        columns: columns(getTranslation, getEnumTranslation),
        getSetting: getSetting,
        columnStyle: {
            padding: '10px 5px'
        }
    }))), /*#__PURE__*/ _react.default.createElement(SectionContainer, null, /*#__PURE__*/ _react.default.createElement(NotesSection, {
        notes: notes
    }))));
};
const DischargeSummaryPrintout = (0, _languageContext.withLanguageContext)((0, _withDateTimeContext.withDateTimeContext)(DischargeSummaryPrintoutComponent));

//# sourceMappingURL=DischargeSummaryPrintout.js.map