// src/backends/pi/translator.ts
function createPiTranslator(opts) {
  const toolClass = /* @__PURE__ */ new Map();
  let sessionId = null;
  let sawCustom = false;
  let sawAny = false;
  const usage = {
    input_tokens: 0,
    output_tokens: 0,
    cache_read_input_tokens: 0,
    cache_creation_input_tokens: 0,
    cost_usd: 0
  };
  let stopReason = "end_turn";
  let numTurns = 0;
  function classify(name) {
    if (opts.customToolNames.has(name)) return "custom";
    return "builtin";
  }
  function mapStopReason(pi) {
    switch (pi) {
      case "length":
        return "max_turns";
      case "error":
      case "aborted":
        return "error";
      default:
        return "end_turn";
    }
  }
  function emitAssistantMessage(msg, out) {
    sawAny = true;
    const content = Array.isArray(msg.content) ? msg.content : [];
    for (const item of content) {
      if (item && item.type === "text") {
        const text = item.text;
        if (typeof text === "string" && text.length > 0) {
          out.push({
            type: "agent.message",
            payload: { content: [{ type: "text", text }] }
          });
        }
      }
    }
    if (msg.usage) {
      if (typeof msg.usage.input === "number") usage.input_tokens += msg.usage.input;
      if (typeof msg.usage.output === "number") usage.output_tokens += msg.usage.output;
      if (typeof msg.usage.cacheRead === "number") usage.cache_read_input_tokens += msg.usage.cacheRead;
      if (typeof msg.usage.cacheWrite === "number") usage.cache_creation_input_tokens += msg.usage.cacheWrite;
      if (msg.usage.cost && typeof msg.usage.cost.total === "number") {
        usage.cost_usd += msg.usage.cost.total;
      }
    }
    stopReason = mapStopReason(msg.stopReason);
  }
  function translate(raw) {
    const out = [];
    if (!raw || typeof raw !== "object") return out;
    const type = String(raw.type ?? "");
    if (type === "session") {
      if (typeof raw.id === "string") sessionId = raw.id;
      return out;
    }
    if (type === "turn_start") {
      numTurns += 1;
      return out;
    }
    if (type === "message_end") {
      const msg = raw.message;
      if (msg && msg.role === "assistant") {
        emitAssistantMessage(msg, out);
      }
      return out;
    }
    if (type === "tool_execution_start") {
      const toolName = String(raw.toolName ?? "unknown");
      const toolId = String(raw.toolCallId ?? "");
      const args = raw.args ?? {};
      const cls = classify(toolName);
      toolClass.set(toolId, cls);
      if (cls === "custom") sawCustom = true;
      sawAny = true;
      const useType = cls === "custom" ? "agent.custom_tool_use" : "agent.tool_use";
      out.push({
        type: useType,
        payload: {
          tool_use_id: toolId,
          name: toolName,
          input: args
        }
      });
      return out;
    }
    if (type === "tool_execution_end") {
      const toolId = String(raw.toolCallId ?? "");
      const isError = raw.isError === true;
      const cls = toolClass.get(toolId);
      if (cls === "custom") return out;
      const result = raw.result;
      let content;
      if (typeof result === "string") {
        content = result;
      } else if (result == null) {
        content = "";
      } else {
        content = JSON.stringify(result);
      }
      out.push({
        type: "agent.tool_result",
        payload: {
          tool_use_id: toolId,
          content,
          is_error: isError
        }
      });
      return out;
    }
    if (type === "agent_end") {
      sawAny = true;
      return out;
    }
    return out;
  }
  function getTurnResult() {
    if (!sawAny) return null;
    return {
      stopReason: sawCustom ? "custom_tool_call" : stopReason,
      usage,
      num_turns: numTurns || 1
    };
  }
  return {
    translate,
    getBackendSessionId: () => sessionId,
    getTurnResult,
    sawCustomToolUse: () => sawCustom
  };
}

export {
  createPiTranslator
};
