"use client";
import * as React from 'react';
import Editor from '@monaco-editor/react';
import { ChevronDown, Check, Copy } from 'lucide-react';
import { useTheme } from 'next-themes';
import { cn } from '@hanzo/ui/lib/utils';
import { Button } from '@hanzo/ui/button';
import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuItem } from '@hanzo/ui/dropdown-menu';
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';

var defaultLanguages = [
  "javascript",
  "typescript",
  "python",
  "java",
  "csharp",
  "cpp",
  "c",
  "go",
  "rust",
  "ruby",
  "php",
  "swift",
  "kotlin",
  "dart",
  "html",
  "css",
  "scss",
  "json",
  "xml",
  "yaml",
  "sql",
  "markdown",
  "shell",
  "plaintext"
];
var languageDisplayNames = {
  javascript: "JavaScript",
  typescript: "TypeScript",
  python: "Python",
  java: "Java",
  csharp: "C#",
  cpp: "C++",
  c: "C",
  go: "Go",
  rust: "Rust",
  ruby: "Ruby",
  php: "PHP",
  swift: "Swift",
  kotlin: "Kotlin",
  dart: "Dart",
  html: "HTML",
  css: "CSS",
  scss: "SCSS",
  json: "JSON",
  xml: "XML",
  yaml: "YAML",
  sql: "SQL",
  markdown: "Markdown",
  shell: "Shell",
  plaintext: "Plain Text"
};
var CodeEditor = React.forwardRef(
  ({
    value,
    defaultValue = "",
    language = "javascript",
    height = "400px",
    theme: themeProp = "auto",
    onChange,
    onMount,
    readOnly = false,
    lineNumbers = true,
    minimap = false,
    wordWrap = "on",
    fontSize = 14,
    className,
    showCopyButton = true,
    showLanguageSelector = true,
    availableLanguages = defaultLanguages
  }, ref) => {
    const { theme: systemTheme } = useTheme();
    const [selectedLanguage, setSelectedLanguage] = React.useState(language);
    const [copied, setCopied] = React.useState(false);
    const [editorValue, setEditorValue] = React.useState(value || defaultValue);
    React.useEffect(() => {
      if (value !== void 0) {
        setEditorValue(value);
      }
    }, [value]);
    const handleCopy = React.useCallback(async () => {
      try {
        await navigator.clipboard.writeText(editorValue);
        setCopied(true);
        setTimeout(() => setCopied(false), 2e3);
      } catch (err) {
        console.error("Failed to copy:", err);
      }
    }, [editorValue]);
    const handleChange = React.useCallback(
      (newValue, ev) => {
        setEditorValue(newValue || "");
        if (onChange) {
          onChange(newValue, ev);
        }
      },
      [onChange]
    );
    const resolveTheme = React.useMemo(() => {
      if (themeProp === "auto") {
        return systemTheme === "dark" ? "vs-dark" : "light";
      }
      return themeProp === "dark" ? "vs-dark" : "light";
    }, [themeProp, systemTheme]);
    return /* @__PURE__ */ jsxs(
      "div",
      {
        ref,
        className: cn(
          "relative overflow-hidden rounded-md border bg-background",
          className
        ),
        children: [
          (showLanguageSelector || showCopyButton) && /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between border-b bg-muted/50 px-3 py-2", children: [
            showLanguageSelector && /* @__PURE__ */ jsxs(DropdownMenu, { children: [
              /* @__PURE__ */ jsx(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(
                Button,
                {
                  variant: "ghost",
                  size: "sm",
                  className: "h-8 gap-1 px-2 text-xs font-mono",
                  children: [
                    languageDisplayNames[selectedLanguage] || selectedLanguage,
                    /* @__PURE__ */ jsx(ChevronDown, { className: "h-3 w-3" })
                  ]
                }
              ) }),
              /* @__PURE__ */ jsx(
                DropdownMenuContent,
                {
                  align: "start",
                  className: "max-h-80 overflow-auto",
                  children: availableLanguages.map((lang) => /* @__PURE__ */ jsx(
                    DropdownMenuItem,
                    {
                      onClick: () => setSelectedLanguage(lang),
                      className: "font-mono text-xs",
                      children: languageDisplayNames[lang] || lang
                    },
                    lang
                  ))
                }
              )
            ] }),
            showCopyButton && /* @__PURE__ */ jsx(
              Button,
              {
                variant: "ghost",
                size: "sm",
                className: "h-8 gap-1.5 px-2 text-xs",
                onClick: handleCopy,
                disabled: !editorValue,
                children: copied ? /* @__PURE__ */ jsxs(Fragment, { children: [
                  /* @__PURE__ */ jsx(Check, { className: "h-3 w-3" }),
                  "Copied!"
                ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
                  /* @__PURE__ */ jsx(Copy, { className: "h-3 w-3" }),
                  "Copy"
                ] })
              }
            )
          ] }),
          /* @__PURE__ */ jsx(
            Editor,
            {
              height,
              defaultLanguage: language,
              language: selectedLanguage,
              value: editorValue,
              defaultValue,
              theme: resolveTheme,
              onChange: handleChange,
              onMount,
              options: {
                readOnly,
                lineNumbers: lineNumbers ? "on" : "off",
                minimap: {
                  enabled: minimap
                },
                wordWrap,
                fontSize,
                scrollBeyondLastLine: false,
                automaticLayout: true,
                tabSize: 2,
                insertSpaces: true,
                formatOnPaste: true,
                formatOnType: true,
                scrollbar: {
                  vertical: "auto",
                  horizontal: "auto",
                  verticalScrollbarSize: 10,
                  horizontalScrollbarSize: 10
                },
                padding: {
                  top: 16,
                  bottom: 16
                }
              }
            }
          )
        ]
      }
    );
  }
);
CodeEditor.displayName = "CodeEditor";

export { CodeEditor };
