import { tail, sum, reverse } from "ramda";
export const getCorrectedPoints = ({ after, before, firstEntry, points, }) => {
    const nowInSeconds = Math.round(new Date().valueOf() / 1000);
    const afterAbsolute = after > 0 ? after : nowInSeconds + after;
    const beforeAbsolute = before > 0 ? before : nowInSeconds + before;
    if (afterAbsolute < firstEntry) {
        // take into account first_entry
        const realAfter = Math.max(afterAbsolute, firstEntry);
        const requestedRange = beforeAbsolute - afterAbsolute;
        const availableRange = beforeAbsolute - realAfter;
        return Math.round((points * availableRange) / requestedRange);
    }
    return null;
};
export const addPointsDygraph = (data, nrOfPointsToFill) => {
    const viewUpdateEvery = data.view_update_every;
    if (!data.result.data.length) {
        return data;
    }
    const firstAddedTimestamp = data.result.data[0][0] - nrOfPointsToFill * viewUpdateEvery;
    const emptyPoint = tail(data.result.labels).map(() => null);
    const nulls = new Array(nrOfPointsToFill)
        .fill(null)
        .map((_, i) => [firstAddedTimestamp + i * viewUpdateEvery, ...emptyPoint]);
    return {
        ...data,
        after: data.after - viewUpdateEvery * nrOfPointsToFill,
        result: {
            ...data.result,
            data: nulls.concat(data.result.data),
        },
    };
};
export const fillMissingData = (data, nrOfPointsToFill) => {
    if (data.format === "json") {
        return addPointsDygraph(data, nrOfPointsToFill);
    }
    return data;
};
const emptyArray = [];
export const transformResults = (data, format, shouldRevertFlip) => {
    if (format === "array" && data.format === "json") {
        if (Array.isArray(data.result))
            return data;
        const dataResult = shouldRevertFlip
            ? reverse(data.result.data)
            : data.result.data;
        return {
            ...data,
            // set proper output type so other functions like fillMissingData work properly
            format: "array",
            result: dataResult.reduce((acc, pointData) => {
                pointData.shift();
                return [...acc, sum(pointData)];
            }, emptyArray),
        };
    }
    return data;
};
export const mapDefaultAggrMethod = (unit) => {
    if (unit.length === 0) {
        return "sum";
    }
    const avgUnits = {
        percentage: true,
        percent: true,
        "rotations/min": true,
        ratio: true,
        seconds: true,
        "seconds ago": true,
        milliseconds: true,
        millisec: true,
        ms: true,
        "log2 s": true,
        minutes: true,
        hours: true,
        interval: true,
        ticks: true,
        celsius: true,
        c: true,
        mhz: true,
        hz: true,
        volts: true,
        kwh: true,
        ampere: true,
        amps: true,
        dbm: true,
        value: true,
        stratum: true,
        units: true,
        watt: true,
        temperature: true,
        "random number": true,
        rpm: true,
        quadro: true,
        "adv/item": true,
        multiplier: true,
        geforce: true,
    };
    if (avgUnits[unit.toLowerCase()]) {
        return "avg";
    }
    const avgUnitsRegExes = [".*%.*", ".*/operation", ".*/run", ".*/ run", ".*/request"];
    if (avgUnitsRegExes.some((regEx) => {
        const regExpression = RegExp(regEx, "i");
        return regExpression.test(unit.toLowerCase());
    })) {
        return "avg";
    }
    return "sum";
};
//# sourceMappingURL=fill-missing-data.js.map