"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const aria = require("../../../base/browser/ui/aria/aria.js");
const lifecycle = require("../../../base/common/lifecycle.js");
const codeEditorService = require("../../browser/services/codeEditorService.js");
const codeEditorWidget = require("../../browser/widget/codeEditorWidget.js");
const diffEditorWidget = require("../../browser/widget/diffEditorWidget.js");
const editorAction = require("../../common/editorAction.js");
const editorWorker = require("../../common/services/editorWorker.js");
const standaloneServices = require("./standaloneServices.js");
const standaloneTheme = require("../common/standaloneTheme.js");
const actions = require("../../../platform/actions/common/actions.js");
const commands = require("../../../platform/commands/common/commands.js");
const configuration = require("../../../platform/configuration/common/configuration.js");
const contextkey = require("../../../platform/contextkey/common/contextkey.js");
const contextView = require("../../../platform/contextview/browser/contextView.js");
const instantiation = require("../../../platform/instantiation/common/instantiation.js");
const keybinding = require("../../../platform/keybinding/common/keybinding.js");
const notification = require("../../../platform/notification/common/notification.js");
const themeService = require("../../../platform/theme/common/themeService.js");
const accessibility = require("../../../platform/accessibility/common/accessibility.js");
const standaloneStrings = require("../../common/standaloneStrings.js");
const clipboardService = require("../../../platform/clipboard/common/clipboardService.js");
const progress = require("../../../platform/progress/common/progress.js");
const model = require("../../common/services/model.js");
const language = require("../../common/languages/language.js");
const standaloneCodeEditorService = require("./standaloneCodeEditorService.js");
const modesRegistry = require("../../common/languages/modesRegistry.js");
const languageConfigurationRegistry = require("../../common/languages/languageConfigurationRegistry.js");
const languageFeatures = require("../../common/services/languageFeatures.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);
  };
};
let LAST_GENERATED_COMMAND_ID = 0;
let ariaDomNodeCreated = false;
function createAriaDomNode(parent) {
  if (!parent) {
    if (ariaDomNodeCreated) {
      return;
    }
    ariaDomNodeCreated = true;
  }
  aria.setARIAContainer(parent || document.body);
}
exports.StandaloneCodeEditor = class StandaloneCodeEditor extends codeEditorWidget.CodeEditorWidget {
  constructor(domElement, _options, instantiationService, codeEditorService2, commandService, contextKeyService, keybindingService, themeService2, notificationService, accessibilityService, languageConfigurationService, languageFeaturesService) {
    const options = Object.assign({}, _options);
    options.ariaLabel = options.ariaLabel || standaloneStrings.StandaloneCodeEditorNLS.editorViewAccessibleLabel;
    options.ariaLabel = options.ariaLabel + ";" + standaloneStrings.StandaloneCodeEditorNLS.accessibilityHelpMessage;
    super(domElement, options, {}, instantiationService, codeEditorService2, commandService, contextKeyService, themeService2, notificationService, accessibilityService, languageConfigurationService, languageFeaturesService);
    if (keybindingService instanceof standaloneServices.StandaloneKeybindingService) {
      this._standaloneKeybindingService = keybindingService;
    } else {
      this._standaloneKeybindingService = null;
    }
    createAriaDomNode(options.ariaContainerElement);
  }
  addCommand(keybinding2, handler, context) {
    if (!this._standaloneKeybindingService) {
      console.warn("Cannot add command because the editor is configured with an unrecognized KeybindingService");
      return null;
    }
    const commandId = "DYNAMIC_" + ++LAST_GENERATED_COMMAND_ID;
    const whenExpression = contextkey.ContextKeyExpr.deserialize(context);
    this._standaloneKeybindingService.addDynamicKeybinding(commandId, keybinding2, handler, whenExpression);
    return commandId;
  }
  createContextKey(key, defaultValue) {
    return this._contextKeyService.createKey(key, defaultValue);
  }
  addAction(_descriptor) {
    if (typeof _descriptor.id !== "string" || typeof _descriptor.label !== "string" || typeof _descriptor.run !== "function") {
      throw new Error("Invalid action descriptor, `id`, `label` and `run` are required properties!");
    }
    if (!this._standaloneKeybindingService) {
      console.warn("Cannot add keybinding because the editor is configured with an unrecognized KeybindingService");
      return lifecycle.Disposable.None;
    }
    const id = _descriptor.id;
    const label = _descriptor.label;
    const precondition = contextkey.ContextKeyExpr.and(contextkey.ContextKeyExpr.equals("editorId", this.getId()), contextkey.ContextKeyExpr.deserialize(_descriptor.precondition));
    const keybindings = _descriptor.keybindings;
    const keybindingsWhen = contextkey.ContextKeyExpr.and(precondition, contextkey.ContextKeyExpr.deserialize(_descriptor.keybindingContext));
    const contextMenuGroupId = _descriptor.contextMenuGroupId || null;
    const contextMenuOrder = _descriptor.contextMenuOrder || 0;
    const run = (accessor, ...args) => {
      return Promise.resolve(_descriptor.run(this, ...args));
    };
    const toDispose = new lifecycle.DisposableStore();
    const uniqueId = this.getId() + ":" + id;
    toDispose.add(commands.CommandsRegistry.registerCommand(uniqueId, run));
    if (contextMenuGroupId) {
      const menuItem = {
        command: {
          id: uniqueId,
          title: label
        },
        when: precondition,
        group: contextMenuGroupId,
        order: contextMenuOrder
      };
      toDispose.add(actions.MenuRegistry.appendMenuItem(actions.MenuId.EditorContext, menuItem));
    }
    if (Array.isArray(keybindings)) {
      for (const kb of keybindings) {
        toDispose.add(this._standaloneKeybindingService.addDynamicKeybinding(uniqueId, kb, run, keybindingsWhen));
      }
    }
    const internalAction = new editorAction.InternalEditorAction(uniqueId, label, label, precondition, run, this._contextKeyService);
    this._actions[id] = internalAction;
    toDispose.add(lifecycle.toDisposable(() => {
      delete this._actions[id];
    }));
    return toDispose;
  }
  _triggerCommand(handlerId, payload) {
    if (this._codeEditorService instanceof standaloneCodeEditorService.StandaloneCodeEditorService) {
      try {
        this._codeEditorService.setActiveCodeEditor(this);
        super._triggerCommand(handlerId, payload);
      } finally {
        this._codeEditorService.setActiveCodeEditor(null);
      }
    } else {
      super._triggerCommand(handlerId, payload);
    }
  }
};
exports.StandaloneCodeEditor = __decorate([
  __param(2, instantiation.IInstantiationService),
  __param(3, codeEditorService.ICodeEditorService),
  __param(4, commands.ICommandService),
  __param(5, contextkey.IContextKeyService),
  __param(6, keybinding.IKeybindingService),
  __param(7, themeService.IThemeService),
  __param(8, notification.INotificationService),
  __param(9, accessibility.IAccessibilityService),
  __param(10, languageConfigurationRegistry.ILanguageConfigurationService),
  __param(11, languageFeatures.ILanguageFeaturesService)
], exports.StandaloneCodeEditor);
exports.StandaloneEditor = class StandaloneEditor extends exports.StandaloneCodeEditor {
  constructor(domElement, _options, instantiationService, codeEditorService2, commandService, contextKeyService, keybindingService, themeService2, notificationService, configurationService, accessibilityService, modelService, languageService, languageConfigurationService, languageFeaturesService) {
    const options = Object.assign({}, _options);
    standaloneServices.updateConfigurationService(configurationService, options, false);
    const themeDomRegistration = themeService2.registerEditorContainer(domElement);
    if (typeof options.theme === "string") {
      themeService2.setTheme(options.theme);
    }
    if (typeof options.autoDetectHighContrast !== "undefined") {
      themeService2.setAutoDetectHighContrast(Boolean(options.autoDetectHighContrast));
    }
    const _model = options.model;
    delete options.model;
    super(domElement, options, instantiationService, codeEditorService2, commandService, contextKeyService, keybindingService, themeService2, notificationService, accessibilityService, languageConfigurationService, languageFeaturesService);
    this._configurationService = configurationService;
    this._standaloneThemeService = themeService2;
    this._register(themeDomRegistration);
    let model2;
    if (typeof _model === "undefined") {
      const languageId = languageService.getLanguageIdByMimeType(options.language) || options.language || modesRegistry.PLAINTEXT_LANGUAGE_ID;
      model2 = createTextModel(modelService, languageService, options.value || "", languageId, void 0);
      this._ownsModel = true;
    } else {
      model2 = _model;
      this._ownsModel = false;
    }
    this._attachModel(model2);
    if (model2) {
      const e = {
        oldModelUrl: null,
        newModelUrl: model2.uri
      };
      this._onDidChangeModel.fire(e);
    }
  }
  dispose() {
    super.dispose();
  }
  updateOptions(newOptions) {
    standaloneServices.updateConfigurationService(this._configurationService, newOptions, false);
    if (typeof newOptions.theme === "string") {
      this._standaloneThemeService.setTheme(newOptions.theme);
    }
    if (typeof newOptions.autoDetectHighContrast !== "undefined") {
      this._standaloneThemeService.setAutoDetectHighContrast(Boolean(newOptions.autoDetectHighContrast));
    }
    super.updateOptions(newOptions);
  }
  _postDetachModelCleanup(detachedModel) {
    super._postDetachModelCleanup(detachedModel);
    if (detachedModel && this._ownsModel) {
      detachedModel.dispose();
      this._ownsModel = false;
    }
  }
};
exports.StandaloneEditor = __decorate([
  __param(2, instantiation.IInstantiationService),
  __param(3, codeEditorService.ICodeEditorService),
  __param(4, commands.ICommandService),
  __param(5, contextkey.IContextKeyService),
  __param(6, keybinding.IKeybindingService),
  __param(7, standaloneTheme.IStandaloneThemeService),
  __param(8, notification.INotificationService),
  __param(9, configuration.IConfigurationService),
  __param(10, accessibility.IAccessibilityService),
  __param(11, model.IModelService),
  __param(12, language.ILanguageService),
  __param(13, languageConfigurationRegistry.ILanguageConfigurationService),
  __param(14, languageFeatures.ILanguageFeaturesService)
], exports.StandaloneEditor);
exports.StandaloneDiffEditor = class StandaloneDiffEditor extends diffEditorWidget.DiffEditorWidget {
  constructor(domElement, _options, instantiationService, contextKeyService, editorWorkerService, codeEditorService2, themeService2, notificationService, configurationService, contextMenuService, editorProgressService, clipboardService2) {
    const options = Object.assign({}, _options);
    standaloneServices.updateConfigurationService(configurationService, options, true);
    const themeDomRegistration = themeService2.registerEditorContainer(domElement);
    if (typeof options.theme === "string") {
      themeService2.setTheme(options.theme);
    }
    if (typeof options.autoDetectHighContrast !== "undefined") {
      themeService2.setAutoDetectHighContrast(Boolean(options.autoDetectHighContrast));
    }
    super(domElement, options, {}, clipboardService2, editorWorkerService, contextKeyService, instantiationService, codeEditorService2, themeService2, notificationService, contextMenuService, editorProgressService);
    this._configurationService = configurationService;
    this._standaloneThemeService = themeService2;
    this._register(themeDomRegistration);
  }
  dispose() {
    super.dispose();
  }
  updateOptions(newOptions) {
    standaloneServices.updateConfigurationService(this._configurationService, newOptions, true);
    if (typeof newOptions.theme === "string") {
      this._standaloneThemeService.setTheme(newOptions.theme);
    }
    if (typeof newOptions.autoDetectHighContrast !== "undefined") {
      this._standaloneThemeService.setAutoDetectHighContrast(Boolean(newOptions.autoDetectHighContrast));
    }
    super.updateOptions(newOptions);
  }
  _createInnerEditor(instantiationService, container, options) {
    return instantiationService.createInstance(exports.StandaloneCodeEditor, container, options);
  }
  getOriginalEditor() {
    return super.getOriginalEditor();
  }
  getModifiedEditor() {
    return super.getModifiedEditor();
  }
  addCommand(keybinding2, handler, context) {
    return this.getModifiedEditor().addCommand(keybinding2, handler, context);
  }
  createContextKey(key, defaultValue) {
    return this.getModifiedEditor().createContextKey(key, defaultValue);
  }
  addAction(descriptor) {
    return this.getModifiedEditor().addAction(descriptor);
  }
};
exports.StandaloneDiffEditor = __decorate([
  __param(2, instantiation.IInstantiationService),
  __param(3, contextkey.IContextKeyService),
  __param(4, editorWorker.IEditorWorkerService),
  __param(5, codeEditorService.ICodeEditorService),
  __param(6, standaloneTheme.IStandaloneThemeService),
  __param(7, notification.INotificationService),
  __param(8, configuration.IConfigurationService),
  __param(9, contextView.IContextMenuService),
  __param(10, progress.IEditorProgressService),
  __param(11, clipboardService.IClipboardService)
], exports.StandaloneDiffEditor);
function createTextModel(modelService, languageService, value, languageId, uri) {
  value = value || "";
  if (!languageId) {
    const firstLF = value.indexOf("\n");
    let firstLine = value;
    if (firstLF !== -1) {
      firstLine = value.substring(0, firstLF);
    }
    return doCreateModel(modelService, value, languageService.createByFilepathOrFirstLine(uri || null, firstLine), uri);
  }
  return doCreateModel(modelService, value, languageService.createById(languageId), uri);
}
function doCreateModel(modelService, value, languageSelection, uri) {
  return modelService.createModel(value, languageSelection, uri);
}
exports.createTextModel = createTextModel;
