"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTrimmedSlice = void 0;
const util_1 = require("util");
const TEN_MEGABYTES = 10 * 1000 * 1000;
const getSlice = (logs, slice) => {
    if (slice?.length === 1 && slice[0]) {
        return { data: logs.slice(slice[0]), metadata: { count: logs.length, slice: [slice[0], logs.length] } };
    }
    if (slice?.length === 2 && slice[0] && slice[1]) {
        return { data: logs.slice(slice[0], slice[1]), metadata: { count: logs.length, slice: [slice[0], slice[1]] } };
    }
    return { data: logs, metadata: { count: logs.length, slice: [0, logs.length] } };
};
const countMethod = (entry) => {
    if (entry.args) {
        return entry.args.map((arg) => (0, util_1.inspect)(arg)).join('').length;
    }
    return (0, util_1.inspect)(entry).length;
};
const getTrimmedSlice = (logs, slice, maxSize = TEN_MEGABYTES) => {
    const currentSlice = getSlice(logs, slice);
    let bytes = 0;
    for (let i = currentSlice.data.length - 1; i >= 0; i--) {
        const count = countMethod(currentSlice.data[i]);
        bytes += count;
        if (bytes > maxSize) {
            return {
                data: currentSlice.data.slice(i + 1),
                metadata: {
                    count: currentSlice.metadata.count,
                    slice: [currentSlice.metadata.slice[0] + i + 1, currentSlice.metadata.slice[1]],
                },
            };
        }
    }
    return currentSlice;
};
exports.getTrimmedSlice = getTrimmedSlice;
//# sourceMappingURL=logs.js.map