import { BuilderEditMode } from "../../state/modules/builder-edit-mode";
import {
  ControlInstance,
  richTextDAOToDTO
} from "@makeswift/controls";
class RichTextControl extends ControlInstance {
  static CHANGE_BUILDER_EDIT_MODE = "CHANGE_BUILDER_EDIT_MODE";
  static INITIALIZE_EDITOR = "INITIALIZE_EDITOR";
  static CHANGE_EDITOR_VALUE = "CHANGE_EDITOR_VALUE";
  static FOCUS = "FOCUS";
  static BLUR = "BLUR";
  static UNDO = "UNDO";
  static REDO = "REDO";
  static CHANGE_BOX_MODEL = "CHANGE_BOX_MODEL";
  editor = null;
  child(_key) {
    return void 0;
  }
  recv = (message) => {
    if (!this.editor)
      return;
    switch (message.type) {
      case RichTextControl.CHANGE_BUILDER_EDIT_MODE: {
        switch (message.payload.editMode) {
          case BuilderEditMode.BUILD:
          case BuilderEditMode.INTERACT:
            this.editor.deselectAndBlur();
        }
        break;
      }
      case RichTextControl.FOCUS: {
        this.editor.focusAndSelectAll();
        break;
      }
    }
  };
  setSlateEditor(editor) {
    this.editor = editor;
    this.sendMessage({
      type: RichTextControl.INITIALIZE_EDITOR,
      value: richTextDAOToDTO(editor.children, editor.selection)
    });
    const _onChange = editor.onChange;
    this.editor.onChange = (options) => {
      _onChange(options);
      if (options?.operation != null) {
        this.sendMessage({
          type: RichTextControl.CHANGE_EDITOR_VALUE,
          value: richTextDAOToDTO(editor.children, editor.selection)
        });
      }
    };
  }
  focus() {
    this.sendMessage({ type: RichTextControl.FOCUS });
  }
  blur() {
    this.sendMessage({ type: RichTextControl.BLUR });
  }
  undo() {
    this.sendMessage({ type: RichTextControl.UNDO });
  }
  redo() {
    this.sendMessage({ type: RichTextControl.REDO });
  }
  changeBoxModel(boxModel) {
    this.sendMessage({
      type: RichTextControl.CHANGE_BOX_MODEL,
      payload: { boxModel }
    });
  }
}
export {
  RichTextControl
};
//# sourceMappingURL=control.js.map