import Includes from "../../Types/BaseDatabase/Includes";
import { DashboardVariableType, } from "../../Types/Dashboard/DashboardVariable";
/*
 * Applies dashboard variables to a metric query at render time.
 *
 * For each TelemetryAttribute variable with a selected value, the
 * variable's bound attribute key is injected into the query's attribute
 * filter. An empty/unset selection means "All" — no filter is applied,
 * and any previously-set filter on that key is removed so the widget
 * shows the cross-cluster view by default.
 *
 * Multi-select variables emit an `Includes` operator on the same map
 * key so the server's WHERE-builder produces `attributes[key] IN (...)`.
 */
export default class DashboardVariableInterpolation {
    static resolveValue(variable) {
        var _a, _b;
        if (variable.isMultiSelect) {
            const values = (variable.selectedValues || []).filter((v) => {
                return Boolean(v);
            });
            if (values.length > 0) {
                return { multi: values };
            }
            // No multi-select picks. Fall through to default value handling.
        }
        const v = (_b = (_a = variable.selectedValue) !== null && _a !== void 0 ? _a : variable.defaultValue) !== null && _b !== void 0 ? _b : undefined;
        if (v === undefined || v === null || v === "") {
            return undefined;
        }
        return { scalar: v };
    }
    /*
     * Generic helper: take a plain attributes map and return a new map
     * with all TelemetryAttribute variables applied. Used by every widget
     * type whose filter is shaped like `{ [attrKey]: value | operator }`
     * (metric charts, log streams). Returns the original reference when
     * nothing changes so React.memo can short-circuit.
     */
    static applyToAttributes(attributes, variables) {
        const source = attributes || {};
        if (!variables || variables.length === 0) {
            return source;
        }
        const telemetryAttributeVariables = variables.filter((v) => {
            return (v.type === DashboardVariableType.TelemetryAttribute &&
                Boolean(v.attributeKey));
        });
        if (telemetryAttributeVariables.length === 0) {
            return source;
        }
        const next = Object.assign({}, source);
        let changed = false;
        for (const variable of telemetryAttributeVariables) {
            const key = variable.attributeKey;
            const resolved = DashboardVariableInterpolation.resolveValue(variable);
            if (!resolved) {
                if (key in next) {
                    delete next[key];
                    changed = true;
                }
                continue;
            }
            if (resolved.multi && resolved.multi.length > 0) {
                next[key] = new Includes(resolved.multi);
                changed = true;
                continue;
            }
            if (resolved.scalar !== undefined && next[key] !== resolved.scalar) {
                next[key] = resolved.scalar;
                changed = true;
            }
        }
        return changed ? next : source;
    }
    static applyToQueryConfig(queryConfig, variables) {
        var _a;
        if (!variables || variables.length === 0) {
            return queryConfig;
        }
        const filterData = (_a = queryConfig.metricQueryData) === null || _a === void 0 ? void 0 : _a.filterData;
        if (!filterData) {
            return queryConfig;
        }
        const originalAttributes = filterData.attributes || {};
        const nextAttributes = DashboardVariableInterpolation.applyToAttributes(originalAttributes, variables);
        if (nextAttributes === originalAttributes) {
            return queryConfig;
        }
        return Object.assign(Object.assign({}, queryConfig), { metricQueryData: Object.assign(Object.assign({}, queryConfig.metricQueryData), { filterData: Object.assign(Object.assign({}, filterData), { attributes: nextAttributes }) }) });
    }
    static applyToQueryConfigs(queryConfigs, variables) {
        if (!variables || variables.length === 0) {
            return queryConfigs;
        }
        return queryConfigs.map((config) => {
            return DashboardVariableInterpolation.applyToQueryConfig(config, variables);
        });
    }
}
//# sourceMappingURL=VariableInterpolation.js.map