"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 get_schema_by_resolved_extensions_exports = {};
__export(get_schema_by_resolved_extensions_exports, {
  getSchemaByResolvedExtensions: () => getSchemaByResolvedExtensions
});
module.exports = __toCommonJS(get_schema_by_resolved_extensions_exports);
var import_model = require("./../../pm/model.js");
var import_call_or_return = require("../utilities/call-or-return.js");
var import_is_empty_object = require("../utilities/is-empty-object.js");
var import_get_attributes_from_extensions = require("./get-attributes-from-extensions.js");
var import_get_extension_field = require("./get-extension-field.js");
var import_get_rendered_attributes = require("./get-rendered-attributes.js");
var import_inject_extension_attributes_to_parse_rule = require("./inject-extension-attributes-to-parse-rule.js");
var import_split_extensions = require("./split-extensions.js");
function cleanUpSchemaItem(data) {
  return Object.fromEntries(
    // @ts-ignore
    Object.entries(data).filter(([key, value]) => {
      if (key === "attrs" && (0, import_is_empty_object.isEmptyObject)(value)) {
        return false;
      }
      return value !== null && value !== void 0;
    })
  );
}
function getSchemaByResolvedExtensions(extensions, editor) {
  const allAttributes = (0, import_get_attributes_from_extensions.getAttributesFromExtensions)(extensions);
  const { nodeExtensions, markExtensions } = (0, import_split_extensions.splitExtensions)(extensions);
  const topNode = nodeExtensions.find((extension) => (0, import_get_extension_field.getExtensionField)(extension, "topNode"))?.name;
  const nodes = Object.fromEntries(
    nodeExtensions.map((extension) => {
      const extensionAttributes = allAttributes.filter(
        (attribute) => attribute.type === extension.name
      );
      const context = {
        name: extension.name,
        options: extension.options,
        storage: extension.storage,
        editor
      };
      const extraNodeFields = extensions.reduce((fields, e) => {
        const extendNodeSchema = (0, import_get_extension_field.getExtensionField)(
          e,
          "extendNodeSchema",
          context
        );
        return {
          ...fields,
          ...extendNodeSchema ? extendNodeSchema(extension) : {}
        };
      }, {});
      const schema = cleanUpSchemaItem({
        ...extraNodeFields,
        content: (0, import_call_or_return.callOrReturn)(
          (0, import_get_extension_field.getExtensionField)(extension, "content", context)
        ),
        marks: (0, import_call_or_return.callOrReturn)((0, import_get_extension_field.getExtensionField)(extension, "marks", context)),
        group: (0, import_call_or_return.callOrReturn)((0, import_get_extension_field.getExtensionField)(extension, "group", context)),
        inline: (0, import_call_or_return.callOrReturn)((0, import_get_extension_field.getExtensionField)(extension, "inline", context)),
        atom: (0, import_call_or_return.callOrReturn)((0, import_get_extension_field.getExtensionField)(extension, "atom", context)),
        selectable: (0, import_call_or_return.callOrReturn)(
          (0, import_get_extension_field.getExtensionField)(extension, "selectable", context)
        ),
        draggable: (0, import_call_or_return.callOrReturn)(
          (0, import_get_extension_field.getExtensionField)(extension, "draggable", context)
        ),
        code: (0, import_call_or_return.callOrReturn)((0, import_get_extension_field.getExtensionField)(extension, "code", context)),
        whitespace: (0, import_call_or_return.callOrReturn)(
          (0, import_get_extension_field.getExtensionField)(extension, "whitespace", context)
        ),
        defining: (0, import_call_or_return.callOrReturn)(
          (0, import_get_extension_field.getExtensionField)(extension, "defining", context)
        ),
        isolating: (0, import_call_or_return.callOrReturn)(
          (0, import_get_extension_field.getExtensionField)(extension, "isolating", context)
        ),
        attrs: Object.fromEntries(
          extensionAttributes.map((extensionAttribute) => {
            return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }];
          })
        )
      });
      const parseHTML = (0, import_call_or_return.callOrReturn)(
        (0, import_get_extension_field.getExtensionField)(extension, "parseHTML", context)
      );
      if (parseHTML) {
        schema.parseDOM = parseHTML.map(
          (parseRule) => (0, import_inject_extension_attributes_to_parse_rule.injectExtensionAttributesToParseRule)(parseRule, extensionAttributes)
        );
      }
      const renderHTML = (0, import_get_extension_field.getExtensionField)(
        extension,
        "renderHTML",
        context
      );
      if (renderHTML) {
        schema.toDOM = (node) => renderHTML({
          node,
          HTMLAttributes: (0, import_get_rendered_attributes.getRenderedAttributes)(node, extensionAttributes)
        });
      }
      const renderText = (0, import_get_extension_field.getExtensionField)(
        extension,
        "renderText",
        context
      );
      if (renderText) {
        schema.toText = renderText;
      }
      return [extension.name, schema];
    })
  );
  const marks = Object.fromEntries(
    markExtensions.map((extension) => {
      const extensionAttributes = allAttributes.filter(
        (attribute) => attribute.type === extension.name
      );
      const context = {
        name: extension.name,
        options: extension.options,
        storage: extension.storage,
        editor
      };
      const extraMarkFields = extensions.reduce((fields, e) => {
        const extendMarkSchema = (0, import_get_extension_field.getExtensionField)(
          e,
          "extendMarkSchema",
          context
        );
        return {
          ...fields,
          ...extendMarkSchema ? extendMarkSchema(extension) : {}
        };
      }, {});
      const schema = cleanUpSchemaItem({
        ...extraMarkFields,
        inclusive: (0, import_call_or_return.callOrReturn)(
          (0, import_get_extension_field.getExtensionField)(extension, "inclusive", context)
        ),
        excludes: (0, import_call_or_return.callOrReturn)(
          (0, import_get_extension_field.getExtensionField)(extension, "excludes", context)
        ),
        group: (0, import_call_or_return.callOrReturn)((0, import_get_extension_field.getExtensionField)(extension, "group", context)),
        spanning: (0, import_call_or_return.callOrReturn)(
          (0, import_get_extension_field.getExtensionField)(extension, "spanning", context)
        ),
        code: (0, import_call_or_return.callOrReturn)((0, import_get_extension_field.getExtensionField)(extension, "code", context)),
        attrs: Object.fromEntries(
          extensionAttributes.map((extensionAttribute) => {
            return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }];
          })
        )
      });
      const parseHTML = (0, import_call_or_return.callOrReturn)(
        (0, import_get_extension_field.getExtensionField)(extension, "parseHTML", context)
      );
      if (parseHTML) {
        schema.parseDOM = parseHTML.map(
          (parseRule) => (0, import_inject_extension_attributes_to_parse_rule.injectExtensionAttributesToParseRule)(parseRule, extensionAttributes)
        );
      }
      const renderHTML = (0, import_get_extension_field.getExtensionField)(
        extension,
        "renderHTML",
        context
      );
      if (renderHTML) {
        schema.toDOM = (mark) => renderHTML({
          mark,
          HTMLAttributes: (0, import_get_rendered_attributes.getRenderedAttributes)(mark, extensionAttributes)
        });
      }
      return [extension.name, schema];
    })
  );
  return new import_model.Schema({
    topNode,
    nodes,
    marks
  });
}
//# sourceMappingURL=get-schema-by-resolved-extensions.js.map
