#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.main = main;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const util_1 = require("util");
// Command imports
const add_assembly_1 = require("./commands/add-assembly");
const add_connection_1 = require("./commands/add-connection");
const add_track_1 = require("./commands/add-track");
const add_track_json_1 = require("./commands/add-track-json");
const admin_server_1 = require("./commands/admin-server");
const create_1 = require("./commands/create");
const make_pif_1 = require("./commands/make-pif");
const remove_track_1 = require("./commands/remove-track");
const set_default_session_1 = require("./commands/set-default-session");
const sort_bed_1 = require("./commands/sort-bed");
const sort_gff_1 = require("./commands/sort-gff");
const text_index_1 = require("./commands/text-index");
const upgrade_1 = require("./commands/upgrade");
const commands = {
    create: create_1.run,
    'add-assembly': add_assembly_1.run,
    'add-track': add_track_1.run,
    'text-index': text_index_1.run,
    'admin-server': admin_server_1.run,
    upgrade: upgrade_1.run,
    'make-pif': make_pif_1.run,
    'sort-gff': sort_gff_1.run,
    'sort-bed': sort_bed_1.run,
    'add-connection': add_connection_1.run,
    'add-track-json': add_track_json_1.run,
    'remove-track': remove_track_1.run,
    'set-default-session': set_default_session_1.run,
};
async function main(args) {
    try {
        const { values: flags, positionals } = (0, util_1.parseArgs)({
            args,
            options: {
                help: {
                    type: 'boolean',
                    short: 'h',
                    default: false,
                },
                version: {
                    type: 'boolean',
                    short: 'v',
                    default: false,
                },
            },
            allowPositionals: true,
            strict: false, // Allow unknown flags to be passed to subcommands
        });
        // Check if help or version is requested at the global level
        if (flags.help && positionals.length === 0) {
            showGlobalHelp();
            return;
        }
        if (flags.version && positionals.length === 0) {
            const packageJson = JSON.parse(fs_1.default.readFileSync(path_1.default.join(__dirname, '../package.json'), 'utf8'));
            console.log(`@jbrowse/cli version ${packageJson.version}`);
            return;
        }
        const commandName = positionals[0];
        if (!commandName) {
            console.error('Error: Missing command');
            showGlobalHelp();
            process.exit(1);
        }
        const command = commands[commandName];
        // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
        if (!command) {
            console.error(`Error: Unknown command "${commandName}"`);
            console.error(`Available commands: ${Object.keys(commands).join(', ')}`);
            process.exit(1);
        }
        // Pass the remaining arguments to the command
        const commandArgs = args.slice(1); // Remove the command name from args
        await command(commandArgs);
    }
    catch (error) {
        console.error('Error:', error instanceof Error ? error.message : error);
        process.exit(1);
    }
}
function showGlobalHelp() {
    console.log(`
JBrowse CLI

USAGE
  $ jbrowse <command> [options]

COMMANDS
  create               Downloads and installs the latest JBrowse 2 release
  add-assembly         Add an assembly to a JBrowse 2 configuration
  add-track            Add a track to a JBrowse 2 configuration
  text-index           Make a text-indexing file for any given track(s)
  admin-server         Start up a small admin server for JBrowse configuration
  upgrade              Upgrades JBrowse 2 to latest version
  make-pif             Creates pairwise indexed PAF (PIF), with bgzip and tabix
  sort-gff             Helper utility to sort GFF files for tabix
  sort-bed             Helper utility to sort BED files for tabix
  add-connection       Add a connection to a JBrowse 2 configuration
  add-track-json       Add a track configuration directly from a JSON hunk
  remove-track         Remove a track configuration from a JBrowse 2 configuration
  set-default-session  Set a default session with views and tracks

OPTIONS
  -h, --help     Show help
  -v, --version  Show version

Use "jbrowse <command> --help" for more information about a command.
`);
}
