"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDefaultKnexChaintracksOptions = createDefaultKnexChaintracksOptions;
const knex_1 = require("knex");
const ChaintracksFs_1 = require("./util/ChaintracksFs");
const ChaintracksStorageKnex_1 = require("./Storage/ChaintracksStorageKnex");
const BulkIngestorCDNBabbage_1 = require("./Ingest/BulkIngestorCDNBabbage");
const ChaintracksFetch_1 = require("./util/ChaintracksFetch");
const LiveIngestorWhatsOnChainPoll_1 = require("./Ingest/LiveIngestorWhatsOnChainPoll");
const BulkIngestorWhatsOnChainCdn_1 = require("./Ingest/BulkIngestorWhatsOnChainCdn");
const BulkFileDataManager_1 = require("./util/BulkFileDataManager");
/**
 *
 * @param chain
 * @param rootFolder defaults to "./data/"
 * @returns
 */
function createDefaultKnexChaintracksOptions(chain, rootFolder = './data/', knexConfig, whatsonchainApiKey = '', maxPerFile = 100000, maxRetained = 2, fetch, cdnUrl = 'https://cdn.projectbabbage.com/blockheaders/', liveHeightThreshold = 2000, reorgHeightThreshold = 400, bulkMigrationChunkSize = 500, batchInsertLimit = 400, addLiveRecursionLimit = 36) {
    fetch || (fetch = new ChaintracksFetch_1.ChaintracksFetch());
    const bfo = {
        chain,
        fetch,
        maxPerFile,
        maxRetained,
        fromKnownSourceUrl: cdnUrl
    };
    const bulkFileDataManager = new BulkFileDataManager_1.BulkFileDataManager(bfo);
    if (!knexConfig) {
        knexConfig = {
            client: 'better-sqlite3',
            connection: { filename: ChaintracksFs_1.ChaintracksFs.pathJoin(rootFolder, `${chain}Net_chaintracks.sqlite`) },
            useNullAsDefault: true
        };
    }
    const knexInstance = (0, knex_1.knex)(knexConfig);
    const so = {
        chain,
        knex: knexInstance,
        bulkFileDataManager,
        liveHeightThreshold,
        reorgHeightThreshold,
        bulkMigrationChunkSize,
        batchInsertLimit
    };
    const storage = new ChaintracksStorageKnex_1.ChaintracksStorageKnex(so);
    const co = {
        chain,
        storage,
        bulkIngestors: [],
        liveIngestors: [],
        addLiveRecursionLimit,
        logging: (...args) => console.log(new Date().toISOString(), ...args),
        readonly: false
    };
    const jsonResource = `${chain}NetBlockHeaders.json`;
    const bulkCdnOptions = {
        chain,
        jsonResource,
        fetch,
        cdnUrl,
        maxPerFile
    };
    co.bulkIngestors.push(new BulkIngestorCDNBabbage_1.BulkIngestorCDNBabbage(bulkCdnOptions));
    const wocOptions = {
        chain,
        apiKey: whatsonchainApiKey,
        timeout: 30000,
        userAgent: 'BabbageWhatsOnChainServices',
        enableCache: true,
        chainInfoMsecs: 5000
    };
    const bulkOptions = {
        ...wocOptions,
        jsonResource,
        idleWait: 5000
    };
    co.bulkIngestors.push(new BulkIngestorWhatsOnChainCdn_1.BulkIngestorWhatsOnChainCdn(bulkOptions));
    const liveOptions = {
        ...wocOptions,
        idleWait: 100000
    };
    co.liveIngestors.push(new LiveIngestorWhatsOnChainPoll_1.LiveIngestorWhatsOnChainPoll(liveOptions));
    return co;
}
//# sourceMappingURL=createDefaultKnexChaintracksOptions.js.map