"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 createStaffNotesAdapter(translator) {
  return {
    list: async ({ entityId }) => {
      const params = new URLSearchParams();
      if (entityId) params.set("entityId", entityId);
      const payload = await readApiResultOrThrow(
        `/api/staff/comments?${params.toString()}`,
        void 0,
        { errorMessage: translator("staff.teamMembers.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 response = await apiCallOrThrow(
        "/api/staff/comments",
        {
          method: "POST",
          headers: { "content-type": "application/json" },
          body: JSON.stringify({
            entityId,
            body,
            appearanceIcon: appearanceIcon ?? void 0,
            appearanceColor: appearanceColor ?? void 0
          })
        },
        { errorMessage: translator("staff.teamMembers.detail.notes.error", "Failed to save note.") }
      );
      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 withScopedApiRequestHeaders(
        buildOptimisticLockHeader(updatedAt ?? null),
        () => apiCallOrThrow(
          "/api/staff/comments",
          {
            method: "PUT",
            headers: { "content-type": "application/json" },
            body: JSON.stringify(payload)
          },
          { errorMessage: translator("staff.teamMembers.detail.notes.updateError", "Failed to update note.") }
        )
      );
    },
    delete: async ({ id, updatedAt }) => {
      await withScopedApiRequestHeaders(
        buildOptimisticLockHeader(updatedAt ?? null),
        () => apiCallOrThrow(
          `/api/staff/comments?id=${encodeURIComponent(id)}`,
          {
            method: "DELETE",
            headers: { "content-type": "application/json" }
          },
          { errorMessage: translator("staff.teamMembers.detail.notes.deleteError", "Failed to delete note") }
        )
      );
    }
  };
}
export {
  createStaffNotesAdapter
};
//# sourceMappingURL=notesAdapter.js.map
