"use client";
import { __rest } from '../../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.mjs';
import { jsxs, jsx, Fragment as Fragment$1 } from 'react/jsx-runtime';
import { useMemo, useState, Fragment } from 'react';
import { cva } from '../../node_modules/.pnpm/class-variance-authority@0.7.1/node_modules/class-variance-authority/dist/index.mjs';
import Link from 'fumadocs-core/link';
import { globalLucideIcons } from '@windrun-huaiin/base-ui/components/server';
import { getLinks, BaseLinkItem } from 'fumadocs-ui/layouts/shared';
import { cn } from 'fumadocs-ui/utils/cn';
import { LargeSearchToggle, SearchToggle } from 'fumadocs-ui/components/layout/search-toggle';
import { ThemeToggle } from 'fumadocs-ui/components/layout/theme-toggle';
import { LanguageToggleText } from 'fumadocs-ui/components/layout/language-toggle';
import { NavigationMenuItem, NavigationMenu, NavigationMenuList, NavigationMenuViewport, NavigationMenuLink, NavigationMenuTrigger, NavigationMenuContent } from 'fumadocs-ui/components/ui/navigation-menu';
import { Popover, PopoverTrigger, PopoverContent } from 'fumadocs-ui/components/ui/popover';
import { buttonVariants } from 'fumadocs-ui/components/ui/button';
import { useNav } from 'fumadocs-ui/contexts/layout';
import { useI18n } from 'fumadocs-ui/contexts/i18n';

function CustomHomeHeader({ nav = {}, i18n = false, links, githubUrl, themeSwitch = {}, searchToggle = {}, bannerHeight = 0, headerHeight = 2.5, maxContentWidth = 1400, navbarClassName, floating = false, }) {
    var _a, _b, _c, _d, _e, _f, _g;
    const finalLinks = useMemo(() => getLinks(links, githubUrl), [links, githubUrl]);
    const navItems = finalLinks.filter((item) => { var _a; return ['nav', 'all'].includes((_a = item.on) !== null && _a !== void 0 ? _a : 'all'); });
    const menuItems = finalLinks.filter((item) => { var _a; return ['menu', 'all'].includes((_a = item.on) !== null && _a !== void 0 ? _a : 'all'); });
    const mobilePinnedItems = navItems.filter((item) => isSecondary(item) && isMobilePinned(item));
    const filteredMenuItems = menuItems.filter((item) => !isMobilePinned(item));
    return (jsxs(CustomNavbar, { bannerHeight: bannerHeight, headerHeight: headerHeight, maxContentWidth: maxContentWidth, className: navbarClassName, floating: floating, children: [jsx(Link, { href: (_a = nav.url) !== null && _a !== void 0 ? _a : '/', className: "inline-flex items-center gap-2.5 font-semibold", children: nav.title }), nav.children, jsx("ul", { className: "flex flex-row items-center gap-2 px-6 max-sm:hidden", children: navItems
                    .filter((item) => !isSecondary(item))
                    .map((item, i) => (jsx(NavbarLinkItem, { item: item, className: "text-sm" }, i))) }), jsxs("div", { className: "flex flex-row items-center justify-end gap-1.5 flex-1 max-lg:hidden", children: [searchToggle.enabled !== false &&
                        ((_c = (_b = searchToggle.components) === null || _b === void 0 ? void 0 : _b.lg) !== null && _c !== void 0 ? _c : (jsx(LargeSearchToggle, { className: "w-full rounded-full ps-2.5 max-w-[240px]", hideIfDisabled: true }))), themeSwitch.enabled !== false &&
                        ((_d = themeSwitch.component) !== null && _d !== void 0 ? _d : jsx(ThemeToggle, { mode: themeSwitch === null || themeSwitch === void 0 ? void 0 : themeSwitch.mode })), i18n && (jsx(CompactLanguageToggle, { children: jsx(globalLucideIcons.Languages, { className: "size-5" }) })), jsx("ul", { className: "flex flex-row gap-2 items-center empty:hidden", children: navItems.filter(isSecondary).map((item, i) => (jsx(NavbarLinkItem, { item: item, className: cn(item.type === 'icon' && [
                                '-mx-1',
                                i === 0 && 'ms-0',
                                i === navItems.length - 1 && 'me-0',
                            ]) }, i))) })] }), jsxs("ul", { className: "flex flex-row items-center ms-auto -me-1.5 lg:hidden", children: [mobilePinnedItems.map((item, i) => (jsx(NavbarLinkItem, { item: item, className: "max-sm:-mr-1" }, `mobile-pinned-${i}`))), searchToggle.enabled !== false &&
                        ((_f = (_e = searchToggle.components) === null || _e === void 0 ? void 0 : _e.sm) !== null && _f !== void 0 ? _f : (jsx(SearchToggle, { className: "p-2", hideIfDisabled: true }))), jsxs(Menu, { children: [jsx(MenuTrigger, { "aria-label": "Toggle Menu", className: cn(buttonVariants({
                                    size: 'icon',
                                    color: 'ghost',
                                    className: 'group [&_svg]:size-5.5',
                                })), enableHover: nav.enableHoverToOpen, children: jsx(globalLucideIcons.ChevronDown, { className: "transition-transform duration-300 group-data-[state=open]:rotate-180" }) }), jsxs(MenuContent, { className: "sm:flex-row sm:items-center sm:justify-end", children: [filteredMenuItems
                                        .filter((item) => !isSecondary(item))
                                        .map((item, i) => (jsx(MenuLinkItem, { item: item, className: "sm:hidden" }, i))), jsxs("div", { className: "-ms-1.5 flex flex-row items-center gap-1.5 max-sm:mt-2", children: [filteredMenuItems.filter(isSecondary).map((item, i) => (jsx(MenuLinkItem, { item: item, className: "-me-1.5" }, i))), jsx("div", { role: "separator", className: "flex-1" }), i18n ? (jsxs(CompactLanguageToggle, { children: [jsx(globalLucideIcons.Languages, { className: "size-5" }), jsx(LanguageToggleText, {}), jsx(globalLucideIcons.ChevronDown, { className: "size-3 text-fd-muted-foreground" })] })) : null, themeSwitch.enabled !== false &&
                                                ((_g = themeSwitch.component) !== null && _g !== void 0 ? _g : (jsx(ThemeToggle, { mode: themeSwitch === null || themeSwitch === void 0 ? void 0 : themeSwitch.mode })))] })] })] })] })] }));
}
function CustomNavbar(_a) {
    var { bannerHeight = 0, headerHeight = 2.5, maxContentWidth = 1400, className, style, floating = false } = _a, props = __rest(_a, ["bannerHeight", "headerHeight", "maxContentWidth", "className", "style", "floating"]);
    const [value, setValue] = useState('');
    const { isTransparent } = useNav();
    const cssVars = Object.assign({ '--fd-banner-height': `${bannerHeight}rem`, '--fd-nav-height': `${headerHeight}rem` }, (maxContentWidth
        ? {
            '--fd-nav-max-width': typeof maxContentWidth === 'number'
                ? `${maxContentWidth}px`
                : maxContentWidth,
        }
        : {}));
    const resolvedMaxWidth = maxContentWidth && maxContentWidth !== 0
        ? typeof maxContentWidth === 'number'
            ? `${maxContentWidth}px`
            : maxContentWidth
        : '88rem';
    const minNavWidth = '15rem';
    const floatingWidth = `clamp(${minNavWidth}, 100vw, ${resolvedMaxWidth})`;
    const widthStyle = floating
        ? { width: floatingWidth }
        : { width: '100%', maxWidth: resolvedMaxWidth, minWidth: minNavWidth };
    const headerStyle = Object.assign(Object.assign(Object.assign({}, cssVars), widthStyle), style);
    return (jsx(NavigationMenu, { value: value, onValueChange: setValue, asChild: true, children: jsxs("header", Object.assign({}, props, { style: headerStyle, className: cn('rounded-2xl border px-4 py-1 transition-[background-color,box-shadow,transform] duration-300 backdrop-blur-xl shadow-lg shadow-black/5', floating
                ? 'fixed left-1/2 top-[--fd-banner-height] z-1001 -translate-x-1/2'
                : 'relative mx-auto w-full', isTransparent
                ? 'border-transparent bg-transparent shadow-transparent'
                : 'border border-fd-border/60 bg-white/85 dark:border-white/20 dark:bg-neutral-900/75', value.length > 0 &&
                'max-lg:rounded-b-3xl border-fd-border/60 bg-white dark:border-white/20 dark:bg-neutral-900', className), children: [jsx(NavigationMenuList, { className: "flex w-full items-center gap-4 px-1", style: { height: 'var(--fd-nav-height)' }, asChild: true, children: jsx("nav", { children: props.children }) }), jsx(NavigationMenuViewport, {})] })) }));
}
const navItemVariants = cva('[&_svg]:size-4', {
    variants: {
        variant: {
            main: 'inline-flex items-center gap-1 p-2 text-fd-muted-foreground transition-colors hover:text-fd-accent-foreground data-[active=true]:text-fd-primary',
            button: buttonVariants({
                color: 'secondary',
                className: 'gap-1.5',
            }),
            icon: buttonVariants({
                color: 'ghost',
                size: 'icon',
            }),
        },
    },
    defaultVariants: {
        variant: 'main',
    },
});
function NavbarLinkItem(_a) {
    var { item } = _a, props = __rest(_a, ["item"]);
    if (item.type === 'custom')
        return jsx("div", Object.assign({}, props, { children: item.children }));
    if (item.type === 'menu') {
        const children = item.items.map((child, j) => {
            var _a, _b;
            if (child.type === 'custom') {
                return jsx(Fragment, { children: child.children }, j);
            }
            const _c = (_a = child.menu) !== null && _a !== void 0 ? _a : {}, { banner = child.icon ? (jsx("div", { className: "w-fit rounded-md border bg-fd-muted p-1 [&_svg]:size-4", children: child.icon })) : null } = _c, rest = __rest(_c, ["banner"]);
            return (jsx(NavigationMenuLink, { asChild: true, children: jsx(Link, Object.assign({ href: child.url, external: child.external }, rest, { className: cn('flex flex-col gap-2 rounded-lg border bg-fd-card p-3 transition-colors hover:bg-fd-accent/80 hover:text-fd-accent-foreground', rest.className), children: (_b = rest.children) !== null && _b !== void 0 ? _b : (jsxs(Fragment$1, { children: [banner, jsx("p", { className: "text-[15px] font-medium", children: child.text }), jsx("p", { className: "text-sm text-fd-muted-foreground empty:hidden", children: child.description })] })) })) }, `${j}-${child.url}`));
        });
        return (jsxs(NavigationMenuItem, { children: [jsx(NavigationMenuTrigger, Object.assign({}, props, { className: cn(navItemVariants(), 'rounded-md', props.className), children: item.url ? (jsx(Link, { href: item.url, external: item.external, children: item.text })) : (item.text) })), jsx(NavigationMenuContent, { className: "grid grid-cols-1 gap-2 p-4 md:grid-cols-2 lg:grid-cols-3", children: children })] }));
    }
    return (jsx(NavigationMenuItem, { children: jsx(NavigationMenuLink, { asChild: true, children: jsx(BaseLinkItem, Object.assign({ item: item, "aria-label": item.type === 'icon' ? item.label : undefined }, props, { className: cn(navItemVariants({ variant: item.type }), props.className), children: item.type === 'icon' ? item.icon : item.text })) }) }));
}
const Menu = NavigationMenuItem;
function MenuLinkItem(_a) {
    var _b;
    var { item } = _a, props = __rest(_a, ["item"]);
    if (item.type === 'custom')
        return jsx("div", { className: cn('grid', props.className), children: item.children });
    if (item.type === 'menu') {
        const header = (jsxs(Fragment$1, { children: [item.icon, item.text] }));
        return (jsxs("div", { className: cn('mb-4 flex flex-col', props.className), children: [jsx("p", { className: "mb-1 text-sm text-fd-muted-foreground", children: item.url ? (jsx(NavigationMenuLink, { asChild: true, children: jsx(Link, { href: item.url, external: item.external, children: header }) })) : (header) }), item.items.map((child, i) => (jsx(MenuLinkItem, { item: child }, i)))] }));
    }
    return (jsx(NavigationMenuLink, { asChild: true, children: jsxs(BaseLinkItem, { item: item, className: cn({
                main: 'inline-flex items-center gap-2 py-1.5 transition-colors hover:text-fd-popover-foreground/50 data-[active=true]:font-medium data-[active=true]:text-fd-primary [&_svg]:size-4',
                icon: buttonVariants({
                    size: 'icon',
                    color: 'ghost',
                }),
                button: buttonVariants({
                    color: 'secondary',
                    className: 'gap-1.5 [&_svg]:size-4',
                }),
            }[(_b = item.type) !== null && _b !== void 0 ? _b : 'main'], props.className), "aria-label": item.type === 'icon' ? item.label : undefined, children: [item.icon, item.type === 'icon' ? undefined : item.text] }) }));
}
function MenuTrigger(_a) {
    var { enableHover = false } = _a, props = __rest(_a, ["enableHover"]);
    return (jsx(NavigationMenuTrigger, Object.assign({}, props, { onPointerMove: enableHover ? undefined : (e) => e.preventDefault(), children: props.children })));
}
function MenuContent(props) {
    return (jsx(NavigationMenuContent, Object.assign({}, props, { className: cn('flex flex-col p-4 w-full max-w-full min-w-0', props.className), style: Object.assign({ minWidth: 0, width: '100%', maxWidth: '100%' }, props.style), children: props.children })));
}
function CompactLanguageToggle(_a) {
    var { contentClassName } = _a, props = __rest(_a, ["contentClassName"]);
    const context = useI18n();
    if (!context.locales)
        throw new Error('Missing `<I18nProvider />`');
    return (jsxs(Popover, { children: [jsx(PopoverTrigger, Object.assign({ "aria-label": context.text.chooseLanguage }, props, { className: cn(buttonVariants({
                    color: 'ghost',
                    className: 'gap-1.5 py-1.5 pl-2 pr-0.5',
                }), props.className), children: props.children })), jsxs(PopoverContent, { className: cn('flex min-w-[150px] z-1001 flex-col overflow-x-hidden rounded-xl border bg-fd-popover/60 p-0 text-fd-popover-foreground backdrop-blur-lg', contentClassName), children: [jsx("p", { className: "mb-1 p-2 text-xs font-medium text-fd-muted-foreground", children: context.text.chooseLanguage }), context.locales.map((item) => (jsx("button", { type: "button", className: cn('p-2 text-start text-sm', item.locale === context.locale
                            ? 'bg-fd-primary/10 font-medium text-fd-primary'
                            : 'hover:bg-fd-accent hover:text-fd-accent-foreground'), onClick: () => {
                            var _a;
                            (_a = context.onChange) === null || _a === void 0 ? void 0 : _a.call(context, item.locale);
                        }, children: item.name }, item.locale)))] })] }));
}
function isSecondary(item) {
    if ('secondary' in item && item.secondary != null)
        return item.secondary;
    return item.type === 'icon';
}
function isMobilePinned(item) {
    return Boolean(item.mobilePinned);
}

export { CustomHomeHeader };
