"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runIxIxx = runIxIxx;
exports.indexFiles = indexFiles;
exports.indexDriver = indexDriver;
exports.prepareFileTrackConfigs = prepareFileTrackConfigs;
const path_1 = __importDefault(require("path"));
const stream_1 = require("stream");
const ixixx_1 = require("ixixx");
const adapter_utils_1 = require("./adapter-utils");
const common_1 = require("../../types/common");
const gff3Adapter_1 = require("../../types/gff3Adapter");
const vcfAdapter_1 = require("../../types/vcfAdapter");
async function runIxIxx({ readStream, outLocation, name, prefixSize, }) {
    await (0, ixixx_1.ixIxxStream)(readStream, path_1.default.join(outLocation, 'trix', `${name}.ix`), path_1.default.join(outLocation, 'trix', `${name}.ixx`), prefixSize);
}
async function* indexFiles({ trackConfigs, attributes, outLocation, quiet, typesToExclude, }) {
    for (const config of trackConfigs) {
        const { adapter, textSearching } = config;
        const { type } = adapter || {};
        const { indexingFeatureTypesToExclude = typesToExclude, indexingAttributes = attributes, } = textSearching || {};
        const loc = (0, adapter_utils_1.getAdapterLocation)(adapter);
        if (!loc) {
            continue;
        }
        if (type === 'Gff3TabixAdapter' || type === 'Gff3Adapter') {
            yield* (0, gff3Adapter_1.indexGff3)({
                config,
                attributesToIndex: indexingAttributes,
                inLocation: (0, adapter_utils_1.getLoc)(loc),
                outLocation,
                typesToExclude: indexingFeatureTypesToExclude,
                quiet,
            });
        }
        else if (type === 'VcfTabixAdapter' || type === 'VcfAdapter') {
            yield* (0, vcfAdapter_1.indexVcf)({
                config,
                attributesToIndex: indexingAttributes,
                inLocation: (0, adapter_utils_1.getLoc)(loc),
                outLocation,
                typesToExclude: indexingFeatureTypesToExclude,
                quiet,
            });
        }
    }
}
async function indexDriver({ trackConfigs, attributes, outLocation, name, quiet, typesToExclude, assemblyNames, prefixSize, }) {
    const readStream = stream_1.Readable.from(indexFiles({
        trackConfigs,
        attributes,
        outLocation,
        quiet,
        typesToExclude,
    }));
    await runIxIxx({
        readStream,
        outLocation,
        name,
        prefixSize: typeof prefixSize === 'string' ? parseInt(prefixSize) : prefixSize,
    });
    await (0, common_1.generateMeta)({
        trackConfigs,
        attributes,
        outLocation,
        name,
        typesToExclude,
        assemblyNames,
    });
}
function prepareFileTrackConfigs(files, fileIds) {
    const trackConfigs = files
        .map(file => (0, common_1.guessAdapterFromFileName)(file))
        .filter(fileConfig => (0, common_1.supported)(fileConfig.adapter?.type));
    if (fileIds?.length) {
        for (const [i, element] of fileIds.entries()) {
            trackConfigs[i].trackId = element;
        }
    }
    return trackConfigs;
}
