"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "insertChangelogRecords", {
    enumerable: true,
    get: function() {
        return insertChangelogRecords;
    }
});
const _config = /*#__PURE__*/ _interop_require_default(require("config"));
const _runFunctionInBatches = require("@tamanu/utils/runFunctionInBatches");
const _sleepAsync = require("@tamanu/utils/sleepAsync");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const { pauseBetweenPersistedCacheBatchesInMilliseconds, persistedCacheBatchSize } = _config.default.sync;
const bulkCreateOptions = {
    /** Entries are immutable, so re-delivered records are skipped rather than merged */ ignoreDuplicates: true,
    returning: false
};
const insertChangelogRecords = async (models, changelogRecords, batchSize = persistedCacheBatchSize)=>{
    if (!changelogRecords.length) return;
    const { ChangeLog } = models;
    await (0, _runFunctionInBatches.runFunctionInBatches)(changelogRecords, async (batch)=>{
        await ChangeLog.bulkCreate(batch, bulkCreateOptions);
        await (0, _sleepAsync.sleepAsync)(pauseBetweenPersistedCacheBatchesInMilliseconds);
        // Result of this `runFunctionInBatches` is unused anyway; let the `bulkCreate` results get
        // garbage collected. Returning empty array simply for type safety.
        return [];
    }, batchSize);
};

//# sourceMappingURL=insertChangelogRecords.js.map