// src/backends/opencode/translator.ts
function createOpencodeTranslator(opts) {
  const toolClass = /* @__PURE__ */ new Map();
  let sessionId = null;
  let seenFirstStep = false;
  let sawCustom = false;
  let totalCostUsd = 0;
  let totalInputTokens = 0;
  let totalOutputTokens = 0;
  let stepCount = 0;
  let turnResult = null;
  function classify(name) {
    if (opts.customToolNames.has(name)) return "custom";
    return "builtin";
  }
  function translate(raw) {
    const out = [];
    if (!raw || typeof raw !== "object") return out;
    if (typeof raw.sessionID === "string" && raw.sessionID) {
      sessionId = raw.sessionID;
    }
    const type = String(raw.type ?? "");
    switch (type) {
      case "step_start": {
        stepCount++;
        if (!seenFirstStep) {
          seenFirstStep = true;
        }
        return out;
      }
      case "text": {
        const part = raw.part ?? {};
        const text = part.text ?? "";
        if (text) {
          out.push({
            type: "agent.message",
            payload: {
              content: [{ type: "text", text }]
            }
          });
        }
        return out;
      }
      case "tool_use": {
        const part = raw.part ?? {};
        const toolUseId = part.callID ?? part.id ?? "";
        const name = part.tool ?? "unknown";
        const input = part.state?.input ?? {};
        if (!toolUseId) return out;
        const cls = classify(name);
        toolClass.set(toolUseId, cls);
        if (cls === "custom") {
          sawCustom = true;
          out.push({
            type: "agent.custom_tool_use",
            payload: { tool_use_id: toolUseId, name, input }
          });
        } else {
          out.push({
            type: "agent.tool_use",
            payload: { tool_use_id: toolUseId, name, input }
          });
        }
        return out;
      }
      case "step_finish": {
        const part = raw.part ?? {};
        totalCostUsd += part.cost ?? 0;
        if (part.tokens) {
          totalInputTokens += part.tokens.input ?? 0;
          totalOutputTokens += part.tokens.output ?? 0;
        }
        if (part.reason === "stop") {
          const usage = {
            input_tokens: totalInputTokens,
            output_tokens: totalOutputTokens,
            cache_read_input_tokens: 0,
            cache_creation_input_tokens: 0,
            cost_usd: totalCostUsd
          };
          const stopReason = sawCustom ? "custom_tool_call" : "end_turn";
          turnResult = {
            stopReason,
            usage,
            num_turns: stepCount
          };
        }
        return out;
      }
      case "error": {
        const err = raw.error;
        const data = err?.data;
        const msg = data?.message || err?.message || raw.message || "Unknown opencode error";
        out.push({
          type: "session.error",
          payload: { error: { type: "backend_error", message: msg } }
        });
        return out;
      }
      default:
        return out;
    }
  }
  return {
    translate,
    getBackendSessionId: () => sessionId,
    getTurnResult: () => turnResult,
    sawCustomToolUse: () => sawCustom
  };
}

export {
  createOpencodeTranslator
};
