"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 create_tiptap_extensions_exports = {};
__export(create_tiptap_extensions_exports, {
  createTiptapExtensions: () => createTiptapExtensions
});
module.exports = __toCommonJS(create_tiptap_extensions_exports);
var import_uncreachable = require("./../../_shared/uncreachable.js");
var import_core = require("./../../tiptap/core/index.js");
var import_constants = require("./constants.js");
var import_functional_extension = require("./functional-extension.js");
var import_mark_extension = require("./mark-extension.js");
var import_node_extension = require("./node-extension.js");
const defaultIExtension = {
  toTiptapExtension: import_core.Extension.create
};
const toIExtension = (extension, services) => {
  switch (extension.type) {
    case "react-node":
      return new import_node_extension.ReactNodeExtension(extension);
    case "html-node":
      return new import_node_extension.HtmlNodeExtension(extension);
    case "extension":
      return new import_functional_extension.FunctionalExtension(extension, services);
    case "mark":
      return new import_mark_extension.MarkExtension(extension);
    default:
      (0, import_uncreachable.unreachable)(extension);
      return defaultIExtension;
  }
};
const isFunctionalExtension = (extension) => extension.type === "extension";
const isReactNodeExtension = (extension) => extension.type === "react-node";
const isHtmlNodeExtension = (extension) => extension.type === "html-node";
const isMarkExtension = (extension) => extension.type === "mark";
const validateNames = (extensions) => {
  const names = extensions.map((extension) => extension.name);
  const duplicateNames = names.map((name) => ({ name, duplicate: names.indexOf(name) !== names.lastIndexOf(name) })).filter(({ duplicate }) => !!duplicate).map(({ name }) => name);
  if (duplicateNames.length > 0) {
    throw new Error(`Extension name collision: ${duplicateNames.join(" ,")}`);
  }
};
const decorateReactNodeExtension = (extension, hocDescriptors) => {
  const ricosExtension = extension.getRicosExtension();
  const Component = extension.getComponent();
  const DecoratedComponent = hocDescriptors.filter((desc) => desc.nodeTypes?.includes(extension.name) || desc.nodeTypes?.includes("*")).map((desc) => desc.getNodeHoc(ricosExtension)).reduce((component, hoc) => hoc(component), Component);
  return extension.asRenderable(DecoratedComponent);
};
const createTiptapExtensions = (ricosExtensions, props, services) => {
  const sortedExtensions = ricosExtensions.map((extension) => toIExtension(extension, services)).sort((a, b) => a.priority - b.priority);
  validateNames(sortedExtensions);
  const sortedRicosExtensions = sortedExtensions.map((extension) => extension.getRicosExtension());
  const hocDescriptors = sortedExtensions.filter(isFunctionalExtension).map((extension) => extension.getNodeHocDescriptor(sortedRicosExtensions, props, services)).filter((descriptor) => !!descriptor).sort((a, b) => (a.priority || import_constants.DEFAULT_PRIORITY) - (b.priority || import_constants.DEFAULT_PRIORITY));
  const reactNodes = sortedExtensions.filter(isReactNodeExtension).map((extension) => decorateReactNodeExtension(extension, hocDescriptors)).map((extension) => extension.toTiptapExtension(sortedRicosExtensions, props, services));
  const htmlNodes = sortedExtensions.filter(isHtmlNodeExtension).map((extension) => extension.toTiptapExtension(sortedRicosExtensions, props, services));
  const marks = sortedExtensions.filter(isMarkExtension).map((extension) => extension.toTiptapExtension(sortedRicosExtensions, props, services));
  const extensions = sortedExtensions.filter(isFunctionalExtension).map((extension) => extension.toTiptapExtension(sortedRicosExtensions, props, services));
  return [...extensions, ...marks, ...reactNodes, ...htmlNodes];
};
//# sourceMappingURL=create-tiptap-extensions.js.map
