"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.humanizeInterval = exports.year = exports.month = exports.week = exports.day = exports.hour = exports.minute = void 0;
exports.getDashboardPath = getDashboardPath;
exports.makePanelId = makePanelId;
exports.parseXYFromPanelId = parseXYFromPanelId;
exports.replaceExprParams = replaceExprParams;
exports.makeDiscoverValuesKey = makeDiscoverValuesKey;
const format_1 = require("../utils/format");
function getDashboardPath(type) {
    return `//sys/interface-monitoring/${type}`;
}
function makePanelId(item) {
    return `${item.gridPos.x}:${item.gridPos.y}:${item.type}`;
}
function parseXYFromPanelId(id) {
    const [x, y] = id.split(':');
    return { x: Number(x), y: Number(y) };
}
const SPECIAL_EXPR_ENV = {
    $__rate_interval: calculateRateInterval,
};
/**
 * Poor implementation of
 * https://github.com/grafana/grafana/blob/192d3783d5bede8362c1eed0c27422f431478b5a/pkg/promlib/models/query.go#L345-L366
 */
function calculateRateInterval(stepSec) {
    const minStep = 15000;
    return (0, exports.humanizeInterval)(0, Math.max(stepSec * 1000 + minStep, minStep * 4));
}
function replaceExprParams(expr, params, stepSec) {
    let res = (0, format_1.formatByParamsQuotedEnv)(expr, params, { sanitizeParams: (v) => (0, format_1.escapeChars)(v) });
    for (const k of Object.keys(SPECIAL_EXPR_ENV)) {
        const key = k;
        if (res.indexOf(key)) {
            res = res.replaceAll(key, SPECIAL_EXPR_ENV[key](stepSec));
        }
    }
    return res;
}
exports.minute = 60 * 1000;
exports.hour = exports.minute * 60;
exports.day = exports.hour * 24;
exports.week = exports.day * 7;
exports.month = exports.day * 30;
exports.year = exports.day * 365;
/**
 * Creates a human-readable representation of time interval
 * @param from Interval start, ms from epoch
 * @param to Interval end, ms from epoch
 * @returns Formatted string, e.g. 1d 3h
 */
const humanizeInterval = (from, to) => {
    const duration = to - from;
    if (duration >= exports.year) {
        const years = Math.floor(duration / exports.year);
        const months = Math.floor((duration % exports.year) / exports.month);
        return months === 0 || years > 4 ? `${years}y` : `${years}y ${months}mo`;
    }
    if (duration >= exports.month) {
        const months = Math.floor(duration / exports.month);
        const days = Math.floor((duration % exports.month) / exports.day);
        return days === 0 || months > 4 ? `${months}mo` : `${months}mo ${days}d`;
    }
    if (duration >= exports.week) {
        const weeks = Math.floor(duration / exports.week);
        const days = Math.floor((duration % exports.week) / exports.day);
        return days === 0 || weeks > 3 ? `${weeks}w` : `${weeks}w ${days}d`;
    }
    if (duration >= exports.day) {
        const days = Math.floor(duration / exports.day);
        const hours = Math.floor((duration % exports.day) / exports.hour);
        return hours === 0 || days > 4 ? `${days}d` : `${days}d ${hours}h`;
    }
    if (duration >= exports.hour) {
        const hours = Math.floor(duration / exports.hour);
        const minutes = Math.floor((duration % exports.hour) / exports.minute);
        return minutes === 0 || hours > 6 ? `${hours}h` : `${hours}h ${minutes}m`;
    }
    const minutes = Math.round(duration / exports.minute);
    return `${minutes}m`;
};
exports.humanizeInterval = humanizeInterval;
function makeDiscoverValuesKey({ label, match }) {
    return `_${label}_${match}_`;
}
