// src/platform/core/string-helpers.ts
function safeExcerpt(content, limit = 3e3) {
  if (!content) {
    return "";
  }
  if (content.length <= limit) {
    return content;
  }
  return `${content.slice(0, limit)}
... (truncated ${content.length - limit} chars)`;
}

// src/features/talk-to-figma/domain/planner.ts
function unwrapCodeFence(text) {
  const match = text.match(/```(?:json)?\s*([\s\S]*?)```/i);
  return match?.[1]?.trim() || text.trim();
}
function extractFirstJsonObject(text) {
  const source = unwrapCodeFence(text);
  const firstBraceIndex = source.indexOf("{");
  if (firstBraceIndex < 0) {
    throw new Error(`JSON object\uB97C \uCC3E\uC9C0 \uBABB\uD588\uC2B5\uB2C8\uB2E4. raw=${safeExcerpt(source, 160)}`);
  }
  let depth = 0;
  let inString = false;
  let escaped = false;
  for (let index = firstBraceIndex; index < source.length; index += 1) {
    const currentCharacter = source[index];
    if (escaped) {
      escaped = false;
      continue;
    }
    if (currentCharacter === "\\") {
      escaped = true;
      continue;
    }
    if (currentCharacter === '"') {
      inString = !inString;
      continue;
    }
    if (inString) {
      continue;
    }
    if (currentCharacter === "{") {
      depth += 1;
      continue;
    }
    if (currentCharacter !== "}") {
      continue;
    }
    depth -= 1;
    if (depth === 0) {
      return source.slice(firstBraceIndex, index + 1);
    }
  }
  throw new Error(`\uC644\uC804\uD55C JSON object\uB97C \uCC3E\uC9C0 \uBABB\uD588\uC2B5\uB2C8\uB2E4. raw=${safeExcerpt(source, 160)}`);
}
function normalizePlannerArguments(value) {
  if (!value || typeof value !== "object" || Array.isArray(value)) {
    return {};
  }
  return value;
}
function normalizeStringArray(value) {
  return Array.isArray(value) ? value.filter((entry) => typeof entry === "string" && entry.trim().length > 0) : [];
}
function normalizePlannerResponse(value) {
  if (!value || typeof value !== "object" || Array.isArray(value)) {
    throw new Error("\uD50C\uB798\uB108 \uC751\uB2F5\uC740 JSON object\uC5EC\uC57C \uD569\uB2C8\uB2E4.");
  }
  const raw = value;
  if (raw.type === "tool_call") {
    if (typeof raw.toolName !== "string" || !raw.toolName.trim()) {
      throw new Error("tool_call \uC751\uB2F5\uC5D0\uB294 \uBE44\uC5B4 \uC788\uC9C0 \uC54A\uC740 toolName\uC774 \uD544\uC694\uD569\uB2C8\uB2E4.");
    }
    return {
      arguments: normalizePlannerArguments(raw.arguments),
      reason: typeof raw.reason === "string" ? raw.reason.trim() : "",
      toolName: raw.toolName.trim(),
      type: "tool_call"
    };
  }
  if (raw.type === "final") {
    if (typeof raw.summary !== "string" || !raw.summary.trim()) {
      throw new Error("final \uC751\uB2F5\uC5D0\uB294 \uBE44\uC5B4 \uC788\uC9C0 \uC54A\uC740 summary\uAC00 \uD544\uC694\uD569\uB2C8\uB2E4.");
    }
    return {
      changedNodes: normalizeStringArray(raw.changedNodes),
      pendingIssues: normalizeStringArray(raw.pendingIssues),
      status: raw.status === "blocked" ? "blocked" : "done",
      summary: raw.summary.trim(),
      type: "final"
    };
  }
  throw new Error('\uD50C\uB798\uB108 \uC751\uB2F5 type\uC740 "tool_call" \uB610\uB294 "final" \uC774\uC5B4\uC57C \uD569\uB2C8\uB2E4.');
}
function parseTalkToFigmaPlannerResponse(text) {
  let parsedValue;
  try {
    parsedValue = JSON.parse(extractFirstJsonObject(text));
  } catch (error) {
    throw new Error(`\uD50C\uB798\uB108 \uC751\uB2F5 JSON \uD30C\uC2F1\uC5D0 \uC2E4\uD328\uD588\uC2B5\uB2C8\uB2E4. raw=${safeExcerpt(text.trim(), 240)} cause=${String(error)}`);
  }
  return normalizePlannerResponse(parsedValue);
}
function stringifyTalkToFigmaArguments(value) {
  return JSON.stringify(value, null, 2);
}

export { parseTalkToFigmaPlannerResponse, stringifyTalkToFigmaArguments };
