import {
  cn
} from "./chunk-ZAUUGK2Y.js";

// src/components/MonacoEditor/MonacoDiffEditor.tsx
import {
  forwardRef,
  lazy,
  Suspense,
  useImperativeHandle,
  useRef,
  useState
} from "react";
import {
  RenewIcon,
  SplitIcon,
  SquareOutlineIcon
} from "@octaviaflow/icons";

// src/components/MonacoEditor/octaviaflowTheme.ts
var OCTAVIAFLOW_LIGHT = {
  base: "vs",
  inherit: true,
  rules: [
    { token: "", foreground: "161616", background: "FFFFFF" },
    { token: "comment", foreground: "6F6F6F", fontStyle: "italic" },
    { token: "string", foreground: "198038" },
    { token: "string.key.json", foreground: "0F62FE" },
    { token: "string.value.json", foreground: "198038" },
    { token: "number", foreground: "0043CE" },
    { token: "keyword", foreground: "8A3FFC" },
    { token: "keyword.sql", foreground: "8A3FFC" },
    { token: "operator", foreground: "393939" },
    { token: "delimiter", foreground: "525252" },
    { token: "type", foreground: "0F62FE" },
    { token: "type.identifier", foreground: "0F62FE" },
    { token: "identifier", foreground: "161616" },
    { token: "constant", foreground: "0043CE" },
    { token: "boolean", foreground: "F1C21B", fontStyle: "bold" },
    { token: "tag", foreground: "DA1E28" },
    { token: "attribute.name", foreground: "8A3FFC" },
    { token: "attribute.value", foreground: "198038" },
    { token: "regexp", foreground: "DA1E28" },
    { token: "function", foreground: "0F62FE" },
    { token: "variable", foreground: "161616" },
    { token: "variable.parameter", foreground: "393939", fontStyle: "italic" },
    { token: "namespace", foreground: "0F62FE" }
  ],
  colors: {
    "editor.background": "#FFFFFF",
    "editor.foreground": "#161616",
    "editor.lineHighlightBackground": "#F4F4F4",
    "editor.lineHighlightBorder": "#00000000",
    "editorLineNumber.foreground": "#A8A8A8",
    "editorLineNumber.activeForeground": "#0F62FE",
    "editorCursor.foreground": "#0F62FE",
    "editor.selectionBackground": "#D0E2FF",
    "editor.selectionHighlightBackground": "#EDF5FF",
    "editor.wordHighlightBackground": "#EDF5FF",
    "editor.wordHighlightStrongBackground": "#D0E2FF",
    "editor.findMatchBackground": "#FFD7D9",
    "editor.findMatchHighlightBackground": "#FFE9EB",
    "editorIndentGuide.background1": "#F4F4F4",
    "editorIndentGuide.activeBackground1": "#E0E0E0",
    "editorWhitespace.foreground": "#E0E0E0",
    "editorBracketMatch.background": "#D0E2FF",
    "editorBracketMatch.border": "#0F62FE",
    "editor.foldBackground": "#F4F4F4",
    "editorGutter.background": "#F4F4F4",
    "editorGutter.modifiedBackground": "#0F62FE",
    "editorGutter.addedBackground": "#198038",
    "editorGutter.deletedBackground": "#DA1E28",
    "editorWidget.background": "#FFFFFF",
    "editorWidget.foreground": "#161616",
    "editorWidget.border": "#E0E0E0",
    "editorSuggestWidget.background": "#FFFFFF",
    "editorSuggestWidget.border": "#E0E0E0",
    "editorSuggestWidget.foreground": "#161616",
    "editorSuggestWidget.selectedBackground": "#EDF5FF",
    "editorSuggestWidget.selectedForeground": "#161616",
    "editorSuggestWidget.highlightForeground": "#0F62FE",
    "editorHoverWidget.background": "#FFFFFF",
    "editorHoverWidget.border": "#E0E0E0",
    "editorError.foreground": "#DA1E28",
    "editorWarning.foreground": "#F1C21B",
    "editorInfo.foreground": "#0F62FE",
    "diffEditor.insertedTextBackground": "#A7F0BA40",
    "diffEditor.removedTextBackground": "#FFB3B840",
    "diffEditor.insertedLineBackground": "#DEFBE640",
    "diffEditor.removedLineBackground": "#FFD7D940",
    "scrollbar.shadow": "#00000000",
    "scrollbarSlider.background": "#0F62FE20",
    "scrollbarSlider.hoverBackground": "#0F62FE40",
    "scrollbarSlider.activeBackground": "#0F62FE60",
    "minimap.background": "#FAFAFA",
    "minimap.selectionHighlight": "#0F62FE",
    "minimapSlider.background": "#0F62FE15",
    "minimapSlider.hoverBackground": "#0F62FE30",
    "minimapSlider.activeBackground": "#0F62FE50",
    "input.background": "#FFFFFF",
    "input.foreground": "#161616",
    "input.border": "#C6C6C6",
    "inputOption.activeBorder": "#0F62FE",
    focusBorder: "#0F62FE"
  }
};
var OCTAVIAFLOW_THEME_ID = "octaviaflow-light";

// src/components/MonacoEditor/MonacoDiffEditor.tsx
import { jsx, jsxs } from "react/jsx-runtime";
var DiffEditor = lazy(
  () => import("@monaco-editor/react").then((mod) => ({ default: mod.DiffEditor }))
);
function LoadingPlaceholder() {
  return /* @__PURE__ */ jsxs("div", { className: "ods-monaco__loading", children: [
    /* @__PURE__ */ jsx("span", { className: "ods-monaco__loading-spin", "aria-hidden": "true", children: /* @__PURE__ */ jsx(RenewIcon, {}) }),
    /* @__PURE__ */ jsx("span", { children: "Loading diff\u2026" })
  ] });
}
var MonacoDiffEditor = forwardRef(function MonacoDiffEditor2({
  original,
  modified,
  onModifiedChange,
  language = "plaintext",
  renderSideBySide = true,
  readOnly = false,
  hideUnchangedRegions = false,
  height = 360,
  minHeight = 200,
  originalLabel = "Original",
  modifiedLabel = "Modified",
  toolbar,
  options,
  onMount,
  className,
  theme,
  hideToolbar = false,
  ...rest
}, ref) {
  const editorRef = useRef(null);
  const monacoRef = useRef(null);
  const [sideBySide, setSideBySide] = useState(renderSideBySide);
  useImperativeHandle(
    ref,
    () => ({
      focus: () => editorRef.current?.getModifiedEditor().focus(),
      getDiffEditor: () => editorRef.current,
      getMonaco: () => monacoRef.current,
      getOriginalValue: () => editorRef.current?.getOriginalEditor().getValue() ?? "",
      getModifiedValue: () => editorRef.current?.getModifiedEditor().getValue() ?? "",
      setModifiedValue: (v) => editorRef.current?.getModifiedEditor().setValue(v)
    }),
    []
  );
  const handleMount = (diffEditor, monaco) => {
    editorRef.current = diffEditor;
    monacoRef.current = monaco;
    monaco.editor.defineTheme(OCTAVIAFLOW_THEME_ID, OCTAVIAFLOW_LIGHT);
    monaco.editor.setTheme(theme ?? OCTAVIAFLOW_THEME_ID);
    const modifiedEditor = diffEditor.getModifiedEditor();
    modifiedEditor.onDidChangeModelContent(() => {
      onModifiedChange?.(modifiedEditor.getValue());
    });
    onMount?.(diffEditor, monaco);
  };
  const mergedOptions = {
    readOnly,
    originalEditable: false,
    renderSideBySide: sideBySide,
    fontFamily: "ui-monospace, SFMono-Regular, 'JetBrains Mono', 'Fira Code', Menlo, Consolas, monospace",
    fontSize: 13,
    lineHeight: 20,
    automaticLayout: true,
    lineNumbersMinChars: 3,
    lineDecorationsWidth: 6,
    folding: true,
    glyphMargin: false,
    minimap: { enabled: false },
    scrollBeyondLastLine: false,
    smoothScrolling: true,
    renderOverviewRuler: true,
    diffWordWrap: "off",
    padding: { top: 8, bottom: 8 },
    hideUnchangedRegions: {
      enabled: hideUnchangedRegions,
      revealLineCount: 20,
      minimumLineCount: 3,
      contextLineCount: 3
    },
    scrollbar: {
      verticalScrollbarSize: 8,
      horizontalScrollbarSize: 8,
      useShadows: false
    },
    ...options
  };
  return /* @__PURE__ */ jsxs(
    "div",
    {
      ...rest,
      className: cn(
        "ods-monaco-diff",
        `ods-monaco-diff--${language}`,
        className
      ),
      children: [
        !hideToolbar && /* @__PURE__ */ jsxs("div", { className: "ods-monaco__toolbar", children: [
          /* @__PURE__ */ jsxs("div", { className: "ods-monaco__toolbar-left", children: [
            /* @__PURE__ */ jsxs("span", { className: "ods-monaco__lang", children: [
              "DIFF \xB7 ",
              language.toUpperCase()
            ] }),
            /* @__PURE__ */ jsxs("span", { className: "ods-monaco-diff__labels", children: [
              /* @__PURE__ */ jsx("span", { className: "ods-monaco-diff__label ods-monaco-diff__label--orig", children: originalLabel }),
              /* @__PURE__ */ jsx("span", { className: "ods-monaco-diff__arrow", "aria-hidden": "true", children: "\u2192" }),
              /* @__PURE__ */ jsx("span", { className: "ods-monaco-diff__label ods-monaco-diff__label--mod", children: modifiedLabel })
            ] }),
            toolbar
          ] }),
          /* @__PURE__ */ jsx("div", { className: "ods-monaco__toolbar-right", children: /* @__PURE__ */ jsx(
            "button",
            {
              type: "button",
              className: cn(
                "ods-monaco__btn",
                sideBySide && "ods-monaco__btn--active"
              ),
              onClick: () => setSideBySide((s) => !s),
              "aria-pressed": sideBySide,
              "aria-label": sideBySide ? "Switch to inline diff" : "Switch to side-by-side",
              title: sideBySide ? "Inline" : "Side-by-side",
              children: sideBySide ? /* @__PURE__ */ jsx(SplitIcon, {}) : /* @__PURE__ */ jsx(SquareOutlineIcon, {})
            }
          ) })
        ] }),
        /* @__PURE__ */ jsx("div", { className: "ods-monaco__body", style: { minHeight }, children: /* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx(LoadingPlaceholder, {}), children: /* @__PURE__ */ jsx(
          DiffEditor,
          {
            original,
            modified,
            language,
            height,
            theme: theme ?? OCTAVIAFLOW_THEME_ID,
            options: mergedOptions,
            onMount: handleMount,
            loading: /* @__PURE__ */ jsx(LoadingPlaceholder, {})
          }
        ) }) })
      ]
    }
  );
});
MonacoDiffEditor.displayName = "MonacoDiffEditor";

// src/components/MonacoEditor/MonacoEditor.tsx
import {
  forwardRef as forwardRef2,
  lazy as lazy2,
  Suspense as Suspense2,
  useCallback,
  useEffect,
  useImperativeHandle as useImperativeHandle2,
  useRef as useRef2,
  useState as useState2
} from "react";
import {
  CheckmarkIcon,
  CopyIcon,
  MagicWandIcon,
  MinimapOffIcon,
  MinimapOnIcon,
  RenewIcon as RenewIcon2,
  TextWrapIcon
} from "@octaviaflow/icons";
import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var Editor = lazy2(
  () => import("@monaco-editor/react").then((mod) => ({ default: mod.Editor }))
);
var MARKER_SEVERITY = {
  hint: 1,
  info: 2,
  warning: 4,
  error: 8
};
function LoadingPlaceholder2({ label }) {
  return /* @__PURE__ */ jsxs2("div", { className: "ods-monaco__loading", children: [
    /* @__PURE__ */ jsx2("span", { className: "ods-monaco__loading-spin", "aria-hidden": "true", children: /* @__PURE__ */ jsx2(RenewIcon2, {}) }),
    /* @__PURE__ */ jsx2("span", { children: label })
  ] });
}
var MonacoEditor = forwardRef2(
  function MonacoEditor2({
    value,
    onChange,
    language = "plaintext",
    readOnly = false,
    height = 320,
    minHeight = 120,
    toolbar,
    status,
    hideToolbar = false,
    hideFooter = false,
    showCopy = true,
    showFormat = true,
    showWordWrapToggle = true,
    showMinimapToggle = true,
    wordWrap: wordWrapProp,
    minimap: minimapProp,
    showLineNumbers = true,
    markers,
    options,
    onMount,
    onCopy,
    copySuccessTimeoutMs = 1200,
    className,
    ariaLabel = "Code editor",
    theme,
    ...rest
  }, ref) {
    const editorRef = useRef2(null);
    const monacoRef = useRef2(null);
    const [wordWrap, setWordWrap] = useState2(wordWrapProp ?? false);
    const [minimap, setMinimap] = useState2(minimapProp ?? false);
    const [copied, setCopied] = useState2(false);
    const [cursor, setCursor] = useState2({ line: 1, column: 1 });
    const [selection, setSelection] = useState2({ chars: 0, lines: 0 });
    const [lineCount, setLineCount] = useState2(value.split("\n").length);
    useImperativeHandle2(
      ref,
      () => ({
        focus: () => editorRef.current?.focus(),
        getEditor: () => editorRef.current,
        getMonaco: () => monacoRef.current,
        getValue: () => editorRef.current?.getValue() ?? "",
        setValue: (v) => editorRef.current?.setValue(v),
        format: () => editorRef.current?.getAction("editor.action.formatDocument")?.run()
      }),
      []
    );
    useEffect(() => {
      if (wordWrapProp !== void 0) setWordWrap(wordWrapProp);
    }, [wordWrapProp]);
    useEffect(() => {
      if (minimapProp !== void 0) setMinimap(minimapProp);
    }, [minimapProp]);
    useEffect(() => {
      editorRef.current?.updateOptions({
        wordWrap: wordWrap ? "on" : "off",
        minimap: { enabled: minimap }
      });
    }, [wordWrap, minimap]);
    useEffect(() => {
      const ed = editorRef.current;
      const monaco = monacoRef.current;
      if (!ed || !monaco || !markers) return;
      const model = ed.getModel();
      if (!model) return;
      monaco.editor.setModelMarkers(
        model,
        "octaviaflow",
        markers.map((m) => ({
          severity: MARKER_SEVERITY[m.severity ?? "error"],
          message: m.message,
          startLineNumber: m.line,
          startColumn: m.column ?? 1,
          endLineNumber: m.endLine ?? m.line,
          endColumn: m.endColumn ?? (m.column ?? 1) + 1,
          source: m.source
        }))
      );
    }, [markers]);
    const handleMount = useCallback(
      (ed, monaco) => {
        editorRef.current = ed;
        monacoRef.current = monaco;
        monaco.editor.defineTheme(OCTAVIAFLOW_THEME_ID, OCTAVIAFLOW_LIGHT);
        monaco.editor.setTheme(theme ?? OCTAVIAFLOW_THEME_ID);
        ed.onDidChangeCursorPosition((e) => {
          setCursor({ line: e.position.lineNumber, column: e.position.column });
        });
        ed.onDidChangeCursorSelection((e) => {
          const sel = e.selection;
          if (sel.isEmpty()) {
            setSelection({ chars: 0, lines: 0 });
            return;
          }
          const model2 = ed.getModel();
          if (!model2) return;
          const text = model2.getValueInRange(sel);
          setSelection({
            chars: text.length,
            lines: text.split("\n").length
          });
        });
        const model = ed.getModel();
        if (model) {
          setLineCount(model.getLineCount());
          model.onDidChangeContent(() => setLineCount(model.getLineCount()));
        }
        onMount?.(ed, monaco);
      },
      [onMount, theme]
    );
    const handleCopy = async () => {
      if (typeof navigator === "undefined") return;
      try {
        await navigator.clipboard.writeText(value);
        setCopied(true);
        onCopy?.(value);
        setTimeout(() => setCopied(false), copySuccessTimeoutMs);
      } catch {
      }
    };
    const handleFormat = () => {
      editorRef.current?.getAction("editor.action.formatDocument")?.run();
    };
    const mergedOptions = {
      readOnly,
      fontFamily: "ui-monospace, SFMono-Regular, 'JetBrains Mono', 'Fira Code', Menlo, Consolas, monospace",
      fontSize: 13,
      fontLigatures: true,
      lineHeight: 20,
      lineNumbers: showLineNumbers ? "on" : "off",
      lineNumbersMinChars: 3,
      lineDecorationsWidth: 6,
      folding: true,
      glyphMargin: false,
      minimap: { enabled: minimap },
      wordWrap: wordWrap ? "on" : "off",
      scrollBeyondLastLine: false,
      smoothScrolling: true,
      cursorBlinking: "smooth",
      cursorSmoothCaretAnimation: "on",
      renderLineHighlight: "line",
      padding: { top: 8, bottom: 8 },
      bracketPairColorization: { enabled: true },
      guides: { bracketPairs: true, indentation: true },
      automaticLayout: true,
      tabSize: 2,
      insertSpaces: true,
      scrollbar: {
        verticalScrollbarSize: 8,
        horizontalScrollbarSize: 8,
        useShadows: false
      },
      fixedOverflowWidgets: true,
      contextmenu: true,
      suggestOnTriggerCharacters: true,
      quickSuggestions: { other: true, comments: false, strings: false },
      formatOnPaste: false,
      formatOnType: false,
      "semanticHighlighting.enabled": true,
      ...options
    };
    const showWrapBtn = showWordWrapToggle && wordWrapProp === void 0;
    const showMinimapBtn = showMinimapToggle && minimapProp === void 0;
    return /* @__PURE__ */ jsxs2(
      "div",
      {
        ...rest,
        className: cn(
          "ods-monaco",
          `ods-monaco--${language}`,
          readOnly && "ods-monaco--readonly",
          className
        ),
        children: [
          !hideToolbar && /* @__PURE__ */ jsxs2("div", { className: "ods-monaco__toolbar", children: [
            /* @__PURE__ */ jsxs2("div", { className: "ods-monaco__toolbar-left", children: [
              /* @__PURE__ */ jsx2("span", { className: "ods-monaco__lang", children: language.toUpperCase() }),
              toolbar
            ] }),
            /* @__PURE__ */ jsxs2("div", { className: "ods-monaco__toolbar-right", children: [
              /* @__PURE__ */ jsxs2("span", { className: "ods-monaco__lines", children: [
                lineCount,
                " line",
                lineCount === 1 ? "" : "s"
              ] }),
              status && /* @__PURE__ */ jsx2("span", { className: "ods-monaco__status", children: status }),
              showWrapBtn && /* @__PURE__ */ jsx2(
                "button",
                {
                  type: "button",
                  className: cn(
                    "ods-monaco__btn",
                    wordWrap && "ods-monaco__btn--active"
                  ),
                  onClick: () => setWordWrap((w) => !w),
                  "aria-pressed": wordWrap,
                  "aria-label": "Toggle word wrap",
                  title: "Word wrap",
                  children: /* @__PURE__ */ jsx2(TextWrapIcon, {})
                }
              ),
              showMinimapBtn && /* @__PURE__ */ jsx2(
                "button",
                {
                  type: "button",
                  className: cn(
                    "ods-monaco__btn",
                    minimap && "ods-monaco__btn--active"
                  ),
                  onClick: () => setMinimap((m) => !m),
                  "aria-pressed": minimap,
                  "aria-label": "Toggle minimap",
                  title: "Minimap",
                  children: minimap ? /* @__PURE__ */ jsx2(MinimapOnIcon, {}) : /* @__PURE__ */ jsx2(MinimapOffIcon, {})
                }
              ),
              showFormat && !readOnly && /* @__PURE__ */ jsx2(
                "button",
                {
                  type: "button",
                  className: "ods-monaco__btn",
                  onClick: handleFormat,
                  "aria-label": "Format document",
                  title: "Format (Shift+Alt+F)",
                  children: /* @__PURE__ */ jsx2(MagicWandIcon, {})
                }
              ),
              showCopy && /* @__PURE__ */ jsx2(
                "button",
                {
                  type: "button",
                  className: cn(
                    "ods-monaco__btn",
                    copied && "ods-monaco__btn--success"
                  ),
                  onClick: handleCopy,
                  "aria-label": copied ? "Copied" : "Copy",
                  title: copied ? "Copied" : "Copy",
                  children: copied ? /* @__PURE__ */ jsx2(CheckmarkIcon, {}) : /* @__PURE__ */ jsx2(CopyIcon, {})
                }
              )
            ] })
          ] }),
          /* @__PURE__ */ jsx2("div", { className: "ods-monaco__body", style: { minHeight }, children: /* @__PURE__ */ jsx2(Suspense2, { fallback: /* @__PURE__ */ jsx2(LoadingPlaceholder2, { label: "Loading editor\u2026" }), children: /* @__PURE__ */ jsx2(
            Editor,
            {
              value,
              onChange: (v) => onChange?.(v ?? ""),
              language,
              height,
              theme: theme ?? OCTAVIAFLOW_THEME_ID,
              options: mergedOptions,
              onMount: handleMount,
              loading: /* @__PURE__ */ jsx2(LoadingPlaceholder2, { label: "Loading editor\u2026" }),
              "aria-label": ariaLabel
            }
          ) }) }),
          !hideFooter && /* @__PURE__ */ jsxs2("div", { className: "ods-monaco__footer", children: [
            /* @__PURE__ */ jsxs2("span", { className: "ods-monaco__pos", children: [
              "Ln ",
              cursor.line,
              ", Col ",
              cursor.column,
              selection.chars > 0 && /* @__PURE__ */ jsxs2(Fragment, { children: [
                " ",
                "\xB7 ",
                selection.chars,
                " char",
                selection.chars === 1 ? "" : "s",
                selection.lines > 1 && ` \xB7 ${selection.lines} lines`
              ] })
            ] }),
            /* @__PURE__ */ jsxs2("span", { className: "ods-monaco__shortcut-hint", children: [
              /* @__PURE__ */ jsx2("kbd", { className: "ods-monaco__kbd", children: "\u2318F" }),
              " find",
              /* @__PURE__ */ jsx2("span", { className: "ods-monaco__sep", children: "\xB7" }),
              /* @__PURE__ */ jsx2("kbd", { className: "ods-monaco__kbd", children: "\u2318\u21E7F" }),
              " replace",
              /* @__PURE__ */ jsx2("span", { className: "ods-monaco__sep", children: "\xB7" }),
              /* @__PURE__ */ jsx2("kbd", { className: "ods-monaco__kbd", children: "F1" }),
              " commands"
            ] })
          ] })
        ]
      }
    );
  }
);
MonacoEditor.displayName = "MonacoEditor";
export {
  MonacoDiffEditor,
  MonacoEditor,
  OCTAVIAFLOW_LIGHT,
  OCTAVIAFLOW_THEME_ID
};
//# sourceMappingURL=monaco.js.map