import { usePage } from '@inertiajs/react';
import { useMemo } from 'react';

type MenuItem = {
    title: string;
    url?: string;
    items?: { title: string; url: string }[];
};

/**
 * Hook for managing active menu states based on current URL
 */
export function useActiveMenu() {
    const { url: currentUrl } = usePage();

    return useMemo(
        () => ({
            /**
             * Check if a menu item should be active
             */
            isMenuItemActive: (item: MenuItem): boolean => {
                if (item.url) {
                    return currentUrl === item.url || currentUrl.startsWith(item.url);
                }

                // Check if any sub-item is active
                if (item.items) {
                    return item.items.some((subItem) => currentUrl === subItem.url || currentUrl.startsWith(subItem.url));
                }

                return false;
            },

            /**
             * Check if a sub-item should be active
             */
            isSubItemActive: (url: string): boolean => {
                return currentUrl === url || currentUrl.startsWith(url);
            },

            /**
             * Check if current URL matches exactly
             */
            isExactMatch: (url: string): boolean => {
                return currentUrl === url;
            },

            /**
             * Check if current URL starts with given path
             */
            isPathMatch: (path: string): boolean => {
                return currentUrl.startsWith(path);
            },

            /**
             * Get the current URL
             */
            currentUrl,

            /**
             * Mark menu items with active state
             */
            markActiveItems: <T extends MenuItem>(items: T[]): T[] => {
                return items.map((item) => ({
                    ...item,
                    isActive: item.url
                        ? currentUrl === item.url || currentUrl.startsWith(item.url)
                        : (item.items?.some((subItem) => currentUrl === subItem.url || currentUrl.startsWith(subItem.url)) ?? false),
                    items: item.items?.map((subItem) => ({
                        ...subItem,
                        isActive: currentUrl === subItem.url || currentUrl.startsWith(subItem.url),
                    })),
                }));
            },
        }),
        [currentUrl],
    );
}

/**
 * Helper function to create navigation items with automatic active state
 */
export function createNavItems<T extends MenuItem>(items: T[], currentUrl: string): (T & { isActive: boolean })[] {
    return items.map((item) => ({
        ...item,
        isActive: item.url
            ? currentUrl === item.url || currentUrl.startsWith(item.url)
            : (item.items?.some((subItem) => currentUrl === subItem.url || currentUrl.startsWith(subItem.url)) ?? false),
        items: item.items?.map((subItem) => ({
            ...subItem,
            isActive: currentUrl === subItem.url || currentUrl.startsWith(subItem.url),
        })),
    }));
}
