"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeIxStream = makeIxStream;
exports.makeIx = makeIx;
const stream_1 = require("stream");
const external_sorting_1 = __importDefault(require("external-sorting"));
const tmp_1 = __importDefault(require("tmp"));
const command_exists_1 = require("command-exists");
const split2_1 = __importDefault(require("split2"));
const fs_1 = __importDefault(require("fs"));
const child_process_1 = require("child_process");
const TrixInputTransform_1 = require("./TrixInputTransform");
const TrixOutputTransform_1 = require("./TrixOutputTransform");
// Characters that may be part of a word
const wordMiddleChars = [];
const wordBeginChars = [];
function isalpha(c) {
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
function isdigit(c) {
    return c >= '0' && c <= '9';
}
function isalnum(c) {
    return isalpha(c) || isdigit(c);
}
function initCharTables() {
    for (let c = 0; c < 256; ++c) {
        if (isalnum(String.fromCharCode(c))) {
            wordBeginChars[c] = true;
            wordMiddleChars[c] = true;
        }
    }
    wordBeginChars['_'.charCodeAt(0)] = wordMiddleChars['_'.charCodeAt(0)] = true;
    wordMiddleChars['.'.charCodeAt(0)] = true;
    wordMiddleChars['-'.charCodeAt(0)] = true;
}
const isWin = typeof process !== 'undefined' ? process.platform === 'win32' : false;
async function makeIxStream(fileStream, outIxFilename) {
    return new Promise((resolve, reject) => {
        initCharTables();
        const out = fs_1.default.createWriteStream(outIxFilename);
        // see https://stackoverflow.com/questions/68835344/ for explainer of
        // writer
        // override locale to C, but keep other env vars
        if ((0, command_exists_1.sync)('sort') && !isWin) {
            const sort = (0, child_process_1.spawn)('sort', ['-k1,1'], {
                env: { ...process.env, LC_ALL: 'C' },
            });
            (0, stream_1.pipeline)(fileStream, (0, split2_1.default)(), new TrixInputTransform_1.TrixInputTransform(), sort.stdin, err => {
                if (err) {
                    reject(err);
                }
            });
            (0, stream_1.pipeline)(sort.stdout, (0, split2_1.default)(), new TrixOutputTransform_1.TrixOutputTransform(), out, err => {
                if (err) {
                    reject(err);
                }
                else {
                    resolve(true);
                }
            });
        }
        else {
            const dir = tmp_1.default.dirSync({
                prefix: 'jbrowse-trix-sort',
            });
            const tempDir = dir.name;
            const output = (0, split2_1.default)();
            (0, stream_1.pipeline)(output, new TrixOutputTransform_1.TrixOutputTransform(), out, err => {
                if (err) {
                    reject(err);
                }
            });
            (0, external_sorting_1.default)({
                input: (0, stream_1.pipeline)(fileStream, (0, split2_1.default)(), new TrixInputTransform_1.TrixInputTransform(), err => {
                    if (err) {
                        reject(err);
                    }
                }),
                output,
                tempDir,
            })
                .asc()
                .then(resolve, reject);
        }
    });
}
async function makeIx(inFile, outIndex) {
    return makeIxStream(fs_1.default.createReadStream(inFile), outIndex);
}
//# sourceMappingURL=makeIx.js.map