import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useRef } from 'react';
import { create } from 'zustand';
export const SplitSubvariantStoreContext = createContext(null);
const shouldRecreateStore = (store, props) => {
    const { state } = store.getState();
    return (!state && props.state) || (state && !props.state);
};
export function SplitSubvariantStoreProvider({ children, ...props }) {
    const storeRef = useRef();
    if (!storeRef.current || shouldRecreateStore(storeRef.current, props)) {
        storeRef.current = createSplitSubvariantStore(props);
    }
    return (_jsx(SplitSubvariantStoreContext.Provider, { value: storeRef.current, children: children }));
}
export function useSplitSubvariantStore(selector, equalityFn) {
    const useStore = useContext(SplitSubvariantStoreContext);
    if (!useStore) {
        throw new Error(`You forgot to wrap your component in <${SplitSubvariantStoreProvider.name}>.`);
    }
    return useStore(selector, equalityFn);
}
export function useSplitSubvariantStoreContext() {
    const useStore = useContext(SplitSubvariantStoreContext);
    if (!useStore) {
        throw new Error(`You forgot to wrap your component in <${SplitSubvariantStoreProvider.name}>.`);
    }
    return useStore;
}
export const createSplitSubvariantStore = ({ state }) => create((set) => ({
    state,
    setState(state) {
        set(() => ({
            state,
        }));
    },
}));
