import * as React from "react";
import { useRouter } from "next/navigation";
import { flash } from "@open-mercato/ui/backend/FlashMessages";
import { surfaceRecordConflict } from "@open-mercato/ui/backend/conflicts";
import { deleteCrud, updateCrud } from "@open-mercato/ui/backend/utils/crud";
import { withScopedApiRequestHeaders } from "@open-mercato/ui/backend/utils/apiCall";
import { buildOptimisticLockHeader } from "@open-mercato/ui/backend/utils/optimisticLock";
import { useT } from "@open-mercato/shared/lib/i18n/context";
function useDealFormHandlers({
  data,
  currentDealId,
  loadData,
  runMutationWithContext,
  formWrapperRef,
  confirm
}) {
  const t = useT();
  const router = useRouter();
  const [isSaving, setIsSaving] = React.useState(false);
  const handleFormSubmit = React.useCallback(
    async (payload) => {
      if (!data) return;
      setIsSaving(true);
      try {
        const body = {
          id: data.deal.id,
          ...payload.base
        };
        if (payload.custom && Object.keys(payload.custom).length) {
          body.customFields = payload.custom;
        }
        await withScopedApiRequestHeaders(
          buildOptimisticLockHeader(data.deal.updatedAt),
          () => updateCrud("customers/deals", body)
        );
        flash(t("customers.deals.detail.updateSuccess", "Deal updated."), "success");
        await loadData();
      } finally {
        setIsSaving(false);
      }
    },
    [data, loadData, t]
  );
  const handleDelete = React.useCallback(async () => {
    if (!data || !currentDealId) return;
    const approved = await confirm({
      title: t("customers.deals.detail.deleteConfirmTitle", "Delete deal?"),
      description: t("customers.deals.detail.deleteConfirmDescription", "This action cannot be undone."),
      confirmText: t("customers.deals.detail.actions.delete", "Delete"),
      cancelText: t("customers.deals.detail.actions.cancel", "Cancel"),
      variant: "destructive"
    });
    if (!approved) return;
    try {
      await runMutationWithContext(
        () => withScopedApiRequestHeaders(
          buildOptimisticLockHeader(data.deal.updatedAt),
          () => deleteCrud("customers/deals", currentDealId)
        ),
        { id: currentDealId, operation: "deleteDeal" }
      );
    } catch (err) {
      if (!surfaceRecordConflict(err, t)) {
        flash(
          err instanceof Error && err.message.trim().length > 0 ? err.message : t("customers.deals.detail.deleteError", "Failed to delete deal."),
          "error"
        );
      }
      return;
    }
    flash(t("customers.deals.detail.deleteSuccess", "Deal deleted."), "success");
    router.push("/backend/customers/deals");
  }, [confirm, currentDealId, data, router, runMutationWithContext, t]);
  const handleHeaderSave = React.useCallback(() => {
    const form = formWrapperRef.current?.querySelector("form");
    if (form) form.requestSubmit();
  }, [formWrapperRef]);
  return {
    isSaving,
    handleFormSubmit,
    handleDelete,
    handleHeaderSave
  };
}
export {
  useDealFormHandlers
};
//# sourceMappingURL=useDealFormHandlers.js.map
