"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveTrackConfigPath = resolveTrackConfigPath;
exports.loadTrackConfig = loadTrackConfig;
exports.processTrackFiles = processTrackFiles;
exports.addTrackToConfig = addTrackToConfig;
exports.saveTrackConfigAndReport = saveTrackConfigAndReport;
exports.buildTrackParams = buildTrackParams;
exports.createTrackConfiguration = createTrackConfiguration;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const utils_1 = require("../utils");
const adapter_utils_1 = require("./add-track-utils/adapter-utils");
const file_operations_1 = require("./add-track-utils/file-operations");
const track_config_1 = require("./add-track-utils/track-config");
const validators_1 = require("./add-track-utils/validators");
function resolveTrackConfigPath(output) {
    const isDir = fs_1.default.lstatSync(output).isDirectory();
    return isDir ? `${output}/config.json` : output;
}
async function loadTrackConfig(targetConfigPath) {
    return await (0, utils_1.readJsonFile)(targetConfigPath);
}
async function processTrackFiles({ location, index, bed1, bed2, load, configDir, subDir, force, }) {
    if (load) {
        await Promise.all(Object.values((0, adapter_utils_1.guessFileNames)({ location, index, bed1, bed2 }))
            .filter(f => !!f)
            .map(src => (0, file_operations_1.loadFile)({
            src,
            destDir: configDir,
            mode: load,
            subDir,
            force,
        })));
    }
}
function addTrackToConfig({ configContents, trackConfig, trackId, force, overwrite, }) {
    const idx = (0, validators_1.validateTrackId)(configContents, trackId, force, overwrite);
    const updatedConfig = { ...configContents };
    if (idx !== -1) {
        (0, utils_1.debug)(`Found existing trackId ${trackId} in configuration`);
        (0, utils_1.debug)(`Overwriting track ${trackId} in configuration`);
        updatedConfig.tracks[idx] = trackConfig;
        return { updatedConfig, wasOverwritten: true };
    }
    else {
        updatedConfig.tracks.push(trackConfig);
        return { updatedConfig, wasOverwritten: false };
    }
}
async function saveTrackConfigAndReport({ config, targetConfigPath, name, trackId, wasOverwritten, }) {
    (0, utils_1.debug)(`Writing configuration to file ${targetConfigPath}`);
    await (0, utils_1.writeJsonFile)(targetConfigPath, config);
    console.log(`${wasOverwritten ? 'Overwrote' : 'Added'} track with name "${name}" and trackId "${trackId}" ${wasOverwritten ? 'in' : 'to'} ${targetConfigPath}`);
}
function buildTrackParams({ flags, location, adapter, configContents, }) {
    const trackType = flags.trackType || (0, adapter_utils_1.guessTrackType)(adapter.type);
    const trackId = flags.trackId || path_1.default.basename(location, path_1.default.extname(location));
    const name = flags.name || trackId;
    const assemblyNames = flags.assemblyNames || configContents.assemblies?.[0]?.name || '';
    return {
        trackType,
        trackId,
        name,
        assemblyNames,
    };
}
function createTrackConfiguration({ location, trackParams, flags, adapter, configContents, skipCheck, }) {
    return (0, track_config_1.buildTrackConfig)({
        location,
        trackType: trackParams.trackType,
        trackId: trackParams.trackId,
        name: trackParams.name,
        assemblyNames: trackParams.assemblyNames,
        category: flags.category,
        description: flags.description,
        config: flags.config,
        adapter,
        configContents,
        skipCheck,
    });
}
