"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const functional = require("../../../../base/common/functional.js");
const lifecycle = require("../../../../base/common/lifecycle.js");
const types = require("../../../../base/common/types.js");
const editorBrowser = require("../../../browser/editorBrowser.js");
const model = require("../../../common/model.js");
const editorColorRegistry = require("../../../common/core/editorColorRegistry.js");
const themeService = require("../../../../platform/theme/common/themeService.js");
class AbstractEditorNavigationQuickAccessProvider {
  constructor(options) {
    this.options = options;
    this.rangeHighlightDecorationId = void 0;
  }
  //#region Provider methods
  provide(picker, token) {
    var _a;
    const disposables = new lifecycle.DisposableStore();
    picker.canAcceptInBackground = !!((_a = this.options) === null || _a === void 0 ? void 0 : _a.canAcceptInBackground);
    picker.matchOnLabel = picker.matchOnDescription = picker.matchOnDetail = picker.sortByLabel = false;
    const pickerDisposable = disposables.add(new lifecycle.MutableDisposable());
    pickerDisposable.value = this.doProvide(picker, token);
    disposables.add(this.onDidActiveTextEditorControlChange(() => {
      pickerDisposable.value = void 0;
      pickerDisposable.value = this.doProvide(picker, token);
    }));
    return disposables;
  }
  doProvide(picker, token) {
    const disposables = new lifecycle.DisposableStore();
    const editor = this.activeTextEditorControl;
    if (editor && this.canProvideWithTextEditor(editor)) {
      const context = { editor };
      const codeEditor = editorBrowser.getCodeEditor(editor);
      if (codeEditor) {
        let lastKnownEditorViewState = types.withNullAsUndefined(editor.saveViewState());
        disposables.add(codeEditor.onDidChangeCursorPosition(() => {
          lastKnownEditorViewState = types.withNullAsUndefined(editor.saveViewState());
        }));
        context.restoreViewState = () => {
          if (lastKnownEditorViewState && editor === this.activeTextEditorControl) {
            editor.restoreViewState(lastKnownEditorViewState);
          }
        };
        disposables.add(functional.once(token.onCancellationRequested)(() => {
          var _a;
          return (_a = context.restoreViewState) === null || _a === void 0 ? void 0 : _a.call(context);
        }));
      }
      disposables.add(lifecycle.toDisposable(() => this.clearDecorations(editor)));
      disposables.add(this.provideWithTextEditor(context, picker, token));
    } else {
      disposables.add(this.provideWithoutTextEditor(picker, token));
    }
    return disposables;
  }
  /**
   * Subclasses to implement if they can operate on the text editor.
   */
  canProvideWithTextEditor(editor) {
    return true;
  }
  gotoLocation({ editor }, options) {
    editor.setSelection(options.range);
    editor.revealRangeInCenter(
      options.range,
      0
      /* Smooth */
    );
    if (!options.preserveFocus) {
      editor.focus();
    }
  }
  getModel(editor) {
    var _a;
    return editorBrowser.isDiffEditor(editor) ? (_a = editor.getModel()) === null || _a === void 0 ? void 0 : _a.modified : editor.getModel();
  }
  addDecorations(editor, range) {
    editor.changeDecorations((changeAccessor) => {
      const deleteDecorations = [];
      if (this.rangeHighlightDecorationId) {
        deleteDecorations.push(this.rangeHighlightDecorationId.overviewRulerDecorationId);
        deleteDecorations.push(this.rangeHighlightDecorationId.rangeHighlightId);
        this.rangeHighlightDecorationId = void 0;
      }
      const newDecorations = [
        // highlight the entire line on the range
        {
          range,
          options: {
            description: "quick-access-range-highlight",
            className: "rangeHighlight",
            isWholeLine: true
          }
        },
        // also add overview ruler highlight
        {
          range,
          options: {
            description: "quick-access-range-highlight-overview",
            overviewRuler: {
              color: themeService.themeColorFromId(editorColorRegistry.overviewRulerRangeHighlight),
              position: model.OverviewRulerLane.Full
            }
          }
        }
      ];
      const [rangeHighlightId, overviewRulerDecorationId] = changeAccessor.deltaDecorations(deleteDecorations, newDecorations);
      this.rangeHighlightDecorationId = { rangeHighlightId, overviewRulerDecorationId };
    });
  }
  clearDecorations(editor) {
    const rangeHighlightDecorationId = this.rangeHighlightDecorationId;
    if (rangeHighlightDecorationId) {
      editor.changeDecorations((changeAccessor) => {
        changeAccessor.deltaDecorations([
          rangeHighlightDecorationId.overviewRulerDecorationId,
          rangeHighlightDecorationId.rangeHighlightId
        ], []);
      });
      this.rangeHighlightDecorationId = void 0;
    }
  }
}
exports.AbstractEditorNavigationQuickAccessProvider = AbstractEditorNavigationQuickAccessProvider;
