"use client";
'use strict';

var React = require('react');
var classVarianceAuthority = require('class-variance-authority');
var lucideReact = require('lucide-react');
var shiki = require('shiki');
var utils = require('@hanzo/ui/lib/utils');
var badge = require('@hanzo/ui/badge');
var button = require('@hanzo/ui/button');
var skeleton = require('@hanzo/ui/skeleton');
var jsxRuntime = require('react/jsx-runtime');

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespace(React);

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var codeSnippetVariants = classVarianceAuthority.cva("relative group", {
  variants: {
    variant: {
      default: "rounded-lg border bg-muted/50",
      inline: "inline-block rounded-md bg-muted px-2 py-1",
      minimal: "bg-transparent"
    },
    theme: {
      dark: "bg-slate-950 border-slate-800",
      light: "bg-slate-50 border-slate-200",
      github: "bg-white border-slate-200",
      "github-dark": "bg-slate-900 border-slate-700",
      terminal: "bg-black border-green-500/30 text-green-400",
      retro: "bg-amber-50 border-amber-200 text-amber-900"
    },
    size: {
      xs: "text-xs",
      sm: "text-sm",
      default: "text-sm",
      lg: "text-base"
    }
  },
  defaultVariants: {
    variant: "default",
    theme: "dark",
    size: "default"
  }
});
var CodeSnippet = React__namespace.forwardRef(
  ({
    className,
    variant,
    theme,
    size,
    code,
    language = "text",
    filename,
    showLineNumbers = false,
    highlightLines = [],
    showCopyButton = true,
    showLanguageBadge = true,
    showHeader = true,
    maxHeight,
    expandable = false,
    collapsedHeight = "200px",
    wrapLines = false,
    startLineNumber = 1,
    ...props
  }, ref) => {
    const [highlightedCode, setHighlightedCode] = React__namespace.useState("");
    const [copied, setCopied] = React__namespace.useState(false);
    const [isLoading, setIsLoading] = React__namespace.useState(true);
    const [isExpanded, setIsExpanded] = React__namespace.useState(!expandable);
    const themeMap = {
      dark: "github-dark",
      light: "github-light",
      github: "github-light",
      "github-dark": "github-dark",
      terminal: "github-dark",
      retro: "github-light"
    };
    React__namespace.useEffect(() => {
      const highlightCode = /* @__PURE__ */ __name(async () => {
        setIsLoading(true);
        try {
          const supportedLang = Object.keys(shiki.bundledLanguages).includes(language);
          const highlighted = await shiki.codeToHtml(code, {
            lang: supportedLang ? language : "text",
            theme: themeMap[theme || "dark"]
          });
          const tempDiv = document.createElement("div");
          tempDiv.innerHTML = highlighted;
          const codeElement = tempDiv.querySelector("code");
          setHighlightedCode(codeElement?.innerHTML || highlighted);
        } catch (error) {
          console.error("Failed to highlight code:", error);
          const escaped = code.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
          setHighlightedCode(escaped);
        } finally {
          setIsLoading(false);
        }
      }, "highlightCode");
      highlightCode();
    }, [code, language, theme]);
    const copyToClipboard = React__namespace.useCallback(async () => {
      try {
        await navigator.clipboard.writeText(code);
        setCopied(true);
        setTimeout(() => setCopied(false), 2e3);
      } catch (error) {
        console.error("Failed to copy code:", error);
      }
    }, [code]);
    const lines = highlightedCode.split("\n").filter((line, index, array) => {
      return line.trim() !== "" || index < array.length - 1;
    });
    const renderInline = /* @__PURE__ */ __name(() => /* @__PURE__ */ jsxRuntime.jsx(
      "code",
      {
        className: utils.cn(
          codeSnippetVariants({ variant, theme, size }),
          "font-mono",
          className
        ),
        dangerouslySetInnerHTML: { __html: highlightedCode }
      }
    ), "renderInline");
    const renderBlock = /* @__PURE__ */ __name(() => /* @__PURE__ */ jsxRuntime.jsxs(
      "div",
      {
        ref,
        className: utils.cn(
          codeSnippetVariants({ variant, theme, size }),
          "overflow-hidden",
          className
        ),
        ...props,
        children: [
          showHeader && (filename || language || showCopyButton || expandable) && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between border-b px-4 py-2 bg-muted/30", children: [
            /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
              filename && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
                /* @__PURE__ */ jsxRuntime.jsx(lucideReact.FileText, { className: "h-3.5 w-3.5 text-muted-foreground" }),
                /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-sm font-medium", children: filename })
              ] }),
              showLanguageBadge && language && /* @__PURE__ */ jsxRuntime.jsx(badge.Badge, { variant: "secondary", className: "text-xs", children: language })
            ] }),
            /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1", children: [
              expandable && /* @__PURE__ */ jsxRuntime.jsxs(
                button.Button,
                {
                  variant: "ghost",
                  size: "sm",
                  onClick: () => setIsExpanded(!isExpanded),
                  className: "h-8 w-8 p-0",
                  children: [
                    isExpanded ? /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Minimize2, { className: "h-3.5 w-3.5" }) : /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Maximize2, { className: "h-3.5 w-3.5" }),
                    /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: isExpanded ? "Collapse" : "Expand" })
                  ]
                }
              ),
              showCopyButton && /* @__PURE__ */ jsxRuntime.jsxs(
                button.Button,
                {
                  variant: "ghost",
                  size: "sm",
                  onClick: copyToClipboard,
                  className: "h-8 w-8 p-0 opacity-0 group-hover:opacity-100 transition-opacity",
                  children: [
                    copied ? /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Check, { className: "h-3.5 w-3.5 text-green-500" }) : /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Copy, { className: "h-3.5 w-3.5" }),
                    /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: copied ? "Copied" : "Copy code" })
                  ]
                }
              )
            ] })
          ] }),
          /* @__PURE__ */ jsxRuntime.jsx(
            "div",
            {
              className: utils.cn("overflow-auto", !wrapLines && "overflow-x-auto"),
              style: {
                maxHeight: isExpanded ? maxHeight : collapsedHeight
              },
              children: isLoading ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "p-4 space-y-2", children: Array.from({ length: Math.min(lines.length || 5, 10) }).map(
                (_, i) => /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-4", children: [
                  showLineNumbers && /* @__PURE__ */ jsxRuntime.jsx(skeleton.Skeleton, { className: "h-4 w-8" }),
                  /* @__PURE__ */ jsxRuntime.jsx(skeleton.Skeleton, { className: "h-4 flex-1" })
                ] }, i)
              ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "font-mono", children: lines.map((line, index) => {
                const lineNumber = index + startLineNumber;
                const isHighlighted = highlightLines.includes(lineNumber);
                return /* @__PURE__ */ jsxRuntime.jsxs(
                  "div",
                  {
                    className: utils.cn(
                      "group relative flex min-h-[1.5rem] items-center px-4 py-0.5",
                      isHighlighted && "bg-blue-500/10 border-l-2 border-l-blue-500",
                      !wrapLines && "whitespace-nowrap"
                    ),
                    children: [
                      showLineNumbers && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "mr-4 inline-block w-8 select-none text-right text-xs text-muted-foreground", children: lineNumber }),
                      /* @__PURE__ */ jsxRuntime.jsx(
                        "div",
                        {
                          className: utils.cn(
                            "flex-1",
                            wrapLines ? "whitespace-pre-wrap break-words" : "overflow-x-auto"
                          ),
                          dangerouslySetInnerHTML: { __html: line }
                        }
                      )
                    ]
                  },
                  index
                );
              }) })
            }
          ),
          expandable && !isExpanded && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "absolute bottom-0 left-0 right-0 h-8 bg-gradient-to-t from-background to-transparent pointer-events-none" }),
          variant === "minimal" && showCopyButton && /* @__PURE__ */ jsxRuntime.jsx(
            button.Button,
            {
              variant: "ghost",
              size: "sm",
              onClick: copyToClipboard,
              className: "absolute top-2 right-2 h-8 w-8 p-0 opacity-0 group-hover:opacity-100 transition-opacity",
              children: copied ? /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Check, { className: "h-3.5 w-3.5 text-green-500" }) : /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Copy, { className: "h-3.5 w-3.5" })
            }
          )
        ]
      }
    ), "renderBlock");
    if (variant === "inline") {
      return renderInline();
    }
    return renderBlock();
  }
);
CodeSnippet.displayName = "CodeSnippet";
var InlineCode = React__namespace.forwardRef(({ children, language, className, ...props }, ref) => {
  return /* @__PURE__ */ jsxRuntime.jsx(
    CodeSnippet,
    {
      ref,
      code: children,
      language,
      variant: "inline",
      showCopyButton: false,
      showLanguageBadge: false,
      showHeader: false,
      className,
      ...props
    }
  );
});
InlineCode.displayName = "InlineCode";

exports.CodeSnippet = CodeSnippet;
exports.InlineCode = InlineCode;
exports.codeSnippetVariants = codeSnippetVariants;
