// src/backends/codex/translator.ts
function createCodexTranslator(opts) {
  const toolClass = /* @__PURE__ */ new Map();
  let sessionId = null;
  let turnCount = 0;
  let lastText = "";
  let totalInputTokens = 0;
  let totalOutputTokens = 0;
  let totalCostUsd = 0;
  let sawCustom = false;
  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 === "thread.started") {
      if (typeof raw.thread_id === "string") sessionId = raw.thread_id;
      return out;
    }
    if (type === "item.completed" && raw.item && typeof raw.item === "object") {
      const item = raw.item;
      if (item.type === "reasoning" && typeof item.text === "string") {
        out.push({
          type: "agent.thinking",
          payload: { content: [{ type: "thinking", thinking: item.text }] }
        });
        return out;
      }
      if (item.type === "agent_message" && typeof item.text === "string") {
        lastText = item.text;
        out.push({
          type: "agent.message",
          payload: { content: [{ type: "text", text: item.text }] }
        });
        return out;
      }
      if (item.type === "command_execution" && typeof item.id === "string") {
        const name = "command";
        const cls = classify(name);
        toolClass.set(item.id, 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: item.id,
            name,
            input: { command: item.command ?? "" }
          }
        });
        if (cls !== "custom") {
          out.push({
            type: "agent.tool_result",
            payload: {
              tool_use_id: item.id,
              content: item.output ?? item.result ?? "",
              is_error: false
            }
          });
        }
        return out;
      }
      if (item.type === "mcp_tool_call" && typeof item.id === "string") {
        const name = item.name ?? "mcp_tool";
        const cls = classify(name);
        toolClass.set(item.id, 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: item.id,
            name,
            input: item.input ?? {}
          }
        });
        if (cls !== "custom") {
          out.push({
            type: "agent.tool_result",
            payload: {
              tool_use_id: item.id,
              content: item.output ?? "",
              is_error: false
            }
          });
        }
        return out;
      }
      if (item.type === "file_change" && typeof item.id === "string") {
        const name = "file_edit";
        const cls = classify(name);
        toolClass.set(item.id, 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: item.id,
            name,
            input: { path: item.path ?? "", action: item.action ?? "edit" }
          }
        });
        return out;
      }
    }
    if (type === "turn.completed") {
      turnCount++;
      const usage = raw.usage;
      if (usage) {
        if (typeof usage.input_tokens === "number") {
          totalInputTokens += usage.input_tokens;
        }
        if (typeof usage.output_tokens === "number") {
          totalOutputTokens += usage.output_tokens;
        }
        if (typeof usage.cost_usd === "number") {
          totalCostUsd += usage.cost_usd;
        }
      }
      return out;
    }
    if (type === "error") {
      const msg = (typeof raw.message === "string" ? raw.message : null) || raw.error?.message || "Unknown codex error";
      out.push({
        type: "session.error",
        payload: { error: { type: "backend_error", message: msg } }
      });
      return out;
    }
    if (type === "turn.failed") {
      const err = raw.error;
      const msg = err?.message || "Turn failed";
      out.push({
        type: "session.error",
        payload: { error: { type: "backend_error", message: msg } }
      });
      return out;
    }
    return out;
  }
  function getTurnResult() {
    if (turnCount === 0 && !lastText) return null;
    return {
      stopReason: sawCustom ? "custom_tool_call" : "end_turn",
      usage: {
        input_tokens: totalInputTokens,
        output_tokens: totalOutputTokens,
        cache_read_input_tokens: 0,
        cache_creation_input_tokens: 0,
        cost_usd: totalCostUsd
      },
      num_turns: turnCount || 1
    };
  }
  return {
    translate,
    getBackendSessionId: () => sessionId,
    getTurnResult,
    sawCustomToolUse: () => sawCustom
  };
}

export {
  createCodexTranslator
};
