import { apiRequest } from "./api.js";
import { readJsonSafe } from "./crmFixtures.js";
async function submitTextExtraction(request, token, input) {
  const text = input?.text ?? "Test email from John Doe <john@example.com> requesting 10 widgets at $5 each.";
  const title = input?.title ?? `QA Fixture ${Date.now()}`;
  const response = await apiRequest(request, "POST", "/api/inbox_ops/extract", {
    token,
    data: { text, title, metadata: input?.metadata }
  });
  const body = await readJsonSafe(response);
  return {
    ok: response.ok(),
    emailId: body?.emailId ?? void 0,
    error: body?.error ?? void 0,
    status: response.status()
  };
}
async function waitForEmailProcessed(request, token, emailId, timeoutMs = 3e4) {
  const pollInterval = 1e3;
  const deadline = Date.now() + timeoutMs;
  while (Date.now() < deadline) {
    const response = await apiRequest(request, "GET", `/api/inbox_ops/emails/${emailId}`, { token });
    if (!response.ok()) return null;
    const body = await readJsonSafe(response);
    const status = body?.email?.status;
    if (status === "processed" || status === "needs_review" || status === "failed") {
      const proposalsResponse = await apiRequest(request, "GET", "/api/inbox_ops/proposals?pageSize=5", { token });
      const proposalsBody = await readJsonSafe(proposalsResponse);
      const proposal = proposalsBody?.items?.find((p) => p.inboxEmailId === emailId);
      return { status, proposalId: proposal?.id };
    }
    await new Promise((resolve) => setTimeout(resolve, pollInterval));
  }
  return null;
}
async function deleteInboxEmail(request, token, emailId) {
  await apiRequest(request, "DELETE", `/api/inbox_ops/emails/${emailId}`, { token }).catch(() => {
  });
}
async function listInboxEmails(request, token, params) {
  const searchParams = new URLSearchParams();
  if (params?.status) searchParams.set("status", params.status);
  if (params?.page) searchParams.set("page", String(params.page));
  if (params?.pageSize) searchParams.set("pageSize", String(params.pageSize));
  const path = `/api/inbox_ops/emails${searchParams.toString() ? `?${searchParams}` : ""}`;
  const response = await apiRequest(request, "GET", path, { token });
  const body = await readJsonSafe(response);
  return { items: body?.items ?? [], total: body?.total ?? 0 };
}
async function listInboxProposals(request, token, params) {
  const searchParams = new URLSearchParams();
  if (params?.status) searchParams.set("status", params.status);
  if (params?.page) searchParams.set("page", String(params.page));
  if (params?.pageSize) searchParams.set("pageSize", String(params.pageSize));
  const path = `/api/inbox_ops/proposals${searchParams.toString() ? `?${searchParams}` : ""}`;
  const response = await apiRequest(request, "GET", path, { token });
  const body = await readJsonSafe(response);
  return { items: body?.items ?? [], total: body?.total ?? 0 };
}
async function fetchProposalDetail(request, token, proposalId) {
  const response = await apiRequest(request, "GET", `/api/inbox_ops/proposals/${proposalId}`, { token });
  if (!response.ok()) return null;
  const body = await readJsonSafe(response);
  return body?.proposal ?? null;
}
function findPendingAction(actions) {
  return actions.find((action) => action.status === "pending") ?? actions[0] ?? null;
}
export {
  deleteInboxEmail,
  fetchProposalDetail,
  findPendingAction,
  listInboxEmails,
  listInboxProposals,
  submitTextExtraction,
  waitForEmailProcessed
};
//# sourceMappingURL=inboxFixtures.js.map
