"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const cancellation = require("../../../base/common/cancellation.js");
const quickInput = require("../../../base/parts/quickinput/browser/quickInput.js");
const accessibility = require("../../accessibility/common/accessibility.js");
const contextkey = require("../../contextkey/common/contextkey.js");
const instantiation = require("../../instantiation/common/instantiation.js");
const layoutService = require("../../layout/browser/layoutService.js");
const listService = require("../../list/browser/listService.js");
const quickAccess = require("./quickAccess.js");
const colorRegistry = require("../../theme/common/colorRegistry.js");
const styler = require("../../theme/common/styler.js");
const themeService = require("../../theme/common/themeService.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);
  };
};
exports.QuickInputService = class QuickInputService extends themeService.Themable {
  constructor(instantiationService, contextKeyService, themeService2, accessibilityService, layoutService2) {
    super(themeService2);
    this.instantiationService = instantiationService;
    this.contextKeyService = contextKeyService;
    this.accessibilityService = accessibilityService;
    this.layoutService = layoutService2;
    this.contexts = /* @__PURE__ */ new Map();
  }
  get controller() {
    if (!this._controller) {
      this._controller = this._register(this.createController());
    }
    return this._controller;
  }
  get quickAccess() {
    if (!this._quickAccess) {
      this._quickAccess = this._register(this.instantiationService.createInstance(quickAccess.QuickAccessController));
    }
    return this._quickAccess;
  }
  createController(host = this.layoutService, options) {
    var _a, _b;
    const defaultOptions = {
      idPrefix: "quickInput_",
      container: host.container,
      ignoreFocusOut: () => false,
      isScreenReaderOptimized: () => this.accessibilityService.isScreenReaderOptimized(),
      backKeybindingLabel: () => void 0,
      setContextKey: (id) => this.setContextKey(id),
      returnFocus: () => host.focus(),
      createList: (user, container, delegate, renderers, options2) => this.instantiationService.createInstance(listService.WorkbenchList, user, container, delegate, renderers, options2),
      styles: this.computeStyles()
    };
    const controller = this._register(new quickInput.QuickInputController(Object.assign(Object.assign({}, defaultOptions), options)));
    controller.layout(host.dimension, (_b = (_a = host.offset) === null || _a === void 0 ? void 0 : _a.top) !== null && _b !== void 0 ? _b : 0);
    this._register(host.onDidLayout((dimension) => {
      var _a2, _b2;
      return controller.layout(dimension, (_b2 = (_a2 = host.offset) === null || _a2 === void 0 ? void 0 : _a2.top) !== null && _b2 !== void 0 ? _b2 : 0);
    }));
    this._register(controller.onShow(() => this.resetContextKeys()));
    this._register(controller.onHide(() => this.resetContextKeys()));
    return controller;
  }
  setContextKey(id) {
    let key;
    if (id) {
      key = this.contexts.get(id);
      if (!key) {
        key = new contextkey.RawContextKey(id, false).bindTo(this.contextKeyService);
        this.contexts.set(id, key);
      }
    }
    if (key && key.get()) {
      return;
    }
    this.resetContextKeys();
    if (key) {
      key.set(true);
    }
  }
  resetContextKeys() {
    this.contexts.forEach((context) => {
      if (context.get()) {
        context.reset();
      }
    });
  }
  pick(picks, options = {}, token = cancellation.CancellationToken.None) {
    return this.controller.pick(picks, options, token);
  }
  createQuickPick() {
    return this.controller.createQuickPick();
  }
  updateStyles() {
    this.controller.applyStyles(this.computeStyles());
  }
  computeStyles() {
    return {
      widget: Object.assign({}, styler.computeStyles(this.theme, {
        quickInputBackground: colorRegistry.quickInputBackground,
        quickInputForeground: colorRegistry.quickInputForeground,
        quickInputTitleBackground: colorRegistry.quickInputTitleBackground,
        contrastBorder: colorRegistry.contrastBorder,
        widgetShadow: colorRegistry.widgetShadow
      })),
      inputBox: styler.computeStyles(this.theme, {
        inputForeground: colorRegistry.inputForeground,
        inputBackground: colorRegistry.inputBackground,
        inputBorder: colorRegistry.inputBorder,
        inputValidationInfoBackground: colorRegistry.inputValidationInfoBackground,
        inputValidationInfoForeground: colorRegistry.inputValidationInfoForeground,
        inputValidationInfoBorder: colorRegistry.inputValidationInfoBorder,
        inputValidationWarningBackground: colorRegistry.inputValidationWarningBackground,
        inputValidationWarningForeground: colorRegistry.inputValidationWarningForeground,
        inputValidationWarningBorder: colorRegistry.inputValidationWarningBorder,
        inputValidationErrorBackground: colorRegistry.inputValidationErrorBackground,
        inputValidationErrorForeground: colorRegistry.inputValidationErrorForeground,
        inputValidationErrorBorder: colorRegistry.inputValidationErrorBorder
      }),
      countBadge: styler.computeStyles(this.theme, {
        badgeBackground: colorRegistry.badgeBackground,
        badgeForeground: colorRegistry.badgeForeground,
        badgeBorder: colorRegistry.contrastBorder
      }),
      button: styler.computeStyles(this.theme, {
        buttonForeground: colorRegistry.buttonForeground,
        buttonBackground: colorRegistry.buttonBackground,
        buttonHoverBackground: colorRegistry.buttonHoverBackground,
        buttonBorder: colorRegistry.contrastBorder
      }),
      progressBar: styler.computeStyles(this.theme, {
        progressBarBackground: colorRegistry.progressBarBackground
      }),
      keybindingLabel: styler.computeStyles(this.theme, {
        keybindingLabelBackground: colorRegistry.keybindingLabelBackground,
        keybindingLabelForeground: colorRegistry.keybindingLabelForeground,
        keybindingLabelBorder: colorRegistry.keybindingLabelBorder,
        keybindingLabelBottomBorder: colorRegistry.keybindingLabelBottomBorder,
        keybindingLabelShadow: colorRegistry.widgetShadow
      }),
      list: styler.computeStyles(this.theme, {
        listBackground: colorRegistry.quickInputBackground,
        // Look like focused when inactive.
        listInactiveFocusForeground: colorRegistry.quickInputListFocusForeground,
        listInactiveSelectionIconForeground: colorRegistry.quickInputListFocusIconForeground,
        listInactiveFocusBackground: colorRegistry.quickInputListFocusBackground,
        listFocusOutline: colorRegistry.activeContrastBorder,
        listInactiveFocusOutline: colorRegistry.activeContrastBorder,
        pickerGroupBorder: colorRegistry.pickerGroupBorder,
        pickerGroupForeground: colorRegistry.pickerGroupForeground
      })
    };
  }
};
exports.QuickInputService = __decorate([
  __param(0, instantiation.IInstantiationService),
  __param(1, contextkey.IContextKeyService),
  __param(2, themeService.IThemeService),
  __param(3, accessibility.IAccessibilityService),
  __param(4, layoutService.ILayoutService)
], exports.QuickInputService);
