// ==========================================
// Chat Switchboard — CM6 Bundle Entrypoint
// ==========================================
// esbuild compiles this into an IIFE that sets
// window.CM with factory functions and utilities.
//
// Consumers:
//   if (window.CM) {
//       const editor = CM.chatInput(el, opts);
//       const code = CM.codeEditor(el, opts);
//   }
// ==========================================

import { EditorView } from '@codemirror/view';
import { EditorState } from '@codemirror/state';

import { javascript } from '@codemirror/lang-javascript';
import { json } from '@codemirror/lang-json';
import { markdown } from '@codemirror/lang-markdown';
import { sql } from '@codemirror/lang-sql';
import { html } from '@codemirror/lang-html';
import { css } from '@codemirror/lang-css';
import { yaml } from '@codemirror/lang-yaml';
import { go } from '@codemirror/lang-go';
import { python } from '@codemirror/lang-python';
import { rust } from '@codemirror/lang-rust';

import { vim } from '@replit/codemirror-vim';
import { emacs } from '@replit/codemirror-emacs';

import { codeEditor } from './code-editor.mjs';
import { chatInput } from './chat-input.mjs';
import { noteEditor } from './note-editor.mjs';
import { mentionExtension } from './mention.mjs';

// ── Expose on window ────────────────────────

window.CM = {
    // Version (injected by the build script or matched to app version)
    version: '0.17.3',

    // Low-level access (for advanced use / debug console)
    EditorView,
    EditorState,

    // Factory: full-featured code editor
    //   CM.codeEditor(container, { language, value, lineNumbers, darkMode, keymap, onChange })
    codeEditor,

    // Factory: chat message input (markdown mode, minimal chrome)
    //   CM.chatInput(container, { placeholder, onSubmit, onChange, maxHeight, darkMode })
    chatInput,

    // Factory: rich markdown note editor with wikilinks
    //   CM.noteEditor(container, { value, darkMode, onChange, onLink, linkCompleter })
    noteEditor,

    // Extension: @mention autocomplete + decoration for chat input
    //   CM.mentionExtension({ completer: async (query) => [{ label, handle, type }] })
    mentionExtension,

    // Bundled language modes (for reference / dynamic use)
    languages: {
        javascript, json, markdown, sql, html, css, yaml, go, python, rust,
    },

    // Keybinding modes (for reference / settings UI)
    keybindings: { vim, emacs },
};

console.log('[CM6] CodeMirror bundle loaded (v' + window.CM.version + ')');
