"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __typeError = (msg) => { throw TypeError(msg); }; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)); var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value); // src/lib/index.tsx var lib_exports = {}; __export(lib_exports, { createStore: () => createStore, createStoreContext: () => createStoreContext }); module.exports = __toCommonJS(lib_exports); var import_react = require("react"); var import_react_dom = require("react-dom"); var import_memoize_one = __toESM(require("memoize-one"), 1); var import_jsx_runtime = require("react/jsx-runtime"); var _state, _subscribers, _initialState, _broadcast; var Store = class { constructor(initializer = {}) { __privateAdd(this, _state); __privateAdd(this, _subscribers); __privateAdd(this, _initialState); __publicField(this, "reset", () => { __privateSet(this, _state, __privateGet(this, _initialState)); __privateGet(this, _broadcast).call(this); }); __publicField(this, "setState", (updater) => { __privateSet(this, _state, updater(this.getState())); __privateGet(this, _broadcast).call(this); }); __publicField(this, "getState", () => __privateGet(this, _state)); __publicField(this, "subscribe", (sub) => { __privateGet(this, _subscribers).push(sub); return () => { __privateSet(this, _subscribers, __privateGet(this, _subscribers).filter((entry) => entry !== sub)); }; }); __privateAdd(this, _broadcast, () => { (0, import_react_dom.unstable_batchedUpdates)(() => { __privateGet(this, _subscribers).forEach((subscriber) => { subscriber(); }); }); }); const initialState = typeof initializer === "function" ? initializer() : initializer; __privateSet(this, _state, initialState); __privateSet(this, _subscribers, []); __privateSet(this, _initialState, initialState); } }; _state = new WeakMap(); _subscribers = new WeakMap(); _initialState = new WeakMap(); _broadcast = new WeakMap(); function defaultSelector(state) { return state; } function createStore(initializer) { const store = new Store(initializer); function useSlice(selector) { const getSnapshot = (0, import_react.useMemo)(() => { if (typeof selector === "string") { return () => store.getState()[selector]; } const _selector = selector ?? defaultSelector; const memoizedSelector = (0, import_memoize_one.default)(_selector); return () => memoizedSelector(store.getState()); }, [selector]); const slice = (0, import_react.useSyncExternalStore)(store.subscribe, getSnapshot); return slice; } return { store, useSlice }; } function createStoreContext() { const StoreContext = (0, import_react.createContext)( {} ); function StoreProvider({ children, initialState = {} }) { const [value] = (0, import_react.useState)(() => createStore(initialState)); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(StoreContext.Provider, { value, children }); } function useStore() { const { store } = (0, import_react.useContext)(StoreContext); return store; } function useSlice(selector) { const { useSlice: useCtxSlice } = (0, import_react.useContext)(StoreContext); return useCtxSlice(selector); } const StoreConsumer = StoreContext.Consumer; return { StoreProvider, StoreConsumer, useStore, useSlice }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createStore, createStoreContext });