"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get gcodeScanningChunkSize () {
        return gcodeScanningChunkSize;
    },
    get readLastLinesLocal () {
        return readLastLinesLocal;
    }
});
const _promises = require("node:fs/promises");
const gcodeScanningChunkSize = 64 * 1024;
async function readLastLinesLocal(filePath, numberOfLines) {
    const file = await (0, _promises.open)(filePath, "r");
    try {
        const buffer = Buffer.alloc(gcodeScanningChunkSize);
        const fileSize = await file.stat();
        let lines = [];
        let position = fileSize.size;
        let iterationsLeft = 100;
        while(lines.length <= numberOfLines && position > 0){
            iterationsLeft--;
            if (iterationsLeft <= 0) {
                throw new Error("Too many iterations reached, 'readLastLines' aborted");
            }
            const bytesToRead = Math.min(gcodeScanningChunkSize, position);
            position -= bytesToRead;
            await file.read(buffer, 0, bytesToRead, position);
            const chunk = buffer.toString("utf-8", 0, bytesToRead);
            lines = chunk.split("\n").concat(lines);
            await file.close();
        }
        return lines.slice(-numberOfLines);
    } catch (e) {
        await file.close();
        throw e;
    }
}

//# sourceMappingURL=gcode.utils.js.map