import "./standaloneQuickInput.css.mjs";
import { registerEditorContribution } from "../../../browser/editorExtensions.mjs";
import { IThemeService } from "../../../../platform/theme/common/themeService.mjs";
import { CancellationToken } from "../../../../base/common/cancellation.mjs";
import { IInstantiationService } from "../../../../platform/instantiation/common/instantiation.mjs";
import { IContextKeyService } from "../../../../platform/contextkey/common/contextkey.mjs";
import { IAccessibilityService } from "../../../../platform/accessibility/common/accessibility.mjs";
import { EditorScopedLayoutService } from "../standaloneLayoutService.mjs";
import { ICodeEditorService } from "../../../browser/services/codeEditorService.mjs";
import { QuickInputService } from "../../../../platform/quickinput/browser/quickInput.mjs";
import { once } from "../../../../base/common/functional.mjs";
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);
  };
};
let EditorScopedQuickInputService = class EditorScopedQuickInputService2 extends QuickInputService {
  constructor(editor, instantiationService, contextKeyService, themeService, accessibilityService, codeEditorService) {
    super(instantiationService, contextKeyService, themeService, accessibilityService, new EditorScopedLayoutService(editor.getContainerDomNode(), codeEditorService));
    this.host = void 0;
    const contribution = QuickInputEditorContribution.get(editor);
    if (contribution) {
      const widget = contribution.widget;
      this.host = {
        _serviceBrand: void 0,
        get hasContainer() {
          return true;
        },
        get container() {
          return widget.getDomNode();
        },
        get dimension() {
          return editor.getLayoutInfo();
        },
        get onDidLayout() {
          return editor.onDidLayoutChange;
        },
        focus: () => editor.focus()
      };
    } else {
      this.host = void 0;
    }
  }
  createController() {
    return super.createController(this.host);
  }
};
EditorScopedQuickInputService = __decorate([
  __param(1, IInstantiationService),
  __param(2, IContextKeyService),
  __param(3, IThemeService),
  __param(4, IAccessibilityService),
  __param(5, ICodeEditorService)
], EditorScopedQuickInputService);
let StandaloneQuickInputService = class StandaloneQuickInputService2 {
  constructor(instantiationService, codeEditorService) {
    this.instantiationService = instantiationService;
    this.codeEditorService = codeEditorService;
    this.mapEditorToService = /* @__PURE__ */ new Map();
  }
  get activeService() {
    const editor = this.codeEditorService.getFocusedCodeEditor();
    if (!editor) {
      throw new Error("Quick input service needs a focused editor to work.");
    }
    let quickInputService = this.mapEditorToService.get(editor);
    if (!quickInputService) {
      const newQuickInputService = quickInputService = this.instantiationService.createInstance(EditorScopedQuickInputService, editor);
      this.mapEditorToService.set(editor, quickInputService);
      once(editor.onDidDispose)(() => {
        newQuickInputService.dispose();
        this.mapEditorToService.delete(editor);
      });
    }
    return quickInputService;
  }
  get quickAccess() {
    return this.activeService.quickAccess;
  }
  pick(picks, options = {}, token = CancellationToken.None) {
    return this.activeService.pick(picks, options, token);
  }
  createQuickPick() {
    return this.activeService.createQuickPick();
  }
};
StandaloneQuickInputService = __decorate([
  __param(0, IInstantiationService),
  __param(1, ICodeEditorService)
], StandaloneQuickInputService);
class QuickInputEditorContribution {
  constructor(editor) {
    this.editor = editor;
    this.widget = new QuickInputEditorWidget(this.editor);
  }
  static get(editor) {
    return editor.getContribution(QuickInputEditorContribution.ID);
  }
  dispose() {
    this.widget.dispose();
  }
}
QuickInputEditorContribution.ID = "editor.controller.quickInput";
class QuickInputEditorWidget {
  constructor(codeEditor) {
    this.codeEditor = codeEditor;
    this.domNode = document.createElement("div");
    this.codeEditor.addOverlayWidget(this);
  }
  getId() {
    return QuickInputEditorWidget.ID;
  }
  getDomNode() {
    return this.domNode;
  }
  getPosition() {
    return {
      preference: 2
      /* TOP_CENTER */
    };
  }
  dispose() {
    this.codeEditor.removeOverlayWidget(this);
  }
}
QuickInputEditorWidget.ID = "editor.contrib.quickInputWidget";
registerEditorContribution(QuickInputEditorContribution.ID, QuickInputEditorContribution);
export {
  EditorScopedQuickInputService,
  QuickInputEditorContribution,
  QuickInputEditorWidget,
  StandaloneQuickInputService
};
