import { useSyncExternalStore } from "react";
import { createSignal } from "../utils/signal.js";
/**
 * Create a lightweight external store compatible with React's useSyncExternalStore.
 * Uses createSignal for pub/sub and Object.freeze for snapshot immutability.
 */
export function createStore(initialState) {
    const signal = createSignal();
    let state = Object.freeze({ ...initialState });
    return {
        subscribe(cb) {
            return signal.subscribe(cb);
        },
        getSnapshot() {
            return state;
        },
        setState(update) {
            const partial = typeof update === "function" ? update(state) : update;
            state = Object.freeze({ ...state, ...partial });
            signal.emit();
        },
    };
}
/**
 * React hook for consuming a store. Triggers re-render when any state changes.
 */
export function useStore(store) {
    return useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
}
//# sourceMappingURL=create-store.js.map