import { Doc, applyUpdate, encodeStateAsUpdate } from "yjs";
import { prosemirrorJSONToYDoc, yDocToProsemirrorJSON } from "y-prosemirror";
import { Schema } from "@tiptap/pm/model";
import { getSchema } from "@tiptap/core";
import { StarterKit } from "@tiptap/starter-kit";

//#region packages/transformer/src/Prosemirror.ts
var Prosemirror = class {
	constructor() {
		this.defaultSchema = new Schema({ nodes: {
			text: {},
			doc: { content: "text*" }
		} });
	}
	schema(schema) {
		this.defaultSchema = schema;
		return this;
	}
	fromYdoc(document, fieldName) {
		const data = {};
		if (typeof fieldName === "string") return yDocToProsemirrorJSON(document, fieldName);
		if (fieldName === void 0 || fieldName.length === 0) fieldName = Array.from(document.share.keys());
		fieldName.forEach((field) => {
			data[field] = yDocToProsemirrorJSON(document, field);
		});
		return data;
	}
	toYdoc(document, fieldName = "prosemirror", schema) {
		if (!document) throw new Error(`You’ve passed an empty or invalid document to the Transformer. Make sure to pass ProseMirror-compatible JSON. Actually passed JSON: ${document}`);
		if (typeof fieldName === "string") return prosemirrorJSONToYDoc(schema || this.defaultSchema, document, fieldName);
		const ydoc = new Doc();
		fieldName.forEach((field) => {
			applyUpdate(ydoc, encodeStateAsUpdate(prosemirrorJSONToYDoc(schema || this.defaultSchema, document, field)));
		});
		return ydoc;
	}
};
const ProsemirrorTransformer = new Prosemirror();

//#endregion
//#region packages/transformer/src/Tiptap.ts
var Tiptap = class {
	constructor() {
		this.defaultExtensions = [StarterKit];
	}
	extensions(extensions) {
		this.defaultExtensions = extensions;
		return this;
	}
	fromYdoc(document, fieldName) {
		return ProsemirrorTransformer.fromYdoc(document, fieldName);
	}
	toYdoc(document, fieldName = "default", extensions) {
		return ProsemirrorTransformer.toYdoc(document, fieldName, getSchema(extensions || this.defaultExtensions));
	}
};
const TiptapTransformer = new Tiptap();

//#endregion
export { ProsemirrorTransformer, Tiptap, TiptapTransformer };
//# sourceMappingURL=hocuspocus-transformer.esm.js.map