import { expect } from "@playwright/test";
import { apiRequest } from "./api.js";
import { expectId, readJsonSafe } from "./generalFixtures.js";
const BASE_URL = process.env.BASE_URL?.trim() || "http://localhost:3000";
function resolveApiUrl(path) {
  return `${BASE_URL}${path}`;
}
async function uploadAttachmentFixture(request, token, input) {
  const multipart = {
    entityId: input.entityId,
    recordId: input.recordId,
    file: {
      name: input.fileName,
      mimeType: input.mimeType,
      buffer: input.buffer
    }
  };
  if (input.partitionCode) multipart.partitionCode = input.partitionCode;
  if (input.tags) multipart.tags = JSON.stringify(input.tags);
  if (input.assignments) multipart.assignments = JSON.stringify(input.assignments);
  const response = await request.fetch(resolveApiUrl("/api/attachments"), {
    method: "POST",
    headers: {
      Authorization: `Bearer ${token}`
    },
    multipart
  });
  const body = await readJsonSafe(response);
  expect(response.status(), "POST /api/attachments should return 200").toBe(200);
  return {
    id: expectId(body?.item?.id, "Attachment upload response should include item.id"),
    partitionCode: String(body?.item?.partitionCode ?? ""),
    fileName: String(body?.item?.fileName ?? ""),
    tags: body?.item?.tags ?? [],
    assignments: body?.item?.assignments ?? []
  };
}
async function deleteAttachmentIfExists(request, token, attachmentId) {
  if (!token || !attachmentId) return;
  await apiRequest(
    request,
    "DELETE",
    `/api/attachments?id=${encodeURIComponent(attachmentId)}`,
    { token }
  ).catch(() => void 0);
}
async function deleteAttachmentPartitionIfExists(request, token, partitionId) {
  if (!token || !partitionId) return;
  await apiRequest(
    request,
    "DELETE",
    `/api/attachments/partitions?id=${encodeURIComponent(partitionId)}`,
    { token }
  ).catch(() => void 0);
}
export {
  deleteAttachmentIfExists,
  deleteAttachmentPartitionIfExists,
  uploadAttachmentFixture
};
//# sourceMappingURL=attachmentsFixtures.js.map
