"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get buildEncounterLinkedSyncFilter () {
        return buildEncounterLinkedSyncFilter;
    },
    get buildEncounterLinkedSyncFilterJoins () {
        return buildEncounterLinkedSyncFilterJoins;
    }
});
const _sequelize = require("sequelize");
const _lodash = require("lodash");
function buildEncounterLinkedSyncFilterJoins(tablesToTraverse) {
    return tablesToTraverse.slice(1).map((table, i)=>{
        const currentTable = (0, _lodash.isString)(tablesToTraverse[i]) ? tablesToTraverse[i] : tablesToTraverse[i]?.model.tableName;
        const joinTable = (0, _lodash.isString)(table) ? table : table.model.tableName;
        const joinColumn = (0, _lodash.isString)(table) ? `${_sequelize.Utils.singularize(table)}_id` : table.joinColumn;
        const joinType = (0, _lodash.isObject)(table) && table.required ? 'INNER' : 'LEFT';
        return `
        ${joinType} JOIN ${joinTable} ON ${currentTable}.${joinColumn} = ${joinTable}.id
      `;
    }).join('\n');
}
function buildEncounterLinkedSyncFilter(tablesToTraverse, markedForSyncPatientsTable) {
    const joins = buildEncounterLinkedSyncFilterJoins(tablesToTraverse);
    const firstTable = (0, _lodash.isString)(tablesToTraverse[0]) ? tablesToTraverse[0] : tablesToTraverse[0]?.model.tableName;
    return `
    ${joins}
    WHERE encounters.patient_id IN (SELECT patient_id FROM ${markedForSyncPatientsTable})
    AND ${firstTable}.updated_at_sync_tick > :since
  `;
}

//# sourceMappingURL=buildEncounterLinkedSyncFilter.js.map