import { markdownLoader as upstreamMarkdownLoader } from '@bangle.dev/pm-markdown/markdown';
import type { CollectionType } from '@learnitive/prosemirror-editor';
import { createBaseMarkdownTokenizer } from '@learnitive/markdown-syntax';
import type { Schema } from 'prosemirror-model';

/**
 * Assembles the ProseMirror-stack tokenizer: the engine-neutral base
 * (`@learnitive/markdown-syntax`) plus every extension's own markdown-it plugins.
 *
 * The base configuration lives in the shared syntax package so this editor
 * engine and any other agree byte-for-byte on how Markdown tokenizes; only the
 * per-extension `tokenizerPlugins` are ProseMirror-collection specific and stay
 * here.
 */
function createMarkdownTokenizer(items: readonly CollectionType[]) {
  const tokenizer = createBaseMarkdownTokenizer();
  for (const item of items) {
    for (const plugin of item.markdown?.tokenizerPlugins ?? []) {
      tokenizer.use(plugin);
    }
  }
  return tokenizer;
}

export function markdownLoader(items: CollectionType[], schema: Schema) {
  const tokenizer = createMarkdownTokenizer(items);
  return upstreamMarkdownLoader(items, schema, tokenizer);
}
