import { SCOPE_ADDRESS_VAR } from "../../../utils/cel-context.js";
export function scopeOrigin(ancestor) {
  return ancestor.name ? `${ancestor.typeName}\u300C${ancestor.name}\u300D` : ancestor.typeName;
}
const SCOPE_REF_RE = new RegExp(
  `${SCOPE_ADDRESS_VAR}\\[("(?:[^"\\\\]|\\\\.)*")\\]\\.([A-Za-z_$][\\w$]*)`,
  "g"
);
export function scanScopeRefs(text) {
  const out = [];
  SCOPE_REF_RE.lastIndex = 0;
  let match;
  while ((match = SCOPE_REF_RE.exec(text)) !== null) {
    let id;
    try {
      id = JSON.parse(match[1]);
    } catch {
      continue;
    }
    out.push({
      from: match.index,
      to: match.index + match[0].length,
      id,
      member: match[2]
    });
  }
  return out;
}
export function buildScopeLookup(ancestry) {
  const lookup = /* @__PURE__ */ new Map();
  for (const ancestor of ancestry) {
    const members = /* @__PURE__ */ new Map();
    for (const member of ancestor.members) {
      members.set(member.key, { label: member.label, type: member.type });
    }
    lookup.set(ancestor.id, { origin: scopeOrigin(ancestor), members });
  }
  return lookup;
}
