const require_rolldown_runtime = require("./rolldown-runtime-C34Jxks9.cjs");
const require_create_super_doc_ui = require("./create-super-doc-ui-BcDSFn5o.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,
	authors: []
};
var EMPTY_CONTENT_CONTROLS = {
	items: [],
	activeIds: [],
	activeId: null,
	total: 0
};
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 useSuperDocContentControls() {
	return useSuperDocSlice((ui) => ui.select((state) => state.contentControls, require_create_super_doc_ui.shallowEqual), EMPTY_CONTENT_CONTROLS);
}
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;
}
Object.defineProperty(exports, "SuperDocUIProvider", {
	enumerable: true,
	get: function() {
		return SuperDocUIProvider;
	}
});
Object.defineProperty(exports, "useSetSuperDoc", {
	enumerable: true,
	get: function() {
		return useSetSuperDoc;
	}
});
Object.defineProperty(exports, "useSuperDocCommand", {
	enumerable: true,
	get: function() {
		return useSuperDocCommand;
	}
});
Object.defineProperty(exports, "useSuperDocComments", {
	enumerable: true,
	get: function() {
		return useSuperDocComments;
	}
});
Object.defineProperty(exports, "useSuperDocContentControls", {
	enumerable: true,
	get: function() {
		return useSuperDocContentControls;
	}
});
Object.defineProperty(exports, "useSuperDocDocument", {
	enumerable: true,
	get: function() {
		return useSuperDocDocument;
	}
});
Object.defineProperty(exports, "useSuperDocHost", {
	enumerable: true,
	get: function() {
		return useSuperDocHost;
	}
});
Object.defineProperty(exports, "useSuperDocSelection", {
	enumerable: true,
	get: function() {
		return useSuperDocSelection;
	}
});
Object.defineProperty(exports, "useSuperDocSlice", {
	enumerable: true,
	get: function() {
		return useSuperDocSlice;
	}
});
Object.defineProperty(exports, "useSuperDocToolbar", {
	enumerable: true,
	get: function() {
		return useSuperDocToolbar;
	}
});
Object.defineProperty(exports, "useSuperDocTrackChanges", {
	enumerable: true,
	get: function() {
		return useSuperDocTrackChanges;
	}
});
Object.defineProperty(exports, "useSuperDocUI", {
	enumerable: true,
	get: function() {
		return useSuperDocUI;
	}
});
