"use client";
import * as React from "react";
import { apiCallOrThrow, readApiResultOrThrow, 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";
function mergeUnique(existing, incoming) {
  if (!existing.length) return incoming;
  if (!incoming.length) return existing;
  const byId = /* @__PURE__ */ new Map();
  const result = [];
  for (const item of existing) {
    byId.set(item.id, item);
    result.push(item);
  }
  for (const item of incoming) {
    if (byId.has(item.id)) {
      const index = result.findIndex((entry) => entry.id === item.id);
      if (index !== -1) result[index] = item;
    } else {
      byId.set(item.id, item);
      result.push(item);
    }
  }
  return result;
}
function useInteractions({
  entityId,
  statusFilter,
  typeFilter,
  excludeTypeFilter,
  pageSize = 25
}) {
  const t = useT();
  const [interactions, setInteractions] = React.useState([]);
  const [nextCursor, setNextCursor] = React.useState(null);
  const [isInitialLoading, setIsInitialLoading] = React.useState(() => Boolean(entityId));
  const [isLoadingMore, setIsLoadingMore] = React.useState(false);
  const [isMutating, setIsMutating] = React.useState(false);
  const [pendingId, setPendingId] = React.useState(null);
  const [error, setError] = React.useState(null);
  const mapResponse = React.useCallback((payload) => {
    const mapped = Array.isArray(payload.items) ? payload.items : [];
    setNextCursor(typeof payload.nextCursor === "string" && payload.nextCursor.trim().length > 0 ? payload.nextCursor : null);
    setError(null);
    return mapped;
  }, []);
  const fetchPage = React.useCallback(
    async (cursor) => {
      if (!entityId) {
        return { items: [], nextCursor: void 0 };
      }
      const params = new URLSearchParams({
        limit: String(pageSize),
        entityId,
        sortField: "scheduledAt",
        sortDir: "asc"
      });
      if (cursor) params.set("cursor", cursor);
      if (statusFilter) params.set("status", statusFilter);
      if (typeFilter) params.set("interactionType", typeFilter);
      if (excludeTypeFilter) params.set("excludeInteractionType", excludeTypeFilter);
      return readApiResultOrThrow(
        `/api/customers/interactions?${params.toString()}`,
        void 0,
        { errorMessage: t("customers.interactions.load.error", "Failed to load interactions.") }
      );
    },
    [entityId, excludeTypeFilter, pageSize, statusFilter, typeFilter, t]
  );
  const refresh = React.useCallback(async () => {
    if (!entityId) {
      setInteractions([]);
      setNextCursor(null);
      return;
    }
    setIsInitialLoading(true);
    try {
      const payload = await fetchPage(null);
      const mapped = mapResponse(payload);
      setInteractions(mapped);
    } catch (err) {
      const message = err instanceof Error ? err.message : t("customers.interactions.load.error", "Failed to load interactions.");
      setError(message);
      throw err;
    } finally {
      setIsInitialLoading(false);
    }
  }, [entityId, fetchPage, mapResponse, t]);
  const loadMore = React.useCallback(async () => {
    if (!entityId) return;
    if (isLoadingMore) return;
    if (!nextCursor) return;
    setIsLoadingMore(true);
    try {
      const payload = await fetchPage(nextCursor);
      const mapped = mapResponse(payload);
      setInteractions((prev) => mergeUnique(prev, mapped));
    } catch (err) {
      const message = err instanceof Error ? err.message : t("customers.interactions.load.error", "Failed to load interactions.");
      setError(message);
      throw err;
    } finally {
      setIsLoadingMore(false);
    }
  }, [entityId, fetchPage, isLoadingMore, mapResponse, nextCursor, t]);
  React.useEffect(() => {
    if (!entityId) {
      setInteractions([]);
      setNextCursor(null);
      setError(null);
      setIsInitialLoading(false);
      return;
    }
    setInteractions([]);
    setNextCursor(null);
    setError(null);
    let cancelled = false;
    setIsInitialLoading(true);
    fetchPage(null).then((payload) => {
      if (cancelled) return;
      const mapped = mapResponse(payload);
      setInteractions(mapped);
    }).catch((err) => {
      if (cancelled) return;
      const message = err instanceof Error ? err.message : t("customers.interactions.load.error", "Failed to load interactions.");
      setError(message);
    }).finally(() => {
      if (!cancelled) setIsInitialLoading(false);
    });
    return () => {
      cancelled = true;
    };
  }, [entityId, excludeTypeFilter, statusFilter, typeFilter, fetchPage, mapResponse, t]);
  const createInteraction = React.useCallback(
    async (payload) => {
      if (!entityId) throw new Error("Interaction creation requires an entity id");
      setIsMutating(true);
      try {
        await apiCallOrThrow(
          "/api/customers/interactions",
          {
            method: "POST",
            headers: { "content-type": "application/json" },
            body: JSON.stringify(payload)
          },
          { errorMessage: t("customers.interactions.create.error", "Failed to create interaction.") }
        );
        await refresh();
      } finally {
        setIsMutating(false);
      }
    },
    [entityId, refresh, t]
  );
  const updateInteraction = React.useCallback(
    async (id, payload) => {
      setIsMutating(true);
      const target = interactions.find((entry) => entry.id === id);
      try {
        await withScopedApiRequestHeaders(
          buildOptimisticLockHeader(target?.updatedAt ?? null),
          () => apiCallOrThrow(
            "/api/customers/interactions",
            {
              method: "PUT",
              headers: { "content-type": "application/json" },
              body: JSON.stringify({ id, ...payload })
            },
            { errorMessage: t("customers.interactions.update.error", "Failed to update interaction.") }
          )
        );
        await refresh();
      } catch (err) {
        if (surfaceRecordConflict(err, t)) {
          await refresh();
          return;
        }
        throw err;
      } finally {
        setIsMutating(false);
      }
    },
    [interactions, refresh, t]
  );
  const completeInteraction = React.useCallback(
    async (id) => {
      setIsMutating(true);
      setPendingId(id);
      const target = interactions.find((entry) => entry.id === id);
      try {
        await withScopedApiRequestHeaders(
          buildOptimisticLockHeader(target?.updatedAt ?? null),
          () => apiCallOrThrow(
            "/api/customers/interactions/complete",
            {
              method: "POST",
              headers: { "content-type": "application/json" },
              body: JSON.stringify({ id })
            },
            { errorMessage: t("customers.interactions.complete.error", "Failed to complete interaction.") }
          )
        );
        await refresh();
      } catch (err) {
        if (surfaceRecordConflict(err, t)) {
          await refresh();
          return;
        }
        throw err;
      } finally {
        setPendingId(null);
        setIsMutating(false);
      }
    },
    [interactions, refresh, t]
  );
  const cancelInteraction = React.useCallback(
    async (id) => {
      setIsMutating(true);
      setPendingId(id);
      const target = interactions.find((entry) => entry.id === id);
      try {
        await withScopedApiRequestHeaders(
          buildOptimisticLockHeader(target?.updatedAt ?? null),
          () => apiCallOrThrow(
            "/api/customers/interactions/cancel",
            {
              method: "POST",
              headers: { "content-type": "application/json" },
              body: JSON.stringify({ id })
            },
            { errorMessage: t("customers.interactions.cancel.error", "Failed to cancel interaction.") }
          )
        );
        await refresh();
      } catch (err) {
        if (surfaceRecordConflict(err, t)) {
          await refresh();
          return;
        }
        throw err;
      } finally {
        setPendingId(null);
        setIsMutating(false);
      }
    },
    [interactions, refresh, t]
  );
  const deleteInteraction = React.useCallback(
    async (id) => {
      setIsMutating(true);
      const target = interactions.find((entry) => entry.id === id);
      try {
        await withScopedApiRequestHeaders(
          buildOptimisticLockHeader(target?.updatedAt ?? null),
          () => apiCallOrThrow(
            `/api/customers/interactions?id=${encodeURIComponent(id)}`,
            {
              method: "DELETE"
            },
            { errorMessage: t("customers.interactions.delete.error", "Failed to delete interaction.") }
          )
        );
        await refresh();
      } catch (err) {
        if (surfaceRecordConflict(err, t)) {
          await refresh();
          return;
        }
        throw err;
      } finally {
        setIsMutating(false);
      }
    },
    [interactions, refresh, t]
  );
  const hasMore = entityId != null && nextCursor != null;
  return {
    interactions,
    isInitialLoading,
    isLoadingMore,
    isMutating,
    hasMore,
    loadMore,
    refresh,
    createInteraction,
    updateInteraction,
    completeInteraction,
    cancelInteraction,
    deleteInteraction,
    pendingId,
    totalCount: interactions.length,
    error
  };
}
export {
  useInteractions
};
//# sourceMappingURL=useInteractions.js.map
