"use client";
import { readApiResultOrThrow } from "@open-mercato/ui/backend/utils/apiCall";
import { createCrud, updateCrud, deleteCrud } from "@open-mercato/ui/backend/utils/crud";
function createResourceActivitiesAdapter(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({
        pageSize: "50",
        sortField: "occurredAt",
        sortDir: "desc"
      });
      if (entityId) params.set("entityId", entityId);
      const payload = await readApiResultOrThrow(
        `/api/resources/activities?${params.toString()}`,
        void 0,
        { errorMessage: translator("resources.resources.detail.activities.loadError", "Failed to load activities.") }
      );
      return Array.isArray(payload?.items) ? payload.items : [];
    },
    create: async ({ entityId, activityType, subject, body, occurredAt, customFields }) => {
      const payload = {
        entityId,
        activityType,
        subject: subject ?? void 0,
        body: body ?? void 0,
        occurredAt: occurredAt ?? void 0,
        ...customFields ? { customFields } : {}
      };
      await runWrite(
        () => createCrud("resources/activities", payload, {
          errorMessage: translator("resources.resources.detail.activities.error", "Failed to save activity")
        }),
        { operation: "createActivity", entityId, activityType }
      );
    },
    update: async ({ id, patch }) => {
      const payload = {
        id,
        entityId: patch.entityId,
        activityType: patch.activityType,
        subject: patch.subject ?? void 0,
        body: patch.body ?? void 0,
        occurredAt: patch.occurredAt ?? void 0,
        ...patch.customFields ? { customFields: patch.customFields } : {}
      };
      await runWrite(
        () => updateCrud("resources/activities", payload, {
          errorMessage: translator("resources.resources.detail.activities.error", "Failed to save activity")
        }),
        { operation: "updateActivity", id }
      );
    },
    delete: async ({ id }) => {
      await runWrite(
        () => deleteCrud("resources/activities", {
          id,
          errorMessage: translator("resources.resources.detail.activities.deleteError", "Failed to delete activity.")
        }),
        { operation: "deleteActivity", id }
      );
    }
  };
}
export {
  createResourceActivitiesAdapter
};
//# sourceMappingURL=activitiesAdapter.js.map
