import "./chunks/SuperConverter-BBrzKfHe.es.js";
import "./chunks/jszip-C49i9kUs.es.js";
import "./chunks/xml-js-CqGKpaft.es.js";
import "./chunks/create-headless-toolbar-CnOrqu6V.es.js";
import "./chunks/constants-DrU4EASo.es.js";
import "./chunks/dist-B8HfvhaK.es.js";
import "./chunks/unified-Dsuw2be5.es.js";
import { n as shallowEqual, t as createSuperDocUI } from "./chunks/create-super-doc-ui-C403m6O0.es.js";
import { createContext, useCallback, useContext, useEffect, useRef, useState } from "react";
import { jsx } from "react/jsx-runtime";
var SuperDocUIContext = createContext(null);
function SuperDocUIProvider({ children }) {
	const [ui, setUI] = useState(null);
	const [host, setHost] = useState(null);
	const uiRef = useRef(null);
	const setSuperDoc = useCallback((instance) => {
		uiRef.current?.destroy();
		const next = createSuperDocUI({ superdoc: instance });
		uiRef.current = next;
		setUI(next);
		setHost(instance);
	}, []);
	useEffect(() => {
		return () => {
			uiRef.current?.destroy();
			uiRef.current = null;
		};
	}, []);
	return /* @__PURE__ */ jsx(SuperDocUIContext.Provider, {
		value: {
			ui,
			host,
			setSuperDoc
		},
		children
	});
}
function useSuperDocUI() {
	const ctx = useContext(SuperDocUIContext);
	if (!ctx) throw new Error("useSuperDocUI must be used inside <SuperDocUIProvider>.");
	return ctx.ui;
}
function useSuperDocHost() {
	const ctx = useContext(SuperDocUIContext);
	if (!ctx) throw new Error("useSuperDocHost must be used inside <SuperDocUIProvider>.");
	return ctx.host;
}
function useSetSuperDoc() {
	const ctx = 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] = useState(() => initial);
	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, shallowEqual), EMPTY_SELECTION);
}
function useSuperDocComments() {
	return useSuperDocSlice((ui) => ui.select((state) => state.comments, shallowEqual), EMPTY_COMMENTS);
}
function useSuperDocTrackChanges() {
	return useSuperDocSlice((ui) => ui.select((state) => state.trackChanges, shallowEqual), EMPTY_TRACK_CHANGES);
}
function useSuperDocToolbar() {
	return useSuperDocSlice((ui) => ui.select((state) => state.toolbar, shallowEqual), EMPTY_TOOLBAR);
}
function useSuperDocDocument() {
	return useSuperDocSlice((ui) => ui.select((state) => state.document, 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] = useState(FALLBACK_COMMAND_STATE);
	useEffect(() => {
		if (!ui) {
			setValue(FALLBACK_COMMAND_STATE);
			return;
		}
		return ui.select((state) => state.toolbar.commands?.[id] ?? FALLBACK_COMMAND_STATE, shallowEqual).subscribe((next) => setValue(next));
	}, [ui, id]);
	return value;
}
export { SuperDocUIProvider, useSetSuperDoc, useSuperDocCommand, useSuperDocComments, useSuperDocDocument, useSuperDocHost, useSuperDocSelection, useSuperDocSlice, useSuperDocToolbar, useSuperDocTrackChanges, useSuperDocUI };
