"use client";
import { apiCallOrThrow, readApiResultOrThrow, withScopedApiRequestHeaders } from "@open-mercato/ui/backend/utils/apiCall";
import { buildOptimisticLockHeader } from "@open-mercato/ui/backend/utils/optimisticLock";
import { mapCommentSummary } from "@open-mercato/ui/backend/detail/NotesSection";
function createResourceNotesAdapter(translator, options = {}) {
  const runWrite = async (runner, payload) => {
    if (options.runMutation) {
      return options.runMutation(runner, payload);
    }
    return runner();
  };
  return {
    list: async ({ entityId }) => {
      const params = new URLSearchParams();
      if (entityId) params.set("entityId", entityId);
      const payload = await readApiResultOrThrow(
        `/api/resources/comments?${params.toString()}`,
        void 0,
        { errorMessage: translator("resources.resources.detail.notes.loadError", "Failed to load notes.") }
      );
      const items = Array.isArray(payload?.items) ? payload.items : [];
      return items.map(mapCommentSummary);
    },
    create: async ({ entityId, body, appearanceIcon, appearanceColor }) => {
      const requestBody = {
        entityId,
        body,
        appearanceIcon: appearanceIcon ?? void 0,
        appearanceColor: appearanceColor ?? void 0
      };
      const response = await runWrite(
        () => apiCallOrThrow(
          "/api/resources/comments",
          {
            method: "POST",
            headers: { "content-type": "application/json" },
            body: JSON.stringify(requestBody)
          },
          { errorMessage: translator("resources.resources.detail.notes.error", "Failed to save note.") }
        ),
        { operation: "createNote", entityId }
      );
      return response.result ?? {};
    },
    update: async ({ id, patch, updatedAt }) => {
      const payload = { id };
      if (patch.body !== void 0) payload.body = patch.body;
      if (patch.appearanceIcon !== void 0) payload.appearanceIcon = patch.appearanceIcon;
      if (patch.appearanceColor !== void 0) payload.appearanceColor = patch.appearanceColor;
      await runWrite(
        () => withScopedApiRequestHeaders(
          buildOptimisticLockHeader(updatedAt ?? null),
          () => apiCallOrThrow(
            "/api/resources/comments",
            {
              method: "PUT",
              headers: { "content-type": "application/json" },
              body: JSON.stringify(payload)
            },
            { errorMessage: translator("resources.resources.detail.notes.updateError", "Failed to update note.") }
          )
        ),
        { operation: "updateNote", id }
      );
    },
    delete: async ({ id, updatedAt }) => {
      await runWrite(
        () => withScopedApiRequestHeaders(
          buildOptimisticLockHeader(updatedAt ?? null),
          () => apiCallOrThrow(
            `/api/resources/comments?id=${encodeURIComponent(id)}`,
            {
              method: "DELETE",
              headers: { "content-type": "application/json" }
            },
            { errorMessage: translator("resources.resources.detail.notes.deleteError", "Failed to delete note") }
          )
        ),
        { operation: "deleteNote", id }
      );
    }
  };
}
export {
  createResourceNotesAdapter
};
//# sourceMappingURL=notesAdapter.js.map
