"use client";
'use strict';

var React = require('react');
var Editor = require('@monaco-editor/react');
var lucideReact = require('lucide-react');
var nextThemes = require('next-themes');
var utils = require('@hanzo/ui/lib/utils');
var button = require('@hanzo/ui/button');
var dropdownMenu = require('@hanzo/ui/dropdown-menu');
var jsxRuntime = require('react/jsx-runtime');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

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 Editor__default = /*#__PURE__*/_interopDefault(Editor);

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__namespace.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 } = nextThemes.useTheme();
    const [selectedLanguage, setSelectedLanguage] = React__namespace.useState(language);
    const [copied, setCopied] = React__namespace.useState(false);
    const [editorValue, setEditorValue] = React__namespace.useState(value || defaultValue);
    React__namespace.useEffect(() => {
      if (value !== void 0) {
        setEditorValue(value);
      }
    }, [value]);
    const handleCopy = React__namespace.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__namespace.useCallback(
      (newValue, ev) => {
        setEditorValue(newValue || "");
        if (onChange) {
          onChange(newValue, ev);
        }
      },
      [onChange]
    );
    const resolveTheme = React__namespace.useMemo(() => {
      if (themeProp === "auto") {
        return systemTheme === "dark" ? "vs-dark" : "light";
      }
      return themeProp === "dark" ? "vs-dark" : "light";
    }, [themeProp, systemTheme]);
    return /* @__PURE__ */ jsxRuntime.jsxs(
      "div",
      {
        ref,
        className: utils.cn(
          "relative overflow-hidden rounded-md border bg-background",
          className
        ),
        children: [
          (showLanguageSelector || showCopyButton) && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between border-b bg-muted/50 px-3 py-2", children: [
            showLanguageSelector && /* @__PURE__ */ jsxRuntime.jsxs(dropdownMenu.DropdownMenu, { children: [
              /* @__PURE__ */ jsxRuntime.jsx(dropdownMenu.DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsxs(
                button.Button,
                {
                  variant: "ghost",
                  size: "sm",
                  className: "h-8 gap-1 px-2 text-xs font-mono",
                  children: [
                    languageDisplayNames[selectedLanguage] || selectedLanguage,
                    /* @__PURE__ */ jsxRuntime.jsx(lucideReact.ChevronDown, { className: "h-3 w-3" })
                  ]
                }
              ) }),
              /* @__PURE__ */ jsxRuntime.jsx(
                dropdownMenu.DropdownMenuContent,
                {
                  align: "start",
                  className: "max-h-80 overflow-auto",
                  children: availableLanguages.map((lang) => /* @__PURE__ */ jsxRuntime.jsx(
                    dropdownMenu.DropdownMenuItem,
                    {
                      onClick: () => setSelectedLanguage(lang),
                      className: "font-mono text-xs",
                      children: languageDisplayNames[lang] || lang
                    },
                    lang
                  ))
                }
              )
            ] }),
            showCopyButton && /* @__PURE__ */ jsxRuntime.jsx(
              button.Button,
              {
                variant: "ghost",
                size: "sm",
                className: "h-8 gap-1.5 px-2 text-xs",
                onClick: handleCopy,
                disabled: !editorValue,
                children: copied ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
                  /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Check, { className: "h-3 w-3" }),
                  "Copied!"
                ] }) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
                  /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Copy, { className: "h-3 w-3" }),
                  "Copy"
                ] })
              }
            )
          ] }),
          /* @__PURE__ */ jsxRuntime.jsx(
            Editor__default.default,
            {
              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";

exports.CodeEditor = CodeEditor;
