// src/json-extract.ts
function stripCodeFences(text) {
  return text.replace(/```(?:json)?\s*([\s\S]*?)```/gi, (_m, inner) => String(inner).trim());
}
function extractJsonCandidates(text) {
  const trimmed = text.trim();
  const cleaned = stripCodeFences(trimmed);
  const candidates = [];
  if (cleaned.length > 0) candidates.push(cleaned);
  candidates.push(...scanBalancedJsonBlocks(cleaned));
  const objMatch = cleaned.match(/\{[\s\S]*\}/);
  if (objMatch) candidates.push(objMatch[0]);
  const seen = /* @__PURE__ */ new Set();
  return candidates.map((c) => c.trim()).filter((c) => c.length > 0).filter((c) => {
    if (seen.has(c)) return false;
    seen.add(c);
    return true;
  });
}
function scanBalancedJsonBlocks(text) {
  const out = [];
  const opens = /* @__PURE__ */ new Set(["{", "["]);
  const closes = { "{": "}", "[": "]" };
  for (let i = 0; i < text.length; i++) {
    const start = text[i];
    if (!opens.has(start)) continue;
    const expectedClose = closes[start];
    let depth = 0;
    let inString = false;
    let escape = false;
    for (let j = i; j < text.length; j++) {
      const ch = text[j];
      if (inString) {
        if (escape) {
          escape = false;
        } else if (ch === "\\") {
          escape = true;
        } else if (ch === '"') {
          inString = false;
        }
        continue;
      }
      if (ch === '"') {
        inString = true;
        continue;
      }
      if (ch === start) depth++;
      if (ch === expectedClose) depth--;
      if (depth === 0) {
        out.push(text.slice(i, j + 1).trim());
        i = j;
        break;
      }
    }
  }
  return out;
}

export {
  stripCodeFences,
  extractJsonCandidates
};
//# sourceMappingURL=chunk-UZB5KHKX.js.map