import { UndoManager } from '../../../../yjs@13.6.26/node_modules/yjs/dist/yjs.js';
import { EditorView } from '../../../../@codemirror_view@6.28.6/node_modules/@codemirror/view/dist/index.js';
import '../../../../@codemirror_state@6.5.2/node_modules/@codemirror/state/dist/index.js';
export { YRange } from './y-range.js';
import { YSyncConfig, ySyncFacet, ySync } from './y-sync.js';
import { yRemoteSelectionsTheme, yRemoteSelections } from './y-remote-selections.js';
import { yUndoManagerFacet, YUndoManagerConfig, yUndoManager, undo, redo } from './y-undomanager.js';
export { yUndoManagerKeymap } from './y-undomanager.js';

/**
 * @param {Y.Text} ytext
 * @param {any} awareness
 * @param {Object} [opts]
 * @param {Y.UndoManager | false} [opts.undoManager] Set undoManager to false to disable the undo-redo plugin
 * @return {cmState.Extension}
 */
const yCollab = (ytext, awareness, { undoManager = new UndoManager(ytext) } = {}) => {
  const ySyncConfig = new YSyncConfig(ytext, awareness);
  const plugins = [
    ySyncFacet.of(ySyncConfig),
    ySync
  ];
  if (awareness) {
    plugins.push(
      yRemoteSelectionsTheme,
      yRemoteSelections
    );
  }
  if (undoManager !== false) {
    // By default, only track changes that are produced by the sync plugin (local edits)
    plugins.push(
      yUndoManagerFacet.of(new YUndoManagerConfig(undoManager)),
      yUndoManager,
      EditorView.domEventHandlers({
        beforeinput (e, view) {
          if (e.inputType === 'historyUndo') return undo(view)
          if (e.inputType === 'historyRedo') return redo(view)
          return false
        }
      })
    );
  }
  return plugins
};

export { YSyncConfig, yCollab, yRemoteSelections, yRemoteSelectionsTheme, ySync, ySyncFacet };
