Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_rolldown_runtime = require("./chunks/rolldown-runtime-C34Jxks9.cjs");
require("./chunks/SuperConverter-6rYi7ar5.cjs");
require("./chunks/jszip-4KbV_vHe.cjs");
require("./chunks/xml-js-B2J5tNmr.cjs");
require("./chunks/create-headless-toolbar-5mU1tOF-.cjs");
require("./chunks/constants-DA5YJ09l.cjs");
require("./chunks/dist-kRpWoWUg.cjs");
require("./chunks/unified-DgEQofGI.cjs");
const require_create_super_doc_ui = require("./chunks/create-super-doc-ui-BK0Qi_5W.cjs");
let react = require("react");
let react_jsx_runtime = require("react/jsx-runtime");
var SuperDocUIContext = (0, react.createContext)(null);
function SuperDocUIProvider({ children }) {
	const [ui, setUI] = (0, react.useState)(null);
	const [host, setHost] = (0, react.useState)(null);
	const uiRef = (0, react.useRef)(null);
	const setSuperDoc = (0, react.useCallback)((instance) => {
		uiRef.current?.destroy();
		const next = require_create_super_doc_ui.createSuperDocUI({ superdoc: instance });
		uiRef.current = next;
		setUI(next);
		setHost(instance);
	}, []);
	(0, react.useEffect)(() => {
		return () => {
			uiRef.current?.destroy();
			uiRef.current = null;
		};
	}, []);
	return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SuperDocUIContext.Provider, {
		value: {
			ui,
			host,
			setSuperDoc
		},
		children
	});
}
function useSuperDocUI() {
	const ctx = (0, react.useContext)(SuperDocUIContext);
	if (!ctx) throw new Error("useSuperDocUI must be used inside <SuperDocUIProvider>.");
	return ctx.ui;
}
function useSuperDocHost() {
	const ctx = (0, react.useContext)(SuperDocUIContext);
	if (!ctx) throw new Error("useSuperDocHost must be used inside <SuperDocUIProvider>.");
	return ctx.host;
}
function useSetSuperDoc() {
	const ctx = (0, react.useContext)(SuperDocUIContext);
	if (!ctx) throw new Error("useSetSuperDoc must be used inside <SuperDocUIProvider>.");
	return ctx.setSuperDoc;
}
function useSuperDocSlice(pickSubscribable, initial) {
	const ui = useSuperDocUI();
	const [value, setValue] = (0, react.useState)(() => initial);
	(0, react.useEffect)(() => {
		if (!ui) return;
		return pickSubscribable(ui).subscribe((next) => setValue(next));
	}, [ui]);
	return value;
}
var EMPTY_SELECTION = {
	empty: true,
	target: null,
	selectionTarget: null,
	activeMarks: [],
	activeCommentIds: [],
	activeChangeIds: [],
	quotedText: ""
};
var EMPTY_COMMENTS = {
	items: [],
	activeIds: [],
	total: 0
};
var EMPTY_TRACK_CHANGES = {
	items: [],
	total: 0,
	activeId: null
};
var EMPTY_TOOLBAR = {
	context: null,
	commands: {}
};
var EMPTY_DOCUMENT = {
	ready: false,
	mode: null,
	dirty: false
};
function useSuperDocSelection() {
	return useSuperDocSlice((ui) => ui.select((state) => state.selection, require_create_super_doc_ui.shallowEqual), EMPTY_SELECTION);
}
function useSuperDocComments() {
	return useSuperDocSlice((ui) => ui.select((state) => state.comments, require_create_super_doc_ui.shallowEqual), EMPTY_COMMENTS);
}
function useSuperDocTrackChanges() {
	return useSuperDocSlice((ui) => ui.select((state) => state.trackChanges, require_create_super_doc_ui.shallowEqual), EMPTY_TRACK_CHANGES);
}
function useSuperDocToolbar() {
	return useSuperDocSlice((ui) => ui.select((state) => state.toolbar, require_create_super_doc_ui.shallowEqual), EMPTY_TOOLBAR);
}
function useSuperDocDocument() {
	return useSuperDocSlice((ui) => ui.select((state) => state.document, require_create_super_doc_ui.shallowEqual), EMPTY_DOCUMENT);
}
var FALLBACK_COMMAND_STATE = {
	active: false,
	disabled: true,
	value: void 0,
	source: "built-in"
};
function useSuperDocCommand(id) {
	const ui = useSuperDocUI();
	const [value, setValue] = (0, react.useState)(FALLBACK_COMMAND_STATE);
	(0, react.useEffect)(() => {
		if (!ui) {
			setValue(FALLBACK_COMMAND_STATE);
			return;
		}
		return ui.select((state) => state.toolbar.commands?.[id] ?? FALLBACK_COMMAND_STATE, require_create_super_doc_ui.shallowEqual).subscribe((next) => setValue(next));
	}, [ui, id]);
	return value;
}
exports.SuperDocUIProvider = SuperDocUIProvider;
exports.useSetSuperDoc = useSetSuperDoc;
exports.useSuperDocCommand = useSuperDocCommand;
exports.useSuperDocComments = useSuperDocComments;
exports.useSuperDocDocument = useSuperDocDocument;
exports.useSuperDocHost = useSuperDocHost;
exports.useSuperDocSelection = useSuperDocSelection;
exports.useSuperDocSlice = useSuperDocSlice;
exports.useSuperDocToolbar = useSuperDocToolbar;
exports.useSuperDocTrackChanges = useSuperDocTrackChanges;
exports.useSuperDocUI = useSuperDocUI;
