"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useCallback, useMemo } from "react";
import CodeMirror from "@uiw/react-codemirror";
import { json } from "@codemirror/lang-json";
import { githubLight, githubDark } from "@uiw/codemirror-theme-github";
import { useTheme } from "next-themes";
import { Copy, Wand2 } from "lucide-react";
import { Button } from "../../ui/button.js";
import { cn } from "../../../lib/utils/index.js";
function JsonEditor({
  value,
  onChange,
  readOnly = false,
  className,
  minHeight = "150px"
}) {
  const { resolvedTheme } = useTheme();
  const isDark = resolvedTheme === "dark";
  const handleFormat = useCallback(() => {
    try {
      const parsed = JSON.parse(value);
      const formatted = JSON.stringify(parsed, null, 2);
      onChange == null ? void 0 : onChange(formatted);
    } catch {
    }
  }, [value, onChange]);
  const handleCopy = useCallback(() => {
    navigator.clipboard.writeText(value);
  }, [value]);
  const isValidJson = useMemo(() => {
    if (!value.trim()) return true;
    try {
      JSON.parse(value);
      return true;
    } catch {
      return false;
    }
  }, [value]);
  return /* @__PURE__ */ jsxs(
    "div",
    {
      className: cn("flex flex-col border rounded-md overflow-hidden", className),
      "data-cy": "json-editor",
      children: [
        /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1 px-2 py-1 border-b bg-muted/30", children: [
          !readOnly && /* @__PURE__ */ jsxs(
            Button,
            {
              variant: "ghost",
              size: "sm",
              onClick: handleFormat,
              className: "h-7 px-2 text-xs",
              disabled: !isValidJson,
              title: "Format JSON",
              "data-cy": "json-editor-format",
              children: [
                /* @__PURE__ */ jsx(Wand2, { className: "h-3.5 w-3.5 mr-1" }),
                "Format"
              ]
            }
          ),
          /* @__PURE__ */ jsxs(
            Button,
            {
              variant: "ghost",
              size: "sm",
              onClick: handleCopy,
              className: "h-7 px-2 text-xs",
              title: "Copy to clipboard",
              "data-cy": "json-editor-copy",
              children: [
                /* @__PURE__ */ jsx(Copy, { className: "h-3.5 w-3.5 mr-1" }),
                "Copy"
              ]
            }
          ),
          /* @__PURE__ */ jsx("div", { className: "flex-1" }),
          !isValidJson && /* @__PURE__ */ jsx("span", { className: "text-xs text-destructive", "data-cy": "json-editor-error", children: "Invalid JSON" })
        ] }),
        /* @__PURE__ */ jsx(
          CodeMirror,
          {
            value,
            onChange,
            extensions: [json()],
            theme: isDark ? githubDark : githubLight,
            readOnly,
            basicSetup: {
              lineNumbers: true,
              foldGutter: true,
              // Expand/collapse for objects/arrays
              bracketMatching: true,
              autocompletion: true
            },
            style: { minHeight },
            "data-cy": "json-editor-codemirror"
          }
        )
      ]
    }
  );
}
export {
  JsonEditor
};
