// src/backends/factory/translator.ts
function createFactoryTranslator(opts) {
  const toolClass = /* @__PURE__ */ new Map();
  let sessionId = null;
  let lastText = "";
  let sawCustom = false;
  let sawCompletion = false;
  let numTurns = 0;
  let durationMs = 0;
  function classify(name) {
    if (opts.customToolNames.has(name)) return "custom";
    return "builtin";
  }
  function translate(raw) {
    const out = [];
    if (!raw || typeof raw !== "object") return out;
    const type = String(raw.type ?? "");
    if (type === "system" && raw.subtype === "init") {
      if (typeof raw.session_id === "string") sessionId = raw.session_id;
      return out;
    }
    if (type === "message" && raw.role === "assistant") {
      const text = typeof raw.text === "string" ? raw.text : "";
      if (text) {
        lastText = text;
        out.push({
          type: "agent.message",
          payload: { content: [{ type: "text", text }] }
        });
      }
      return out;
    }
    if (type === "tool_call") {
      const toolName = String(raw.toolName ?? "unknown");
      const toolId = String(raw.id ?? "");
      const parameters = raw.parameters ?? {};
      const cls = classify(toolName);
      toolClass.set(toolId, cls);
      if (cls === "custom") sawCustom = true;
      const useType = cls === "custom" ? "agent.custom_tool_use" : "agent.tool_use";
      out.push({
        type: useType,
        payload: {
          tool_use_id: toolId,
          name: toolName,
          input: parameters
        }
      });
      return out;
    }
    if (type === "tool_result") {
      const toolId = String(raw.id ?? "");
      const value = typeof raw.value === "string" ? raw.value : JSON.stringify(raw.value ?? "");
      const isError = raw.isError === true;
      const cls = toolClass.get(toolId);
      if (cls !== "custom") {
        out.push({
          type: "agent.tool_result",
          payload: {
            tool_use_id: toolId,
            content: value,
            is_error: isError
          }
        });
      }
      return out;
    }
    if (type === "completion") {
      sawCompletion = true;
      if (typeof raw.finalText === "string") lastText = raw.finalText;
      if (typeof raw.numTurns === "number") numTurns = raw.numTurns;
      if (typeof raw.durationMs === "number") durationMs = raw.durationMs;
      return out;
    }
    if (type === "error") {
      const msg = (typeof raw.message === "string" ? raw.message : null) || "Unknown factory error";
      out.push({
        type: "session.error",
        payload: { error: { type: "backend_error", message: msg } }
      });
      return out;
    }
    return out;
  }
  function getTurnResult() {
    if (!sawCompletion && !lastText) return null;
    return {
      stopReason: sawCustom ? "custom_tool_call" : "end_turn",
      usage: {
        input_tokens: 0,
        output_tokens: 0,
        cache_read_input_tokens: 0,
        cache_creation_input_tokens: 0,
        cost_usd: 0
      },
      num_turns: numTurns || 1
    };
  }
  return {
    translate,
    getBackendSessionId: () => sessionId,
    getTurnResult,
    sawCustomToolUse: () => sawCustom
  };
}

export {
  createFactoryTranslator
};
