"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 ricos_schema_exports = {};
__export(ricos_schema_exports, {
  RichContentSchema: () => RichContentSchema,
  RicosSchema: () => RicosSchema
});
module.exports = __toCommonJS(ricos_schema_exports);
var import_function = require("fp-ts-esm/function");
var import_ricos_content = require("./../ricos-content/index.js");
var import_v1 = require("./../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_editor_common = require("./../editor-common/index.js");
class RicosSchema {
  constructor(initialContent, converters, shouldNormalizeRichContent = true) {
    this.getContent = () => {
      return this.schema.getContent();
    };
    this.initialContent = converters.toPm(
      initialContent?.nodes?.length ? initialContent : (0, import_editor_common.getEmptyContent)()
    );
    this.schema = new RichContentSchema(converters, shouldNormalizeRichContent);
  }
  setEditor(editor) {
    this.initialTiptapContent = editor.adapter.tiptapEditor.state.doc.content;
    this.editor = editor;
    this.schema.setEditor(editor);
  }
  setContent(content) {
    this.schema.setContent(content);
  }
  getTiptapContent() {
    if (!this.editor) {
      return this.initialContent;
    }
    return this.editor.adapter.tiptapEditor.getJSON();
  }
  getID() {
    return this.schema.getID();
  }
  isContentChanged() {
    const editorContent = this.editor?.adapter.tiptapEditor.state.doc.content;
    return !!editorContent && !this.initialTiptapContent?.eq(editorContent);
  }
  isEmpty() {
    return this.schema.isEmpty();
  }
}
class RichContentSchema {
  constructor(converters, shouldNormalizeRichContent = true) {
    this.converters = converters;
    this.shouldNormalizeRichContent = shouldNormalizeRichContent;
  }
  setEditor(editor) {
    this.editor = editor;
  }
  setContent(content) {
    const jsonContent = this.converters.toPm(content);
    this.editor.adapter.setTiptapContent(jsonContent);
  }
  getContent() {
    const tiptapContent = this.editor?.adapter.tiptapEditor.getJSON();
    const content = this.converters.fromPm(tiptapContent);
    return this.shouldNormalizeRichContent ? normalizeRichContent(content) : content;
  }
  getID() {
    return this.getContent().metadata?.id;
  }
  isEmpty() {
    const content = this.getContent();
    return (0, import_ricos_content.isRichContentEmpty)(content);
  }
}
const normalizeRichContent = (0, import_function.flow)(import_v1.RichContent.fromJSON, import_v1.RichContent.toJSON);
//# sourceMappingURL=ricos-schema.js.map
