"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import React, { useMemo } from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { Badge } from "../ui/badge.js";
import { Button } from "../ui/button.js";
import { Separator } from "../ui/separator.js";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger
} from "../ui/tooltip.js";
import { cn } from "../../lib/utils/index.js";
import { useAuth } from "../../hooks/useAuth.js";
function getEntityUsage(entityName) {
  const mockData = {
    "user": 25,
    "task": 142,
    "project": 8,
    "client": 12
  };
  return { currentRecords: mockData[entityName] || 0 };
}
function EntityNavigation({
  entities,
  variant = "sidebar",
  showLabels = true,
  showBadges = true,
  showTooltips = variant === "sidebar",
  className,
  onNavigate
}) {
  const { user } = useAuth();
  const pathname = usePathname();
  const accessibleEntities = useMemo(() => {
    if (!user) return [];
    return entities.filter((entity) => {
      var _a, _b;
      return entity.enabled && ((_b = (_a = entity.ui) == null ? void 0 : _a.dashboard) == null ? void 0 : _b.showInMenu);
    }).map((entity) => {
      const isAccessible = true;
      const usage = getEntityUsage(entity.slug);
      return {
        entity,
        isActive: pathname.startsWith(`/dashboard/${entity.slug}`),
        isAccessible,
        limits: {
          maxRecords: "unlimited",
          currentUsage: usage.currentRecords,
          isNearLimit: false
        },
        href: `/dashboard/${entity.slug}`
      };
    }).filter((item) => item.isAccessible).sort((a, b) => a.entity.names.singular.localeCompare(b.entity.names.singular));
  }, [user, entities, pathname]);
  if (!user || accessibleEntities.length === 0) {
    return null;
  }
  const handleNavigation = (entityName) => {
    onNavigate == null ? void 0 : onNavigate(entityName);
  };
  if (variant === "sidebar") {
    return /* @__PURE__ */ jsx(TooltipProvider, { delayDuration: 300, children: /* @__PURE__ */ jsx("nav", { className: cn("space-y-1", className), children: accessibleEntities.map(({ entity, isActive, limits, href }) => {
      const IconComponent = entity.icon;
      return /* @__PURE__ */ jsxs(Tooltip, { delayDuration: 300, children: [
        /* @__PURE__ */ jsx(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(
          Link,
          {
            href,
            onClick: () => handleNavigation(entity.slug),
            className: cn(
              "flex items-center gap-3 rounded-lg px-3 py-2 text-sm font-medium transition-colors hover:bg-accent hover:text-accent-foreground",
              isActive ? "bg-accent text-accent-foreground" : "text-muted-foreground"
            ),
            "data-cy": `nav-${entity.slug}`,
            children: [
              /* @__PURE__ */ jsx(IconComponent, { className: "h-4 w-4 shrink-0" }),
              showLabels && /* @__PURE__ */ jsx("span", { className: "truncate", children: entity.names.plural }),
              showBadges && limits && /* @__PURE__ */ jsxs("div", { className: "ml-auto flex items-center gap-1", children: [
                limits.isNearLimit && /* @__PURE__ */ jsx(Badge, { variant: "outline", className: "text-xs", children: "\u26A0" }),
                typeof limits.currentUsage === "number" && /* @__PURE__ */ jsxs(Badge, { variant: "secondary", className: "text-xs", children: [
                  limits.currentUsage,
                  limits.maxRecords !== "unlimited" && `/${limits.maxRecords}`
                ] })
              ] })
            ]
          }
        ) }),
        showTooltips && /* @__PURE__ */ jsxs(TooltipContent, { side: "right", className: "space-y-1", children: [
          /* @__PURE__ */ jsx("p", { className: "font-medium", children: entity.names.plural }),
          /* @__PURE__ */ jsxs("p", { className: "text-xs text-muted-foreground", children: [
            "Manage your ",
            entity.names.plural.toLowerCase()
          ] }),
          limits && /* @__PURE__ */ jsxs("div", { className: "text-xs", children: [
            /* @__PURE__ */ jsxs("p", { children: [
              "Usage: ",
              limits.currentUsage || 0,
              limits.maxRecords !== "unlimited" && ` / ${limits.maxRecords}`
            ] }),
            limits.isNearLimit && /* @__PURE__ */ jsx("p", { className: "text-yellow-600", children: "Approaching limit" })
          ] })
        ] })
      ] }, entity.slug);
    }) }) });
  }
  if (variant === "horizontal") {
    return /* @__PURE__ */ jsx("nav", { className: cn("flex items-center space-x-1", className), children: accessibleEntities.map(({ entity, isActive, limits, href }, index) => {
      const IconComponent = entity.icon;
      return /* @__PURE__ */ jsxs(React.Fragment, { children: [
        index > 0 && /* @__PURE__ */ jsx(Separator, { orientation: "vertical", className: "h-6" }),
        /* @__PURE__ */ jsx(TooltipProvider, { delayDuration: 300, children: /* @__PURE__ */ jsxs(Tooltip, { children: [
          /* @__PURE__ */ jsx(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ jsx(
            Button,
            {
              variant: isActive ? "default" : "ghost",
              size: "sm",
              asChild: true,
              className: "h-9",
              children: /* @__PURE__ */ jsxs(
                Link,
                {
                  href,
                  onClick: () => handleNavigation(entity.slug),
                  "data-cy": `nav-${entity.slug}`,
                  children: [
                    /* @__PURE__ */ jsx(IconComponent, { className: "h-4 w-4" }),
                    showLabels && /* @__PURE__ */ jsx("span", { className: "ml-2", children: entity.names.plural }),
                    showBadges && (limits == null ? void 0 : limits.isNearLimit) && /* @__PURE__ */ jsx(Badge, { variant: "outline", className: "ml-2 text-xs", children: "\u26A0" })
                  ]
                }
              )
            }
          ) }),
          showTooltips && /* @__PURE__ */ jsx(TooltipContent, { children: /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
            /* @__PURE__ */ jsx("p", { className: "font-medium", children: entity.names.plural }),
            limits && /* @__PURE__ */ jsxs("p", { className: "text-xs", children: [
              limits.currentUsage || 0,
              limits.maxRecords !== "unlimited" && ` / ${limits.maxRecords}`
            ] })
          ] }) })
        ] }) })
      ] }, entity.slug);
    }) });
  }
  return /* @__PURE__ */ jsx("div", { className: cn("space-y-1", className), children: accessibleEntities.map(({ entity, isActive, limits, href }) => {
    const IconComponent = entity.icon;
    return /* @__PURE__ */ jsxs(
      Link,
      {
        href,
        onClick: () => handleNavigation(entity.slug),
        className: cn(
          "flex items-center justify-between rounded-md px-3 py-2 text-sm transition-colors hover:bg-accent hover:text-accent-foreground",
          isActive ? "bg-accent text-accent-foreground" : "text-muted-foreground"
        ),
        "data-cy": `nav-${entity.slug}`,
        children: [
          /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-3", children: [
            /* @__PURE__ */ jsx(IconComponent, { className: "h-4 w-4" }),
            /* @__PURE__ */ jsx("span", { children: entity.names.plural })
          ] }),
          showBadges && limits && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1", children: [
            limits.isNearLimit && /* @__PURE__ */ jsx(Badge, { variant: "outline", className: "text-xs", children: "\u26A0" }),
            /* @__PURE__ */ jsxs(Badge, { variant: "secondary", className: "text-xs", children: [
              limits.currentUsage || 0,
              limits.maxRecords !== "unlimited" && `/${limits.maxRecords}`
            ] })
          ] })
        ]
      },
      entity.slug
    );
  }) });
}
function EntityNavigationSection({
  title,
  entities,
  variant = "sidebar",
  showLabels = true,
  showBadges = true,
  showTooltips = true,
  className,
  onNavigate
}) {
  return /* @__PURE__ */ jsxs("div", { className: cn("space-y-2", className), children: [
    /* @__PURE__ */ jsx("h3", { className: "px-3 text-xs font-semibold text-muted-foreground uppercase tracking-wider", children: title }),
    /* @__PURE__ */ jsx(
      EntityNavigation,
      {
        entities,
        variant,
        showLabels,
        showBadges,
        showTooltips,
        onNavigate
      }
    )
  ] });
}
function FullEntityNavigation({
  variant = "sidebar",
  showLabels = true,
  showBadges = true,
  showTooltips = true,
  className,
  onNavigate,
  entities
}) {
  const { user } = useAuth();
  const entitySections = useMemo(() => {
    if (!user) return [];
    const allEntities = entities.filter((entity) => {
      var _a, _b;
      return entity.enabled && ((_b = (_a = entity.ui) == null ? void 0 : _a.dashboard) == null ? void 0 : _b.showInMenu);
    });
    const sections = [
      {
        title: "Core",
        entities: allEntities.filter(
          (entity) => ["user", "task"].includes(entity.slug)
        )
      },
      {
        title: "Business",
        entities: allEntities.filter(
          (entity) => ["client", "project", "invoice", "order"].includes(entity.slug)
        )
      },
      {
        title: "Other",
        entities: allEntities.filter(
          (entity) => !["user", "task", "client", "project", "invoice", "order"].includes(entity.slug)
        )
      }
    ].filter((section) => section.entities.length > 0);
    return sections;
  }, [user, entities]);
  if (!user) return null;
  return /* @__PURE__ */ jsx("div", { className: cn("space-y-6", className), children: entitySections.map((section) => /* @__PURE__ */ jsx(
    EntityNavigationSection,
    {
      title: section.title,
      entities: section.entities,
      variant,
      showLabels,
      showBadges,
      showTooltips,
      onNavigate
    },
    section.title
  )) });
}
export {
  EntityNavigation,
  EntityNavigationSection,
  FullEntityNavigation
};
