"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapLocationForFiles = mapLocationForFiles;
exports.buildTrackConfig = buildTrackConfig;
exports.addSyntenyAssemblyNames = addSyntenyAssemblyNames;
const path_1 = __importDefault(require("path"));
const json_parse_better_errors_1 = __importDefault(require("json-parse-better-errors"));
const validators_1 = require("./validators");
const SYNTENY_ADAPTERS = new Set([
    'PAFAdapter',
    'PairwiseIndexedPAFAdapter',
    'DeltaAdapter',
    'ChainAdapter',
    'MashMapAdapter',
    'MCScanAnchorsAdapter',
    'MCScanSimpleAnchorsAdapter',
]);
function mapLocationForFiles(p, load, subDir) {
    return !p || (0, validators_1.isUrl)(p) || load === 'inPlace'
        ? p
        : path_1.default.join(subDir || '', path_1.default.basename(p));
}
function buildTrackConfig({ location, trackType, trackId, name, assemblyNames, category, description, config, adapter, configContents, skipCheck, }) {
    const configObj = config ? (0, json_parse_better_errors_1.default)(config) : {};
    const finalTrackId = trackId || path_1.default.basename(location, path_1.default.extname(location));
    const finalName = name || finalTrackId;
    const finalAssemblyNames = assemblyNames || configContents.assemblies?.[0]?.name || '';
    const trackConfig = {
        type: trackType,
        trackId: finalTrackId,
        name: finalName,
        adapter,
        category: category?.split(',').map(c => c.trim()),
        assemblyNames: finalAssemblyNames.split(',').map(a => a.trim()),
        description,
        ...configObj,
    };
    // Special handling for AlignmentsTrack
    if (trackType === 'AlignmentsTrack') {
        const assembly = configContents.assemblies?.find(asm => asm.name === finalAssemblyNames);
        if (assembly) {
            // @ts-expect-error
            trackConfig.adapter.sequenceAdapter = assembly.sequence.adapter;
        }
        else if (!skipCheck) {
            console.error(`Error: Failed to find assemblyName ${finalAssemblyNames}`);
            process.exit(1);
        }
    }
    return trackConfig;
}
function addSyntenyAssemblyNames(adapter, assemblyNames) {
    if (SYNTENY_ADAPTERS.has(adapter.type)) {
        return {
            ...adapter,
            assemblyNames: assemblyNames?.split(',').map(a => a.trim()),
        };
    }
    return adapter;
}
