import { SCOPE_ADDRESS_VAR, SELECTIONS_VAR, STEPS_VAR } from "../../../utils/cel-context.js";
import { scopeOrigin } from "./scope-refs.js";
const SUPPRESSED_VARS = /* @__PURE__ */ new Set([SCOPE_ADDRESS_VAR, SELECTIONS_VAR, STEPS_VAR]);
export function buildVarEntries(celContext, extraVars) {
  const out = [];
  const seen = /* @__PURE__ */ new Set();
  for (const [name, meta] of Object.entries(celContext)) {
    if (SUPPRESSED_VARS.has(name)) continue;
    seen.add(name);
    out.push({ id: name, group: "var", insert: name, display: name, label: meta.label, type: meta.type, description: meta.description });
  }
  for (const [name, meta] of Object.entries(extraVars ?? {})) {
    if (seen.has(name) || SUPPRESSED_VARS.has(name)) continue;
    out.push({ id: name, group: "var", insert: name, display: name, label: meta.label, type: meta.type, description: meta.description });
  }
  return out;
}
export function buildSelectionEntries(roster) {
  return roster.map((field) => ({
    id: `selection:${field.id}`,
    group: "selection",
    insert: `${SELECTIONS_VAR}[${JSON.stringify(field.id)}]`,
    display: field.name,
    label: "\u9009\u4E2D\u9879",
    type: "dyn"
  }));
}
export function buildStepEntries(roster) {
  return roster.filter((step) => step.valid).map((step) => ({
    id: `step:${step.id}`,
    group: "step",
    insert: `${STEPS_VAR}[${JSON.stringify(step.id)}]`,
    display: step.label,
    label: "\u524D\u5E8F\u6B65\u9AA4",
    type: "dyn",
    description: step.description
  }));
}
export function selectShadowedScopes(ancestry) {
  const claimed = /* @__PURE__ */ new Set();
  const out = [];
  for (const frame of ancestry) {
    const members = frame.members.filter((m) => claimed.has(m.shortcut ?? m.key));
    if (members.length > 0) out.push({ ...frame, members });
    for (const s of frame.shadows ?? frame.members.map((m) => m.shortcut ?? m.key)) {
      claimed.add(s);
    }
  }
  return out;
}
export function buildScopeEntries(ancestry) {
  const out = [];
  for (const ancestor of ancestry) {
    const origin = scopeOrigin(ancestor);
    for (const member of ancestor.members) {
      out.push({
        id: `scope:${ancestor.id}:${member.key}`,
        group: "scope",
        insert: `${SCOPE_ADDRESS_VAR}[${JSON.stringify(ancestor.id)}].${member.key}`,
        display: `${origin} \u203A ${member.label}`,
        label: origin,
        type: member.type,
        description: member.description
      });
    }
  }
  return out;
}
