'use client';
// packages/ui/src/routing/hooks/useQueryParams.vite.ts
/**
 * @fileoverview Vite-specific useQueryParams hook
 * @description Query parameters hook with setter functionality for React Router
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
import { useCallback } from 'react';
import { useLocation } from './useLocation.vite';
import { useNavigate } from './useNavigate.vite';
import { useSearchParams } from './useSearchParams.vite';
/**
 * Platform-agnostic useQueryParams hook
 *
 * Provides easy access to query parameters with setter functionality
 *
 * @returns Object with query parameters and setter functions
 *
 * @example
 * ```tsx
 * const { query, setQuery, removeQuery } = useQueryParams();
 *
 * // Get a query parameter
 * const page = query.get('page');
 *
 * // Set a query parameter
 * setQuery('page', '2');
 *
 * // Remove a query parameter
 * removeQuery('page');
 * ```
 */
export function useQueryParams() {
    const searchParams = useSearchParams();
    const navigate = useNavigate();
    const location = useLocation();
    const setQuery = useCallback((key, value) => {
        const newParams = new URLSearchParams(searchParams.toString());
        newParams.set(key, value);
        const newSearch = newParams.toString();
        const pathname = location.pathname || '/';
        const newUrl = `${pathname}${newSearch ? `?${newSearch}` : ''}`;
        navigate(newUrl);
    }, [searchParams, navigate, location.pathname]);
    const removeQuery = useCallback((key) => {
        const newParams = new URLSearchParams(searchParams.toString());
        newParams.delete(key);
        const newSearch = newParams.toString();
        const pathname = location.pathname || '/';
        const newUrl = `${pathname}${newSearch ? `?${newSearch}` : ''}`;
        navigate(newUrl);
    }, [searchParams, navigate, location.pathname]);
    const clearQueries = useCallback(() => {
        const pathname = location.pathname || '/';
        navigate(pathname);
    }, [navigate, location.pathname]);
    return {
        query: searchParams,
        setQuery,
        removeQuery,
        clearQueries,
    };
}
