"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const findInput = require("../../../base/browser/ui/findinput/findInput.js");
const replaceInput = require("../../../base/browser/ui/findinput/replaceInput.js");
const contextkey = require("../../contextkey/common/contextkey.js");
const keybindingsRegistry = require("../../keybinding/common/keybindingsRegistry.js");
const nls = require("../../../nls.js");
var __decorate = globalThis && globalThis.__decorate || function(decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
    r = Reflect.decorate(decorators, target, key, desc);
  else
    for (var i = decorators.length - 1; i >= 0; i--)
      if (d = decorators[i])
        r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = globalThis && globalThis.__param || function(paramIndex, decorator) {
  return function(target, key) {
    decorator(target, key, paramIndex);
  };
};
const historyNavigationVisible = new contextkey.RawContextKey("suggestWidgetVisible", false, nls.localize("suggestWidgetVisible", "Whether suggestion are visible"));
const HistoryNavigationWidgetContext = "historyNavigationWidget";
const HistoryNavigationForwardsEnablementContext = "historyNavigationForwardsEnabled";
const HistoryNavigationBackwardsEnablementContext = "historyNavigationBackwardsEnabled";
function bindContextScopedWidget(contextKeyService, widget, contextKey) {
  new contextkey.RawContextKey(contextKey, widget).bindTo(contextKeyService);
}
function createWidgetScopedContextKeyService(contextKeyService, widget) {
  return contextKeyService.createScoped(widget.target);
}
function getContextScopedWidget(contextKeyService, contextKey) {
  return contextKeyService.getContext(document.activeElement).getValue(contextKey);
}
function createAndBindHistoryNavigationWidgetScopedContextKeyService(contextKeyService, widget) {
  const scopedContextKeyService = createWidgetScopedContextKeyService(contextKeyService, widget);
  bindContextScopedWidget(scopedContextKeyService, widget, HistoryNavigationWidgetContext);
  const historyNavigationForwardsEnablement = new contextkey.RawContextKey(HistoryNavigationForwardsEnablementContext, true).bindTo(scopedContextKeyService);
  const historyNavigationBackwardsEnablement = new contextkey.RawContextKey(HistoryNavigationBackwardsEnablementContext, true).bindTo(scopedContextKeyService);
  return {
    scopedContextKeyService,
    historyNavigationForwardsEnablement,
    historyNavigationBackwardsEnablement
  };
}
exports.ContextScopedFindInput = class ContextScopedFindInput extends findInput.FindInput {
  constructor(container, contextViewProvider, options, contextKeyService, showFindOptions = false) {
    super(container, contextViewProvider, showFindOptions, options);
    this._register(createAndBindHistoryNavigationWidgetScopedContextKeyService(contextKeyService, { target: this.inputBox.element, historyNavigator: this.inputBox }).scopedContextKeyService);
  }
};
exports.ContextScopedFindInput = __decorate([
  __param(3, contextkey.IContextKeyService)
], exports.ContextScopedFindInput);
exports.ContextScopedReplaceInput = class ContextScopedReplaceInput extends replaceInput.ReplaceInput {
  constructor(container, contextViewProvider, options, contextKeyService, showReplaceOptions = false) {
    super(container, contextViewProvider, showReplaceOptions, options);
    this._register(createAndBindHistoryNavigationWidgetScopedContextKeyService(contextKeyService, { target: this.inputBox.element, historyNavigator: this.inputBox }).scopedContextKeyService);
  }
};
exports.ContextScopedReplaceInput = __decorate([
  __param(3, contextkey.IContextKeyService)
], exports.ContextScopedReplaceInput);
keybindingsRegistry.KeybindingsRegistry.registerCommandAndKeybindingRule({
  id: "history.showPrevious",
  weight: 200,
  when: contextkey.ContextKeyExpr.and(contextkey.ContextKeyExpr.has(HistoryNavigationWidgetContext), contextkey.ContextKeyExpr.equals(HistoryNavigationBackwardsEnablementContext, true), historyNavigationVisible.isEqualTo(false)),
  primary: 16,
  secondary: [
    512 | 16
    /* UpArrow */
  ],
  handler: (accessor) => {
    const widget = getContextScopedWidget(accessor.get(contextkey.IContextKeyService), HistoryNavigationWidgetContext);
    if (widget) {
      const historyInputBox = widget.historyNavigator;
      historyInputBox.showPreviousValue();
    }
  }
});
keybindingsRegistry.KeybindingsRegistry.registerCommandAndKeybindingRule({
  id: "history.showNext",
  weight: 200,
  when: contextkey.ContextKeyExpr.and(contextkey.ContextKeyExpr.has(HistoryNavigationWidgetContext), contextkey.ContextKeyExpr.equals(HistoryNavigationForwardsEnablementContext, true), historyNavigationVisible.isEqualTo(false)),
  primary: 18,
  secondary: [
    512 | 18
    /* DownArrow */
  ],
  handler: (accessor) => {
    const widget = getContextScopedWidget(accessor.get(contextkey.IContextKeyService), HistoryNavigationWidgetContext);
    if (widget) {
      const historyInputBox = widget.historyNavigator;
      historyInputBox.showNextValue();
    }
  }
});
exports.HistoryNavigationWidgetContext = HistoryNavigationWidgetContext;
exports.createAndBindHistoryNavigationWidgetScopedContextKeyService = createAndBindHistoryNavigationWidgetScopedContextKeyService;
exports.historyNavigationVisible = historyNavigationVisible;
