"use client";
import * as React from "react";
import { flash } from "@open-mercato/ui/backend/FlashMessages";
import { apiCallOrThrow, withScopedApiRequestHeaders } from "@open-mercato/ui/backend/utils/apiCall";
import { buildOptimisticLockHeader } from "@open-mercato/ui/backend/utils/optimisticLock";
import { surfaceRecordConflict } from "@open-mercato/ui/backend/conflicts";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("customers");
function useInteractionMutations({
  runMutationWithContext,
  onAfterChange,
  logContext = "customers.interactionMutations"
}) {
  const t = useT();
  const triggerRefresh = React.useCallback(async () => {
    if (!onAfterChange) return;
    try {
      await onAfterChange();
    } catch (err) {
      logger.warn("onAfterChange threw", { component: logContext, err });
    }
  }, [logContext, onAfterChange]);
  const completeInteraction = React.useCallback(
    async (interactionId, updatedAt) => {
      try {
        await runMutationWithContext(
          () => withScopedApiRequestHeaders(
            buildOptimisticLockHeader(updatedAt ?? null),
            () => apiCallOrThrow("/api/customers/interactions/complete", {
              method: "POST",
              headers: { "content-type": "application/json" },
              body: JSON.stringify({ id: interactionId, occurredAt: (/* @__PURE__ */ new Date()).toISOString() })
            })
          ),
          { id: interactionId, status: "done", operation: "completeActivity" }
        );
        flash(t("customers.timeline.planned.completed", "Activity completed"), "success");
        await triggerRefresh();
      } catch (err) {
        if (surfaceRecordConflict(err, t)) {
          await triggerRefresh();
          return;
        }
        logger.warn("Complete interaction failed", { component: logContext, interactionId, err });
        flash(t("customers.timeline.planned.error", "Failed to complete activity"), "error");
      }
    },
    [logContext, runMutationWithContext, t, triggerRefresh]
  );
  const cancelInteraction = React.useCallback(
    async (interactionId, updatedAt) => {
      try {
        await runMutationWithContext(
          () => withScopedApiRequestHeaders(
            buildOptimisticLockHeader(updatedAt ?? null),
            () => apiCallOrThrow("/api/customers/interactions", {
              method: "PUT",
              headers: { "content-type": "application/json" },
              body: JSON.stringify({ id: interactionId, status: "canceled" })
            })
          ),
          { id: interactionId, status: "canceled", operation: "cancelActivity" }
        );
        flash(t("customers.timeline.planned.canceled", "Activity canceled"), "success");
        await triggerRefresh();
      } catch (err) {
        if (surfaceRecordConflict(err, t)) {
          await triggerRefresh();
          return;
        }
        logger.warn("Cancel interaction failed", { component: logContext, interactionId, err });
        flash(t("customers.timeline.planned.cancelError", "Failed to cancel activity"), "error");
      }
    },
    [logContext, runMutationWithContext, t, triggerRefresh]
  );
  return { completeInteraction, cancelInteraction };
}
export {
  useInteractionMutations
};
//# sourceMappingURL=useInteractionMutations.js.map
