"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const dom = require("../../../base/browser/dom.js");
const network = require("../../../base/common/network.js");
const abstractCodeEditorService = require("../../browser/services/abstractCodeEditorService.js");
const codeEditorService = require("../../browser/services/codeEditorService.js");
const contextkey = require("../../../platform/contextkey/common/contextkey.js");
const extensions = require("../../../platform/instantiation/common/extensions.js");
const themeService = require("../../../platform/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.StandaloneCodeEditorService = class StandaloneCodeEditorService extends abstractCodeEditorService.AbstractCodeEditorService {
  constructor(contextKeyService, themeService2) {
    super(themeService2);
    this.onCodeEditorAdd(() => this._checkContextKey());
    this.onCodeEditorRemove(() => this._checkContextKey());
    this._editorIsOpen = contextKeyService.createKey("editorIsOpen", false);
    this._activeCodeEditor = null;
  }
  _checkContextKey() {
    let hasCodeEditor = false;
    for (const editor of this.listCodeEditors()) {
      if (!editor.isSimpleWidget) {
        hasCodeEditor = true;
        break;
      }
    }
    this._editorIsOpen.set(hasCodeEditor);
  }
  setActiveCodeEditor(activeCodeEditor) {
    this._activeCodeEditor = activeCodeEditor;
  }
  getActiveCodeEditor() {
    return this._activeCodeEditor;
  }
  openCodeEditor(input, source, sideBySide) {
    if (!source) {
      return Promise.resolve(null);
    }
    return Promise.resolve(this.doOpenEditor(source, input));
  }
  doOpenEditor(editor, input) {
    const model = this.findModel(editor, input.resource);
    if (!model) {
      if (input.resource) {
        const schema = input.resource.scheme;
        if (schema === network.Schemas.http || schema === network.Schemas.https) {
          dom.windowOpenNoOpener(input.resource.toString());
          return editor;
        }
      }
      return null;
    }
    const selection = input.options ? input.options.selection : null;
    if (selection) {
      if (typeof selection.endLineNumber === "number" && typeof selection.endColumn === "number") {
        editor.setSelection(selection);
        editor.revealRangeInCenter(
          selection,
          1
          /* Immediate */
        );
      } else {
        const pos = {
          lineNumber: selection.startLineNumber,
          column: selection.startColumn
        };
        editor.setPosition(pos);
        editor.revealPositionInCenter(
          pos,
          1
          /* Immediate */
        );
      }
    }
    return editor;
  }
  findModel(editor, resource) {
    const model = editor.getModel();
    if (model && model.uri.toString() !== resource.toString()) {
      return null;
    }
    return model;
  }
};
exports.StandaloneCodeEditorService = __decorate([
  __param(0, contextkey.IContextKeyService),
  __param(1, themeService.IThemeService)
], exports.StandaloneCodeEditorService);
extensions.registerSingleton(codeEditorService.ICodeEditorService, exports.StandaloneCodeEditorService);
