"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = run;
const util_1 = require("util");
const utils_1 = require("../utils");
const pif_generator_1 = require("./make-pif-utils/pif-generator");
const validators_1 = require("./make-pif-utils/validators");
async function run(args) {
    const options = {
        help: {
            type: 'boolean',
            short: 'h',
        },
        out: {
            type: 'string',
            description: 'Where to write the output file. will write ${file}.pif.gz and ${file}.pif.gz.tbi',
        },
        csi: {
            type: 'boolean',
            description: 'Create a CSI index for the PIF file instead of TBI',
        },
    };
    const { values: flags, positionals } = (0, util_1.parseArgs)({
        args,
        options,
        allowPositionals: true,
    });
    const description = 'creates pairwise indexed PAF (PIF), with bgzip and tabix';
    const examples = [
        '$ jbrowse make-pif input.paf # creates input.pif.gz in same directory',
        '',
        '$ jbrowse make-pif input.paf --out output.pif.gz # specify output file, creates output.pif.gz.tbi also',
    ];
    if (flags.help) {
        (0, utils_1.printHelp)({
            description,
            examples,
            usage: 'jbrowse make-pif <file> [options]',
            options,
        });
        return;
    }
    const file = positionals[0];
    (0, validators_1.validateFileArgument)(file);
    (0, validators_1.validateRequiredCommands)();
    const { out, csi = false } = flags;
    const outputFile = (0, pif_generator_1.getOutputFilename)(file, out);
    try {
        const child = (0, pif_generator_1.spawnSortProcess)(outputFile, csi);
        await (0, pif_generator_1.createPIF)(file, child.stdin);
        child.stdin.end();
        await (0, pif_generator_1.waitForProcessClose)(child);
    }
    catch (error) {
        console.error('Error during PIF creation:', error);
        process.exit(1);
    }
}
