import { JsonViewer } from '@douyinfe/semi-json-viewer-core';
import BaseFoundation from '../base/foundation';
class JsonViewerFoundation extends BaseFoundation {
  constructor(adapter) {
    super(Object.assign(Object.assign({}, JsonViewerFoundation), adapter));
    this.jsonViewer = null;
  }
  init() {
    const props = this.getProps();
    const editorRef = this._adapter.getEditorRef();
    this.jsonViewer = new JsonViewer(editorRef, props.value, props.options);
    this.jsonViewer.emitter.on('customRender', e => {
      this._adapter.notifyCustomRender(e.customRenderMap);
    });
    this.jsonViewer.layout();
    this.jsonViewer.emitter.on('contentChanged', e => {
      var _a;
      this._adapter.notifyChange((_a = this.jsonViewer) === null || _a === void 0 ? void 0 : _a.getModel().getValue());
      if (this.getState('showSearchBar')) {
        this.search(this._adapter.getSearchRef().value);
      }
    });
  }
  search(searchText) {
    var _a;
    const state = this.getState('searchOptions');
    const {
      caseSensitive,
      wholeWord,
      regex
    } = state;
    (_a = this.jsonViewer) === null || _a === void 0 ? void 0 : _a.getSearchWidget().search(searchText, caseSensitive, wholeWord, regex);
  }
  prevSearch() {
    var _a;
    (_a = this.jsonViewer) === null || _a === void 0 ? void 0 : _a.getSearchWidget().navigateResults(-1);
  }
  nextSearch() {
    var _a;
    (_a = this.jsonViewer) === null || _a === void 0 ? void 0 : _a.getSearchWidget().navigateResults(1);
  }
  replace(replaceText) {
    var _a;
    if (this.getProps().options.readOnly) {
      return;
    }
    (_a = this.jsonViewer) === null || _a === void 0 ? void 0 : _a.getSearchWidget().replace(replaceText);
  }
  replaceAll(replaceText) {
    var _a;
    if (this.getProps().options.readOnly) {
      return;
    }
    (_a = this.jsonViewer) === null || _a === void 0 ? void 0 : _a.getSearchWidget().replaceAll(replaceText);
  }
  setSearchOptions(key) {
    this._adapter.setSearchOptions(key);
  }
  showSearchBar() {
    this._adapter.showSearchBar();
  }
}
export default JsonViewerFoundation;