"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 createCustomerNotesAdapter(translator, options = {}) {
  const runWrite = async (runner, payload) => {
    if (options.runMutation) {
      return options.runMutation(runner, payload);
    }
    return runner();
  };
  return {
    list: async ({ entityId, dealId }) => {
      const params = new URLSearchParams();
      if (entityId) params.set("entityId", entityId);
      if (dealId) params.set("dealId", dealId);
      const payload = await readApiResultOrThrow(
        `/api/customers/comments?${params.toString()}`,
        void 0,
        { errorMessage: translator("customers.people.detail.notes.loadError", "Failed to load notes.") }
      );
      const items = Array.isArray(payload?.items) ? payload.items : [];
      return items.map(mapCommentSummary);
    },
    listPage: async ({ entityId, dealId, page, pageSize }) => {
      const params = new URLSearchParams();
      if (entityId) params.set("entityId", entityId);
      if (dealId) params.set("dealId", dealId);
      params.set("page", String(page));
      params.set("pageSize", String(pageSize));
      const payload = await readApiResultOrThrow(
        `/api/customers/comments?${params.toString()}`,
        void 0,
        { errorMessage: translator("customers.people.detail.notes.loadError", "Failed to load notes.") }
      );
      const items = Array.isArray(payload?.items) ? payload.items.map(mapCommentSummary) : [];
      const total = typeof payload?.total === "number" ? payload.total : items.length;
      const totalPages = typeof payload?.totalPages === "number" ? payload.totalPages : Math.max(1, Math.ceil(total / pageSize));
      const resolvedPage = typeof payload?.page === "number" ? payload.page : page;
      const resolvedPageSize = typeof payload?.pageSize === "number" ? payload.pageSize : pageSize;
      return {
        items,
        total,
        page: resolvedPage,
        pageSize: resolvedPageSize,
        totalPages
      };
    },
    create: async ({ entityId, body, appearanceIcon, appearanceColor, dealId }) => {
      const requestBody = {
        entityId,
        body,
        appearanceIcon: appearanceIcon ?? void 0,
        appearanceColor: appearanceColor ?? void 0,
        dealId: dealId ?? void 0
      };
      const response = await runWrite(
        () => apiCallOrThrow(
          "/api/customers/comments",
          {
            method: "POST",
            headers: { "content-type": "application/json" },
            body: JSON.stringify(requestBody)
          },
          { errorMessage: translator("customers.people.detail.notes.error") }
        ),
        { operation: "createNote", entityId, dealId: dealId ?? null }
      );
      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/customers/comments",
            {
              method: "PUT",
              headers: { "content-type": "application/json" },
              body: JSON.stringify(payload)
            },
            { errorMessage: translator("customers.people.detail.notes.updateError") }
          )
        ),
        { operation: "updateNote", id }
      );
    },
    delete: async ({ id, updatedAt }) => {
      await runWrite(
        () => withScopedApiRequestHeaders(
          buildOptimisticLockHeader(updatedAt ?? null),
          () => apiCallOrThrow(
            `/api/customers/comments?id=${encodeURIComponent(id)}`,
            {
              method: "DELETE",
              headers: { "content-type": "application/json" }
            },
            { errorMessage: translator("customers.people.detail.notes.deleteError", "Failed to delete note") }
          )
        ),
        { operation: "deleteNote", id }
      );
    }
  };
}
export {
  createCustomerNotesAdapter
};
//# sourceMappingURL=notesAdapter.js.map
