"use client";
import * as React from "react";
import { useQuery } from "@tanstack/react-query";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
import { toErrorMessage } from "../utils.js";
function useMessageDetailsQueries({
  id,
  t,
  scopeVersion,
  queryClient
}) {
  const detailQueryKey = React.useMemo(
    () => ["messages", "detail", id, scopeVersion],
    [id, scopeVersion]
  );
  const suppressAutoMarkReadRef = React.useRef(false);
  React.useEffect(() => {
    suppressAutoMarkReadRef.current = false;
  }, [id]);
  const detailQuery = useQuery({
    queryKey: detailQueryKey,
    queryFn: async () => {
      const detailUrl = suppressAutoMarkReadRef.current ? `/api/messages/${encodeURIComponent(id)}?skipMarkRead=1` : `/api/messages/${encodeURIComponent(id)}`;
      const call = await apiCall(detailUrl);
      if (call.status === 404) {
        return null;
      }
      if (!call.ok || !call.result) {
        throw new Error(
          toErrorMessage(call.result) ?? t("messages.errors.loadDetailFailed", "Failed to load message details.")
        );
      }
      return call.result;
    }
  });
  const detail = detailQuery.data ?? null;
  const isLoadingDetail = detailQuery.isLoading;
  const isDetailMissing = detailQuery.isSuccess && detailQuery.data === null;
  const loadErrorMessage = isDetailMissing ? t("messages.errors.messageDeleted", "This message is no longer available.") : detailQuery.error instanceof Error ? detailQuery.error.message : t("messages.errors.loadDetailFailed", "Failed to load message details.");
  const attachmentsQuery = useQuery({
    queryKey: ["messages", "detail", id, "attachments", scopeVersion],
    queryFn: async () => {
      const call = await apiCall(
        `/api/messages/${encodeURIComponent(id)}/attachments`
      );
      if (call.status === 404) {
        return [];
      }
      if (!call.ok) {
        throw new Error(
          toErrorMessage(call.result) ?? t("messages.errors.loadDetailFailed", "Failed to load message details.")
        );
      }
      return call.result?.attachments ?? [];
    }
  });
  const attachments = attachmentsQuery.data;
  const refreshDetailWithoutAutoMarkRead = React.useCallback(async () => {
    const call = await apiCall(
      `/api/messages/${encodeURIComponent(id)}?skipMarkRead=1`
    );
    if (call.status === 404) {
      queryClient.setQueryData(detailQueryKey, null);
      return null;
    }
    if (!call.ok || !call.result) {
      throw new Error(
        toErrorMessage(call.result) ?? t("messages.errors.stateChangeFailed", "Failed to update message state.")
      );
    }
    queryClient.setQueryData(detailQueryKey, call.result);
    return call.result;
  }, [detailQueryKey, id, queryClient, t]);
  const suppressAutoMarkRead = React.useCallback(() => {
    suppressAutoMarkReadRef.current = true;
  }, []);
  const listItemComponentKey = detail?.typeDefinition.ui?.listItemComponent ?? null;
  const contentComponentKey = detail?.typeDefinition.ui?.contentComponent ?? null;
  const actionsComponentKey = detail?.typeDefinition.ui?.actionsComponent ?? null;
  return {
    detailQueryKey,
    detailQuery,
    detail,
    isLoadingDetail,
    isDetailMissing,
    loadErrorMessage,
    attachmentsQuery,
    attachments,
    refreshDetailWithoutAutoMarkRead,
    suppressAutoMarkRead,
    listItemComponentKey,
    contentComponentKey,
    actionsComponentKey
  };
}
export {
  useMessageDetailsQueries
};
//# sourceMappingURL=useMessageDetailsQueries.js.map
