/**
 * React Query hooks for Widgets
 * @since 1.1.0
 */

import { useQuery } from '@tanstack/react-query'
import { widgetsApi } from '@/api/widgets'
import { useLanguage } from '@/contexts/LanguageContext'

/**
 * Get all widgets (filtered by current admin language)
 */
export function useWidgets(page = 1, perPage = 50, type?: string) {
    const { currentLanguage } = useLanguage()
    return useQuery({
        queryKey: ['widgets', page, perPage, type, currentLanguage],
        queryFn: () => widgetsApi.getWidgets(page, perPage, type, currentLanguage),
    })
}

/**
 * Get a single widget
 */
export function useWidget(id: number, options: { enabled?: boolean } = {}) {
    return useQuery({
        queryKey: ['widget', id],
        queryFn: () => widgetsApi.getWidget(id),
        enabled: options.enabled !== false && id > 0,
    })
}

/**
 * Get widgets for a chatbot
 */
export function useWidgetsForChatbot(chatbotId: number, options: { enabled?: boolean } = {}) {
    return useQuery({
        queryKey: ['widgets', 'for-chatbot', chatbotId],
        queryFn: () => widgetsApi.getWidgetsForChatbot(chatbotId),
        enabled: options.enabled !== false && !!chatbotId && chatbotId > 0,
    })
}
