"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.aggregateIndex = aggregateIndex;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const config_utils_1 = require("./config-utils");
const indexing_utils_1 = require("./indexing-utils");
async function aggregateIndex(flags) {
    const { out, target, tracks, assemblies, attributes, quiet, force, exclude, dryrun, prefixSize, } = flags;
    const outFlag = target || out || '.';
    const isDir = fs_1.default.lstatSync(outFlag).isDirectory();
    const confPath = isDir ? path_1.default.join(outFlag, 'config.json') : outFlag;
    const outLocation = path_1.default.dirname(confPath);
    const config = (0, config_utils_1.readConf)(confPath);
    const trixDir = path_1.default.join(outLocation, 'trix');
    if (!fs_1.default.existsSync(trixDir)) {
        fs_1.default.mkdirSync(trixDir);
    }
    const aggregateTextSearchAdapters = config.aggregateTextSearchAdapters || [];
    const asms = assemblies?.split(',') ||
        config.assemblies?.map(a => a.name) ||
        (config.assembly ? [config.assembly.name] : []);
    if (!asms.length) {
        throw new Error('No assemblies found');
    }
    for (const asm of asms) {
        const trackConfigs = (0, config_utils_1.getTrackConfigs)(confPath, tracks?.split(','), asm);
        if (!trackConfigs.length) {
            console.log(`Indexing assembly ${asm}...(no tracks found)...`);
            continue;
        }
        console.log(`Indexing assembly ${asm}...`);
        if (dryrun) {
            console.log(trackConfigs.map(e => `${e.trackId}	${e.adapter?.type}`).join('\n'));
        }
        else {
            const id = `${asm}-index`;
            const idx = aggregateTextSearchAdapters.findIndex(x => x.textSearchAdapterId === id);
            if (idx !== -1 && !force) {
                console.log(`Note: ${asm} has already been indexed with this configuration, use --force to overwrite this assembly. Skipping for now`);
                continue;
            }
            await (0, indexing_utils_1.indexDriver)({
                trackConfigs,
                outLocation,
                quiet,
                name: asm,
                attributes: attributes.split(','),
                typesToExclude: exclude.split(','),
                assemblyNames: [asm],
                prefixSize,
            });
            const trixConf = {
                type: 'TrixTextSearchAdapter',
                textSearchAdapterId: id,
                ixFilePath: {
                    uri: `trix/${asm}.ix`,
                    locationType: 'UriLocation',
                },
                ixxFilePath: {
                    uri: `trix/${asm}.ixx`,
                    locationType: 'UriLocation',
                },
                metaFilePath: {
                    uri: `trix/${asm}_meta.json`,
                    locationType: 'UriLocation',
                },
                assemblyNames: [asm],
            };
            if (idx === -1) {
                aggregateTextSearchAdapters.push(trixConf);
            }
            else {
                aggregateTextSearchAdapters[idx] = trixConf;
            }
        }
    }
    if (!dryrun) {
        (0, config_utils_1.writeConf)({
            ...config,
            aggregateTextSearchAdapters,
        }, confPath);
    }
}
