type DashboardVariable = { name?: unknown; default?: unknown }; // Resolve only exact `$name` references declared by the dashboard. The query // planner remains the authority for accepted scope keys and values; unresolved // references are preserved so they fail visibly instead of widening a query. export function resolveDashboardScope( rawScope: Record, variables: unknown[], ): Record { const defaults = new Map(); for (const candidate of variables) { if (!candidate || typeof candidate !== 'object') continue; const variable = candidate as DashboardVariable; if (typeof variable.name === 'string' && variable.name !== '' && typeof variable.default === 'string') { defaults.set(variable.name, variable.default); } } return Object.fromEntries(Object.entries(rawScope).flatMap(([key, value]) => { if (typeof value !== 'string') return []; const reference = /^\$([A-Za-z][A-Za-z0-9_-]{0,63})$/.exec(value); if (!reference) return [[key, value]]; return [[key, defaults.get(reference[1]) ?? value]]; })); }