import { enableSearch, SearchState } from './code-editor-search';
import 'codemirror/addon/scroll/annotatescrollbar';
export default function InjectSearch(CodeMirrorModule, getSearchQuery) {
  const {
    doSearch,
    clearSearch
  } = enableSearch(CodeMirrorModule);
  const setSearchQuery = cm => {
    cm.state.search = new SearchState();
    cm.state.search.lastQuery = getSearchQuery();
  };
  const find = (cm, rev) => {
    clearSearch(cm);
    setSearchQuery(cm);
    doSearch(cm, rev);
  };

  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  CodeMirrorModule.commands.findNext = cm => find(cm, false);
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  CodeMirrorModule.commands.findPrev = cm => find(cm, true);
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  CodeMirrorModule.commands.clearSearch = clearSearch;
}