"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.optimizePrefixSize = optimizePrefixSize;
const fs_1 = __importDefault(require("fs"));
const readline_1 = __importDefault(require("readline"));
const util_1 = require("./util");
async function optimizePrefixSize(inIx) {
    let binSizeTotal = 0;
    let binCount = 0;
    let prefixSize = 5;
    for (; prefixSize < 40; prefixSize++) {
        const fileStream = fs_1.default.createReadStream(inIx);
        const rl = readline_1.default.createInterface({
            input: fileStream,
        });
        let lastPrefix;
        let writtenPrefix;
        let writtenPos = -util_1.binSize;
        let startPrefixPos = 0;
        let bytes = 0;
        let lastBin = 0;
        let maxBinSize = 0;
        for await (const line of rl) {
            const [word] = line.split(/\s/);
            const curPrefix = (0, util_1.getPrefix)(word, prefixSize);
            if (curPrefix !== lastPrefix) {
                startPrefixPos = bytes;
            }
            if (bytes - writtenPos >= util_1.binSize && curPrefix !== writtenPrefix) {
                const binSize = startPrefixPos - lastBin;
                binSizeTotal += binSize;
                maxBinSize = Math.max(binSize, maxBinSize);
                binCount++;
                lastBin = startPrefixPos;
                writtenPos = bytes;
                writtenPrefix = curPrefix;
            }
            lastPrefix = curPrefix;
            bytes += line.length + 1;
        }
        const avgBinSize = binSizeTotal / binCount;
        // some heuristics. note: binSizeTotal===0 means everything was lumped into
        // one bin
        if ((binSizeTotal === 0 && bytes > util_1.binSize) ||
            avgBinSize > 3 * util_1.binSize ||
            maxBinSize > 10 * util_1.binSize) {
            continue;
        }
        else {
            break;
        }
    }
    return prefixSize;
}
//# sourceMappingURL=optimizePrefixSize.js.map