import { EditorState } from "../_vendor/@codemirror/state/dist-index.js";
import { lineNumbers, highlightActiveLineGutter, highlightActiveLine, EditorView, drawSelection, dropCursor, keymap } from "../_vendor/@codemirror/view/dist-index.js";
import { useMemo } from "react";
import { history } from "../_vendor/@codemirror/commands/dist-index.js";
import { closeBrackets, closeBracketsKeymap } from "../_vendor/@codemirror/autocomplete/dist-index.js";
import { highlightSelectionMatches } from "../_vendor/@codemirror/search/dist-index.js";
import { foldGutter, indentOnInput, bracketMatching } from "../_vendor/@codemirror/language/dist-index.js";
function useBaseExtensions(options = {}) {
  const {
    readonly = false,
    lineWrapping = true,
    onChange,
    onCursorChange,
    onSelectionChange,
    onEditorUpdate,
    theme,
    includeLineNumbers = true,
    includeKeymap = true
  } = options;
  const extensions = useMemo(() => {
    const baseExtensions = [];
    if (includeLineNumbers) {
      baseExtensions.push(lineNumbers());
    }
    baseExtensions.push(
      highlightActiveLineGutter(),
      highlightActiveLine()
    );
    if (lineWrapping) {
      baseExtensions.push(EditorView.lineWrapping);
    }
    if (theme) {
      baseExtensions.push(theme);
    }
    if (readonly) {
      baseExtensions.push(
        EditorState.readOnly.of(true),
        EditorView.editable.of(false)
      );
    } else {
      baseExtensions.push(
        history(),
        foldGutter({
          markerDOM: (open) => {
            const svgNS = "http://www.w3.org/2000/svg";
            const wrapper = document.createElement("div");
            wrapper.classList.add("cm-fold-marker");
            wrapper.style.cursor = "default";
            const svgElement = document.createElementNS(svgNS, "svg");
            svgElement.setAttribute("viewBox", "0 0 10 10");
            svgElement.setAttribute("width", "6");
            svgElement.setAttribute("height", "6");
            const pathElement = document.createElementNS(svgNS, "path");
            const d = open ? "M1 3 L5 7 L9 3" : "M3 1 L7 5 L3 9";
            pathElement.setAttribute("d", d);
            pathElement.setAttribute("fill", "none");
            pathElement.setAttribute("stroke", "currentColor");
            pathElement.setAttribute("stroke-width", "1.5");
            pathElement.setAttribute("stroke-linecap", "round");
            svgElement.appendChild(pathElement);
            wrapper.appendChild(svgElement);
            return wrapper;
          }
        }),
        indentOnInput(),
        bracketMatching(),
        closeBrackets(),
        highlightSelectionMatches({ minSelectionLength: 2 }),
        drawSelection(),
        dropCursor(),
        EditorState.allowMultipleSelections.of(true),
        EditorView.clickAddsSelectionRange.of(
          (event) => event.altKey && !event.metaKey && !event.shiftKey
        )
      );
      if (includeKeymap) {
        baseExtensions.push(
          keymap.of(closeBracketsKeymap)
        );
      }
      if (onChange || onCursorChange || onSelectionChange || onEditorUpdate) {
        baseExtensions.push(
          EditorView.updateListener.of((update) => {
            onEditorUpdate == null ? void 0 : onEditorUpdate(update);
            if (onChange && update.docChanged) {
              const newValue = update.state.doc.toString();
              onChange(newValue, update);
            }
            if (onCursorChange && update.selectionSet) {
              const { state } = update;
              const pos = state.selection.main.head;
              const lineInfo = state.doc.lineAt(pos);
              onCursorChange({
                column: pos - lineInfo.from + 1,
                line: lineInfo.number
              });
            }
            if (onSelectionChange && update.selectionSet) {
              const { state } = update;
              const { from, to } = state.selection.main;
              const characterCount = to - from;
              onSelectionChange(characterCount);
            }
          })
        );
      }
    }
    return baseExtensions;
  }, [
    readonly,
    lineWrapping,
    onChange,
    onCursorChange,
    onSelectionChange,
    onEditorUpdate,
    theme,
    includeLineNumbers,
    includeKeymap
  ]);
  return extensions;
}
export {
  useBaseExtensions
};
//# sourceMappingURL=use-base-extensions.js.map
