"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readConf = readConf;
exports.writeConf = writeConf;
exports.getConfigPath = getConfigPath;
exports.ensureTrixDir = ensureTrixDir;
exports.getAssemblyNames = getAssemblyNames;
exports.getTrackConfigs = getTrackConfigs;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const common_1 = require("../../types/common");
function readConf(configPath) {
    return JSON.parse(fs_1.default.readFileSync(configPath, 'utf8'));
}
function writeConf(obj, configPath) {
    fs_1.default.writeFileSync(configPath, JSON.stringify(obj, null, 2));
}
function getConfigPath(outFlag) {
    const isDir = fs_1.default.lstatSync(outFlag).isDirectory();
    const configPath = isDir ? path_1.default.join(outFlag, 'config.json') : outFlag;
    const outLocation = path_1.default.dirname(configPath);
    return { configPath, outLocation };
}
function ensureTrixDir(outLocation) {
    const trixDir = path_1.default.join(outLocation, 'trix');
    if (!fs_1.default.existsSync(trixDir)) {
        fs_1.default.mkdirSync(trixDir);
    }
    return trixDir;
}
function getAssemblyNames(config, assemblies) {
    const asms = assemblies?.split(',') ||
        config.assemblies?.map(a => a.name) ||
        (config.assembly ? [config.assembly.name] : []);
    if (!asms.length) {
        throw new Error('No assemblies found');
    }
    return asms;
}
function getTrackConfigs(configPath, trackIds, assemblyName) {
    const { tracks } = readConf(configPath);
    if (!tracks) {
        return [];
    }
    const trackIdsToIndex = trackIds || tracks.map(track => track.trackId);
    return trackIdsToIndex
        .map(trackId => {
        const currentTrack = tracks.find(t => trackId === t.trackId);
        if (!currentTrack) {
            throw new Error(`Track not found in config.json for trackId ${trackId}, please add track configuration before indexing.`);
        }
        return currentTrack;
    })
        .filter(track => (0, common_1.supported)(track.adapter?.type))
        .filter(track => assemblyName ? track.assemblyNames.includes(assemblyName) : true);
}
