"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var control_exports = {};
__export(control_exports, {
  RichTextV2Control: () => RichTextV2Control
});
module.exports = __toCommonJS(control_exports);
var import_controls = require("@makeswift/controls");
var import_rich_text_v2 = require("./rich-text-v2");
class RichTextV2Control extends import_controls.ControlInstance {
  constructor(send, descriptor) {
    super(send);
    this.descriptor = descriptor;
  }
  static messagePrefix = `${import_controls.RichTextDefinition.type}::control-message`;
  // COSMOS -> HOST
  static RESET_VALUE = `${this.messagePrefix}::reset-value`;
  static FOCUS = `${this.messagePrefix}::focus`;
  static RUN_PLUGIN_CONTROL_ACTION = `${this.messagePrefix}::run-plugin-control-action`;
  // HOST -> COSMOS
  static SET_DEFAULT_VALUE = `${this.messagePrefix}::set-default-value`;
  static SET_PLUGIN_CONTROL_VALUE = `${this.messagePrefix}::set-plugin-control-value`;
  static ON_CHANGE = `${this.messagePrefix}::on-change`;
  static SELECT = `${this.messagePrefix}::select`;
  static SWITCH_TO_BUILD_MODE = `${this.messagePrefix}::switch-to-build-mode`;
  static CHANGE_BOX_MODEL = `${this.messagePrefix}::change-box-model`;
  static REDO = `${this.messagePrefix}::redo`;
  static UNDO = `${this.messagePrefix}::undo`;
  editor = null;
  defaultValue = null;
  recv = (message) => {
    if (!this.editor)
      return;
    switch (message.type) {
      case RichTextV2Control.FOCUS: {
        this.editor.focusAndSelectAll();
        break;
      }
      case RichTextV2Control.RESET_VALUE: {
        if (this.defaultValue) {
          this.editor.resetValue(this.defaultValue);
          setTimeout(() => {
            this.onLocalUserChange();
          });
        }
        break;
      }
      case RichTextV2Control.RUN_PLUGIN_CONTROL_ACTION: {
        this.descriptor.pluginControlAt(message.pluginIndex)?.onChange?.(this.editor, message.value);
        break;
      }
    }
  };
  child(_key) {
    return void 0;
  }
  setEditor(editor) {
    this.editor = editor;
    this.sendMessage({
      type: RichTextV2Control.SET_PLUGIN_CONTROL_VALUE,
      value: this.getValue(editor)
    });
    const _onChange = editor.onChange;
    this.editor.onChange = (options) => {
      _onChange(options);
      this.updatePluginValues();
      if (this.editor == null || options?.operation == null)
        return;
      this.onLocalUserChange();
    };
  }
  setDefaultValue(defaultValue) {
    this.defaultValue = defaultValue;
    this.sendMessage({
      type: RichTextV2Control.SET_DEFAULT_VALUE,
      value: defaultValue
    });
  }
  onLocalUserChange() {
    if (this.editor == null)
      return;
    this.sendMessage({
      type: RichTextV2Control.ON_CHANGE,
      value: import_rich_text_v2.RichTextV2Definition.nodesToDataV2(this.editor.children, this.editor.currentKey)
    });
  }
  updatePluginValues() {
    const editor = this.editor;
    if (editor == null)
      return;
    this.sendMessage({
      type: RichTextV2Control.SET_PLUGIN_CONTROL_VALUE,
      value: this.getValue(editor)
    });
  }
  getValue(editor) {
    return this.descriptor.pluginControls.map((control) => control.getValue?.(editor)) ?? [];
  }
  select() {
    this.sendMessage({ type: RichTextV2Control.SELECT });
  }
  switchToBuildMode() {
    this.sendMessage({ type: RichTextV2Control.SWITCH_TO_BUILD_MODE });
  }
  undo() {
    this.sendMessage({ type: RichTextV2Control.UNDO });
  }
  redo() {
    this.sendMessage({ type: RichTextV2Control.REDO });
  }
  changeBoxModel(boxModel) {
    this.sendMessage({
      type: RichTextV2Control.CHANGE_BOX_MODEL,
      payload: { boxModel }
    });
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  RichTextV2Control
});
//# sourceMappingURL=control.js.map