"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSharedEditingState = useSharedEditingState;
const react_1 = require("react");
const react_use_1 = require("react-use");
function useSharedEditingState(view, key) {
    const [value, setValue] = (0, react_1.useState)(false);
    const valueRef = (0, react_use_1.useLatest)(value);
    (0, react_1.useLayoutEffect)(() => {
        setValue(key.getValue(view.state)?.editing || false);
        return key.getNotifier(view.state).subscribe((data) => setValue(data?.editing || false));
    }, [key, view]);
    const { set, unset, toggle } = (0, react_1.useMemo)(() => {
        const dispatch = (value) => {
            view.dispatch(key.appendTransaction.update(view.state.tr, { editing: value }));
        };
        return {
            set: () => dispatch(true),
            unset: () => dispatch(false),
            toggle: () => dispatch(!valueRef.current),
        };
    }, [key, view, valueRef]);
    return [value, set, unset, toggle];
}
//# sourceMappingURL=useSharedEditingState.js.map
