import "./standalone-tokens.css.mjs";
import { splitLines } from "../../../base/common/strings.mjs";
import { FontMeasurements } from "../../browser/config/fontMeasurements.mjs";
import { ICodeEditorService } from "../../browser/services/codeEditorService.mjs";
import { DiffNavigator } from "../../browser/widget/diffNavigator.mjs";
import { ConfigurationChangedEvent, ApplyUpdateResult, EditorOptions } from "../../common/config/editorOptions.mjs";
import { BareFontInfo, FontInfo } from "../../common/config/fontInfo.mjs";
import { EditorType } from "../../common/editorCommon.mjs";
import { TextModelResolvedOptions, FindMatch } from "../../common/model.mjs";
import { TokenizationRegistry } from "../../common/languages.mjs";
import { ILanguageConfigurationService } from "../../common/languages/languageConfigurationRegistry.mjs";
import { NullState, nullTokenize } from "../../common/languages/nullTokenize.mjs";
import { ILanguageService } from "../../common/languages/language.mjs";
import { IModelService } from "../../common/services/model.mjs";
import { createWebWorker as createWebWorker$1 } from "../../browser/services/webWorker.mjs";
import { AccessibilitySupport, ContentWidgetPositionPreference, CursorChangeReason, DefaultEndOfLine, EditorAutoIndentStrategy, EditorOption, EndOfLinePreference, EndOfLineSequence, MinimapPosition, MouseTargetType, OverlayWidgetPositionPreference, OverviewRulerLane, RenderLineNumbersType, RenderMinimap, ScrollbarVisibility, ScrollType, TextEditorCursorBlinkingStyle, TextEditorCursorStyle, TrackedRangeStickiness, WrappingIndent, InjectedTextCursorStops, PositionAffinity } from "../../common/standalone/standaloneEnums.mjs";
import { Colorizer } from "./colorizer.mjs";
import { StandaloneEditor, StandaloneDiffEditor, createTextModel } from "./standaloneCodeEditor.mjs";
import { StandaloneServices } from "./standaloneServices.mjs";
import { IStandaloneThemeService } from "../common/standaloneTheme.mjs";
import { CommandsRegistry } from "../../../platform/commands/common/commands.mjs";
import { IMarkerService } from "../../../platform/markers/common/markers.mjs";
function create(domElement, options, override) {
  const instantiationService = StandaloneServices.initialize(override || {});
  return instantiationService.createInstance(StandaloneEditor, domElement, options);
}
function onDidCreateEditor(listener) {
  const codeEditorService = StandaloneServices.get(ICodeEditorService);
  return codeEditorService.onCodeEditorAdd((editor) => {
    listener(editor);
  });
}
function createDiffEditor(domElement, options, override) {
  const instantiationService = StandaloneServices.initialize(override || {});
  return instantiationService.createInstance(StandaloneDiffEditor, domElement, options);
}
function createDiffNavigator(diffEditor, opts) {
  return new DiffNavigator(diffEditor, opts);
}
function createModel(value, language, uri) {
  const languageService = StandaloneServices.get(ILanguageService);
  const languageId = languageService.getLanguageIdByMimeType(language) || language;
  return createTextModel(StandaloneServices.get(IModelService), languageService, value, languageId, uri);
}
function setModelLanguage(model, languageId) {
  const languageService = StandaloneServices.get(ILanguageService);
  const modelService = StandaloneServices.get(IModelService);
  modelService.setMode(model, languageService.createById(languageId));
}
function setModelMarkers(model, owner, markers) {
  if (model) {
    const markerService = StandaloneServices.get(IMarkerService);
    markerService.changeOne(owner, model.uri, markers);
  }
}
function getModelMarkers(filter) {
  const markerService = StandaloneServices.get(IMarkerService);
  return markerService.read(filter);
}
function onDidChangeMarkers(listener) {
  const markerService = StandaloneServices.get(IMarkerService);
  return markerService.onMarkerChanged(listener);
}
function getModel(uri) {
  const modelService = StandaloneServices.get(IModelService);
  return modelService.getModel(uri);
}
function getModels() {
  const modelService = StandaloneServices.get(IModelService);
  return modelService.getModels();
}
function onDidCreateModel(listener) {
  const modelService = StandaloneServices.get(IModelService);
  return modelService.onModelAdded(listener);
}
function onWillDisposeModel(listener) {
  const modelService = StandaloneServices.get(IModelService);
  return modelService.onModelRemoved(listener);
}
function onDidChangeModelLanguage(listener) {
  const modelService = StandaloneServices.get(IModelService);
  return modelService.onModelLanguageChanged((e) => {
    listener({
      model: e.model,
      oldLanguage: e.oldLanguageId
    });
  });
}
function createWebWorker(opts) {
  return createWebWorker$1(StandaloneServices.get(IModelService), StandaloneServices.get(ILanguageConfigurationService), opts);
}
function colorizeElement(domNode, options) {
  const languageService = StandaloneServices.get(ILanguageService);
  const themeService = StandaloneServices.get(IStandaloneThemeService);
  themeService.registerEditorContainer(domNode);
  return Colorizer.colorizeElement(themeService, languageService, domNode, options);
}
function colorize(text, languageId, options) {
  const languageService = StandaloneServices.get(ILanguageService);
  const themeService = StandaloneServices.get(IStandaloneThemeService);
  themeService.registerEditorContainer(document.body);
  return Colorizer.colorize(languageService, text, languageId, options);
}
function colorizeModelLine(model, lineNumber, tabSize = 4) {
  const themeService = StandaloneServices.get(IStandaloneThemeService);
  themeService.registerEditorContainer(document.body);
  return Colorizer.colorizeModelLine(model, lineNumber, tabSize);
}
function getSafeTokenizationSupport(language) {
  const tokenizationSupport = TokenizationRegistry.get(language);
  if (tokenizationSupport) {
    return tokenizationSupport;
  }
  return {
    getInitialState: () => NullState,
    tokenize: (line, hasEOL, state) => nullTokenize(language, state)
  };
}
function tokenize(text, languageId) {
  TokenizationRegistry.getOrCreate(languageId);
  const tokenizationSupport = getSafeTokenizationSupport(languageId);
  const lines = splitLines(text);
  const result = [];
  let state = tokenizationSupport.getInitialState();
  for (let i = 0, len = lines.length; i < len; i++) {
    const line = lines[i];
    const tokenizationResult = tokenizationSupport.tokenize(line, true, state);
    result[i] = tokenizationResult.tokens;
    state = tokenizationResult.endState;
  }
  return result;
}
function defineTheme(themeName, themeData) {
  const standaloneThemeService = StandaloneServices.get(IStandaloneThemeService);
  standaloneThemeService.defineTheme(themeName, themeData);
}
function setTheme(themeName) {
  const standaloneThemeService = StandaloneServices.get(IStandaloneThemeService);
  standaloneThemeService.setTheme(themeName);
}
function remeasureFonts() {
  FontMeasurements.clearAllFontInfos();
}
function registerCommand(id, handler) {
  return CommandsRegistry.registerCommand({ id, handler });
}
function createMonacoEditorAPI() {
  return {
    // methods
    create,
    onDidCreateEditor,
    createDiffEditor,
    createDiffNavigator,
    createModel,
    setModelLanguage,
    setModelMarkers,
    getModelMarkers,
    onDidChangeMarkers,
    getModels,
    getModel,
    onDidCreateModel,
    onWillDisposeModel,
    onDidChangeModelLanguage,
    createWebWorker,
    colorizeElement,
    colorize,
    colorizeModelLine,
    tokenize,
    defineTheme,
    setTheme,
    remeasureFonts,
    registerCommand,
    // enums
    AccessibilitySupport,
    ContentWidgetPositionPreference,
    CursorChangeReason,
    DefaultEndOfLine,
    EditorAutoIndentStrategy,
    EditorOption,
    EndOfLinePreference,
    EndOfLineSequence,
    MinimapPosition,
    MouseTargetType,
    OverlayWidgetPositionPreference,
    OverviewRulerLane,
    RenderLineNumbersType,
    RenderMinimap,
    ScrollbarVisibility,
    ScrollType,
    TextEditorCursorBlinkingStyle,
    TextEditorCursorStyle,
    TrackedRangeStickiness,
    WrappingIndent,
    InjectedTextCursorStops,
    PositionAffinity,
    // classes
    ConfigurationChangedEvent,
    BareFontInfo,
    FontInfo,
    TextModelResolvedOptions,
    FindMatch,
    ApplyUpdateResult,
    // vars
    EditorType,
    EditorOptions
  };
}
export {
  colorize,
  colorizeElement,
  colorizeModelLine,
  create,
  createDiffEditor,
  createDiffNavigator,
  createModel,
  createMonacoEditorAPI,
  createWebWorker,
  defineTheme,
  getModel,
  getModelMarkers,
  getModels,
  onDidChangeMarkers,
  onDidChangeModelLanguage,
  onDidCreateEditor,
  onDidCreateModel,
  onWillDisposeModel,
  registerCommand,
  remeasureFonts,
  setModelLanguage,
  setModelMarkers,
  setTheme,
  tokenize
};
