"use client";
import { clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import * as React from 'react';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import { Slot } from '@radix-ui/react-slot';
import { cva } from 'class-variance-authority';
import * as SeparatorPrimitive from '@radix-ui/react-separator';
import { Img } from '@page-speed/img';

// lib/utils.ts
function cn(...inputs) {
  return twMerge(clsx(inputs));
}
var svgCache = /* @__PURE__ */ new Map();
function DynamicIcon({
  name,
  size = 28,
  color,
  className,
  alt
}) {
  const [svgContent, setSvgContent] = React.useState(null);
  const [isLoading, setIsLoading] = React.useState(true);
  const [error, setError] = React.useState(null);
  const { url, iconName } = React.useMemo(() => {
    const separator = name.includes("/") ? "/" : ":";
    const [prefix, iconName2] = name.split(separator);
    const baseUrl = `https://icons.opensite.ai/api/icon/${prefix}/${iconName2}?format=svg&width=${size}&height=${size}`;
    return {
      url: baseUrl,
      iconName: iconName2
    };
  }, [name, size]);
  React.useEffect(() => {
    let isMounted = true;
    const fetchSvg = async () => {
      const cached = svgCache.get(url);
      if (cached) {
        if (isMounted) {
          setSvgContent(cached);
          setIsLoading(false);
        }
        return;
      }
      try {
        setIsLoading(true);
        setError(null);
        const response = await fetch(url);
        if (!response.ok) {
          throw new Error(`Failed to fetch icon: ${response.status}`);
        }
        let svg = await response.text();
        svg = processSvgForCurrentColor(svg);
        svgCache.set(url, svg);
        if (isMounted) {
          setSvgContent(svg);
          setIsLoading(false);
        }
      } catch (err) {
        if (isMounted) {
          setError(err instanceof Error ? err.message : "Failed to load icon");
          setIsLoading(false);
        }
      }
    };
    fetchSvg();
    return () => {
      isMounted = false;
    };
  }, [url]);
  if (isLoading) {
    return /* @__PURE__ */ jsx(
      "span",
      {
        className: cn("inline-block", className),
        style: { width: size, height: size },
        "aria-hidden": "true"
      }
    );
  }
  if (error || !svgContent) {
    return /* @__PURE__ */ jsx(
      "span",
      {
        className: cn("inline-block", className),
        style: { width: size, height: size },
        role: "img",
        "aria-label": alt || iconName
      }
    );
  }
  return /* @__PURE__ */ jsx(
    "span",
    {
      className: cn("inline-flex items-center justify-center", className),
      style: {
        width: size,
        height: size,
        color: color || "inherit"
      },
      role: "img",
      "aria-label": alt || iconName,
      dangerouslySetInnerHTML: { __html: svgContent }
    }
  );
}
function processSvgForCurrentColor(svg) {
  let processed = svg;
  processed = processed.replace(
    /stroke=["'](#000000|#000|black)["']/gi,
    'stroke="currentColor"'
  );
  processed = processed.replace(
    /fill=["'](#000000|#000|black)["']/gi,
    'fill="currentColor"'
  );
  return processed;
}
var badgeVariants = cva(
  "inline-flex items-center justify-center rounded-full border px-2 py-0.5 text-xs font-medium w-fit whitespace-nowrap shrink-0 [&>svg]:size-3 gap-1 [&>svg]:pointer-events-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive transition-[color,box-shadow] overflow-hidden",
  {
    variants: {
      variant: {
        default: "border-transparent bg-primary text-primary-foreground [a&]:hover:bg-primary/90",
        secondary: "border-transparent bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/90",
        destructive: "border-transparent bg-destructive text-white [a&]:hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60",
        outline: "text-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground"
      }
    },
    defaultVariants: {
      variant: "default"
    }
  }
);
function Badge({
  className,
  variant,
  asChild = false,
  ...props
}) {
  const Comp = asChild ? Slot : "span";
  return /* @__PURE__ */ jsx(
    Comp,
    {
      "data-slot": "badge",
      className: cn(badgeVariants({ variant }), className),
      ...props
    }
  );
}
function Card({ className, ...props }) {
  return /* @__PURE__ */ jsx(
    "div",
    {
      "data-slot": "card",
      className: cn(
        "bg-card text-card-foreground flex flex-col gap-6 rounded-xl border py-6 shadow-sm",
        className
      ),
      ...props
    }
  );
}
function Separator({
  className,
  orientation = "horizontal",
  decorative = true,
  ...props
}) {
  return /* @__PURE__ */ jsx(
    SeparatorPrimitive.Root,
    {
      "data-slot": "separator",
      decorative,
      orientation,
      className: cn(
        "bg-border shrink-0 data-[orientation=horizontal]:h-px data-[orientation=horizontal]:w-full data-[orientation=vertical]:h-full data-[orientation=vertical]:w-px",
        className
      ),
      ...props
    }
  );
}
function FeatureBentoUtilities({
  label,
  labelIconName = "lucide/square-dashed-mouse-pointer",
  labelIcon,
  title,
  description,
  leftColumnCards,
  leftColumnSlot,
  rightColumnCards,
  rightColumnSlot,
  className,
  containerClassName,
  headerClassName,
  labelClassName,
  titleClassName,
  descriptionClassName,
  gridClassName,
  columnClassName,
  optixFlowConfig
}) {
  const renderCard = (card, index) => {
    const hasImage = card.imageSrc || card.imageSlot;
    const cardClasses = cn(
      hasImage ? "overflow-hidden pt-0" : "p-6",
      card.isDashed && "border-dashed bg-transparent shadow-none",
      card.className
    );
    const renderImage = () => {
      if (card.imageSlot) return card.imageSlot;
      if (card.imageSrc) {
        return /* @__PURE__ */ jsx(
          Img,
          {
            src: card.imageSrc,
            alt: card.imageAlt || "",
            className: "aspect-video w-full object-cover",
            loading: "lazy",
            optixFlowConfig
          }
        );
      }
      return null;
    };
    const renderTitle = () => {
      if (!card.title) return null;
      const titleContent = /* @__PURE__ */ jsxs(Fragment, { children: [
        card.title,
        card.showSparkle && /* @__PURE__ */ jsx(DynamicIcon, { name: "lucide/sparkles", size: 16 }),
        card.badge && /* @__PURE__ */ jsx(Badge, { variant: "outline", children: card.badge })
      ] });
      return /* @__PURE__ */ jsx("div", { className: "mb-1 flex items-center gap-2 font-medium", children: titleContent });
    };
    if (hasImage) {
      return /* @__PURE__ */ jsxs(Card, { className: cardClasses, children: [
        renderImage(),
        /* @__PURE__ */ jsxs("div", { className: "p-6", children: [
          renderTitle(),
          card.description && /* @__PURE__ */ jsx("p", { className: "text-muted-foreground", children: card.description })
        ] })
      ] }, index);
    }
    return /* @__PURE__ */ jsxs(Card, { className: cardClasses, children: [
      renderTitle(),
      card.description && /* @__PURE__ */ jsx("p", { className: "text-muted-foreground", children: card.description })
    ] }, index);
  };
  const renderColumn = (cards, slot) => {
    if (slot) return slot;
    if (!cards || cards.length === 0) return null;
    return cards.map((card, index) => renderCard(card, index));
  };
  const labelIconElement = labelIcon ?? (labelIconName ? /* @__PURE__ */ jsx(DynamicIcon, { name: labelIconName, size: 20 }) : null);
  return /* @__PURE__ */ jsx("section", { className: cn("bg-gray-50 py-32 dark:bg-background", className), children: /* @__PURE__ */ jsxs("div", { className: cn("container max-w-7xl", containerClassName), children: [
    /* @__PURE__ */ jsxs("div", { className: cn("flex items-center gap-2 text-muted-foreground", labelClassName), children: [
      labelIconElement,
      label && (typeof label === "string" ? /* @__PURE__ */ jsx("p", { className: "text-sm", children: label }) : /* @__PURE__ */ jsx("div", { className: "text-sm", children: label }))
    ] }),
    /* @__PURE__ */ jsx(Separator, { className: "mt-3 mb-8" }),
    /* @__PURE__ */ jsxs("div", { className: cn("flex flex-col justify-between gap-6 md:flex-row", headerClassName), children: [
      title && (typeof title === "string" ? /* @__PURE__ */ jsx("h2", { className: cn("text-3xl font-medium md:w-1/2", titleClassName), children: title }) : /* @__PURE__ */ jsx("div", { className: cn("text-3xl font-medium md:w-1/2", titleClassName), children: title })),
      description && (typeof description === "string" ? /* @__PURE__ */ jsx("p", { className: cn("md:w-1/2", descriptionClassName), children: description }) : /* @__PURE__ */ jsx("div", { className: cn("md:w-1/2", descriptionClassName), children: description }))
    ] }),
    /* @__PURE__ */ jsxs("div", { className: cn("mt-11 flex flex-col gap-6 md:flex-row", gridClassName), children: [
      /* @__PURE__ */ jsx("div", { className: cn("flex w-full flex-col gap-6", columnClassName), children: renderColumn(leftColumnCards, leftColumnSlot) }),
      /* @__PURE__ */ jsx("div", { className: cn("flex w-full flex-col gap-6", columnClassName), children: renderColumn(rightColumnCards, rightColumnSlot) })
    ] })
  ] }) });
}

export { FeatureBentoUtilities };
