"use client";
'use strict';

const client = require('@btst/stack/plugins/client');
const reactQuery = require('@tanstack/react-query');
const plugins_aiChat_queryKeys = require('../../../../../../../plugins/ai-chat/query-keys.cjs');
const context = require('@btst/stack/context');

const SHARED_QUERY_CONFIG = {
  retry: false,
  refetchOnWindowFocus: false,
  refetchOnMount: false,
  refetchOnReconnect: false,
  staleTime: 1e3 * 60 * 5,
  // 5 minutes
  gcTime: 1e3 * 60 * 10
  // 10 minutes
};
function useConversations(options = {}) {
  const { apiBaseURL, apiBasePath, headers } = context.usePluginOverrides("ai-chat");
  const client$1 = client.createApiClient({
    baseURL: apiBaseURL,
    basePath: apiBasePath
  });
  const { enabled = true } = options;
  const queries = plugins_aiChat_queryKeys.createAiChatQueryKeys(client$1, headers);
  const listQuery = queries.conversations.list();
  const { data, isLoading, error, refetch } = reactQuery.useQuery({
    ...listQuery,
    ...SHARED_QUERY_CONFIG,
    enabled: enabled && !!client$1
  });
  return {
    conversations: data ?? [],
    isLoading,
    error,
    refetch
  };
}
function useSuspenseConversations() {
  const { apiBaseURL, apiBasePath, headers } = context.usePluginOverrides("ai-chat");
  const client$1 = client.createApiClient({
    baseURL: apiBaseURL,
    basePath: apiBasePath
  });
  const queries = plugins_aiChat_queryKeys.createAiChatQueryKeys(client$1, headers);
  const listQuery = queries.conversations.list();
  const { data, refetch, error, isFetching } = reactQuery.useSuspenseQuery({
    ...listQuery,
    ...SHARED_QUERY_CONFIG
  });
  if (error && !isFetching) {
    throw error;
  }
  return {
    conversations: data ?? [],
    refetch
  };
}
function useConversation(id, options = {}) {
  const { apiBaseURL, apiBasePath, headers } = context.usePluginOverrides("ai-chat");
  const client$1 = client.createApiClient({
    baseURL: apiBaseURL,
    basePath: apiBasePath
  });
  const { enabled = true } = options;
  const queries = plugins_aiChat_queryKeys.createAiChatQueryKeys(client$1, headers);
  const detailQuery = queries.conversations.detail(id ?? "");
  const { data, isLoading, error, refetch } = reactQuery.useQuery({
    ...detailQuery,
    ...SHARED_QUERY_CONFIG,
    enabled: enabled && !!client$1 && !!id
  });
  return {
    conversation: data || null,
    isLoading,
    error,
    refetch
  };
}
function useSuspenseConversation(id) {
  const { apiBaseURL, apiBasePath, headers } = context.usePluginOverrides("ai-chat");
  const client$1 = client.createApiClient({
    baseURL: apiBaseURL,
    basePath: apiBasePath
  });
  const queries = plugins_aiChat_queryKeys.createAiChatQueryKeys(client$1, headers);
  const detailQuery = queries.conversations.detail(id);
  const { data, refetch, error, isFetching } = reactQuery.useSuspenseQuery({
    ...detailQuery,
    ...SHARED_QUERY_CONFIG
  });
  if (error && !isFetching) {
    throw error;
  }
  return {
    conversation: data || null,
    refetch
  };
}
function useCreateConversation() {
  const { refresh, apiBaseURL, apiBasePath } = context.usePluginOverrides("ai-chat");
  const client$1 = client.createApiClient({
    baseURL: apiBaseURL,
    basePath: apiBasePath
  });
  const queryClient = reactQuery.useQueryClient();
  const queries = plugins_aiChat_queryKeys.createAiChatQueryKeys(client$1);
  return reactQuery.useMutation({
    mutationKey: [...queries.conversations._def, "create"],
    mutationFn: async (data) => {
      const response = await client$1("@post/chat/conversations", {
        method: "POST",
        body: data
      });
      return response.data;
    },
    onSuccess: async (created) => {
      await queryClient.invalidateQueries({
        queryKey: queries.conversations.list().queryKey
      });
      if (refresh) {
        await refresh();
      }
    }
  });
}
function useRenameConversation() {
  const { refresh, apiBaseURL, apiBasePath } = context.usePluginOverrides("ai-chat");
  const client$1 = client.createApiClient({
    baseURL: apiBaseURL,
    basePath: apiBasePath
  });
  const queryClient = reactQuery.useQueryClient();
  const queries = plugins_aiChat_queryKeys.createAiChatQueryKeys(client$1);
  return reactQuery.useMutation({
    mutationKey: [...queries.conversations._def, "rename"],
    mutationFn: async ({ id, title }) => {
      const response = await client$1("@put/chat/conversations/:id", {
        method: "PUT",
        params: { id },
        body: { title }
      });
      return response.data;
    },
    onSuccess: async (updated) => {
      if (updated?.id) {
        queryClient.setQueryData(
          queries.conversations.detail(updated.id).queryKey,
          (old) => old ? { ...old, ...updated } : null
        );
      }
      await queryClient.invalidateQueries({
        queryKey: queries.conversations.list().queryKey
      });
      if (refresh) {
        await refresh();
      }
    }
  });
}
function useDeleteConversation() {
  const { refresh, apiBaseURL, apiBasePath } = context.usePluginOverrides("ai-chat");
  const client$1 = client.createApiClient({
    baseURL: apiBaseURL,
    basePath: apiBasePath
  });
  const queryClient = reactQuery.useQueryClient();
  const queries = plugins_aiChat_queryKeys.createAiChatQueryKeys(client$1);
  return reactQuery.useMutation({
    mutationKey: [...queries.conversations._def, "delete"],
    mutationFn: async ({ id }) => {
      const response = await client$1("@delete/chat/conversations/:id", {
        method: "DELETE",
        params: { id }
      });
      return response.data;
    },
    onSuccess: async (_, { id }) => {
      queryClient.removeQueries({
        queryKey: queries.conversations.detail(id).queryKey
      });
      await queryClient.invalidateQueries({
        queryKey: queries.conversations.list().queryKey
      });
      if (refresh) {
        await refresh();
      }
    }
  });
}

exports.useConversation = useConversation;
exports.useConversations = useConversations;
exports.useCreateConversation = useCreateConversation;
exports.useDeleteConversation = useDeleteConversation;
exports.useRenameConversation = useRenameConversation;
exports.useSuspenseConversation = useSuspenseConversation;
exports.useSuspenseConversations = useSuspenseConversations;
