{"version":3,"file":"createReduxAsyncStore.cjs","names":["createAsyncStore","useSelector","noop","combineReducers"],"sources":["../../src/createReduxAsyncStore/createReduxAsyncStore.ts"],"sourcesContent":["import { combineReducers, type  Reducer, type  Store } from 'redux';\nimport { configureStore, type UnknownAction } from '@reduxjs/toolkit';\nimport { useDispatch, useSelector, type TypedUseSelectorHook } from 'react-redux';\n// import { createAsyncStore, noop } from '@pastweb/tools';\nimport { noop } from '@pastweb/tools';\nimport { createAsyncStore } from './temp/createAsyncStore';\nimport type { ReduxAsyncStore, ReduxStoreOptions } from './types';\n\n/**\n * Creates and configures an async Redux store with support for async reducers and custom initialization.\n *\n * @param options - Configuration options for the Redux store.\n * @param options.settings - The settings object used to configure the Redux store. Passed directly to `configureStore`.\n * @param options.onInit - A function to be called when the store is initialized.\n *\n * @returns A customized `ReduxStore` object that includes:\n * - `store`: The underlying Redux store with async reducers support.\n * - `asyncReducers`: An object to store async reducers.\n * - `useDispatch`: A typed hook for dispatching actions.\n * - `useSelector`: A typed hook for selecting state from the store.\n * - `init`: A method to initialize the store and execute the `onInit` function.\n * - `addReducer`: A method to dynamically add a new reducer to the store.\n * - `removeReducer`: A method to dynamically remove an existing reducer from the store.\n *\n * @example\n * // Example usage:\n * const store = createReduxAsyncStore({\n *   settings: {\n *     reducer: { ...initial reducers },\n *   },\n *   onInit: async (store) => {\n *     // Custom initialization logic\n *   },\n * });\n * \n * store.addReducer('newReducer', myReducer);\n * store.removeReducer('oldReducer');\n */\nexport function createReduxAsyncStore(options: ReduxStoreOptions): ReduxAsyncStore {\n  const store: Store & {\n    asyncReducers: {\n      [reducerName: string]: Reducer;\n    };\n  } = {\n    ...configureStore(options.settings),\n    asyncReducers: {},\n  };\n  \n  const asyncStore = createAsyncStore<ReduxAsyncStore>({\n    ...options,\n    name: `ReduxAsyncStore${options.name ? `:${options.name}` : ''}`,\n  });\n\n  asyncStore.store = store;\n\n  type AppDispatch = typeof store.dispatch;\n  // Use throughout your app instead of plain `useDispatch` and `useSelector`\n  asyncStore.useDispatch = () => useDispatch<AppDispatch>();\n  asyncStore.useSelector = useSelector;\n  asyncStore.init = init;\n  asyncStore.addReducer = addReducer;\n  asyncStore.removeReducer = removeReducer;\n\n  async function init(): Promise<void> {\n    const { onInit = noop } = asyncStore.options;\n    await onInit(store);\n    asyncStore.setStoreReady();\n  }\n\n  function getRootReducer(asyncReducers = {}): Reducer<Record<string, any>, never> {\n    return combineReducers({\n      ...asyncStore.options.settings.reducer,\n      ...asyncReducers,\n    });\n  }\n\n  function addReducer(reducerKey: string, reducer: Reducer): void {\n    if (store.asyncReducers[reducerKey]) return;\n\n    store.asyncReducers[reducerKey] = reducer;\n    store.replaceReducer(getRootReducer(store.asyncReducers) as Reducer<any, UnknownAction, any>);\n  }\n\n  function removeReducer(reducerKey: string): void {\n    if (!store.asyncReducers[reducerKey]) return;\n\n    // Create new object without the removed reducer\n    const filtered = { ...store.asyncReducers };\n    delete filtered[reducerKey];\n    store.asyncReducers = filtered;\n\n    // Update Redux store with new root reducer\n    store.replaceReducer(getRootReducer(filtered) as Reducer<any, UnknownAction, any>);\n  }\n\n  return asyncStore as Omit<ReduxAsyncStore, 'useSelector'> & {\n    useSelector: TypedUseSelectorHook<ReturnType<typeof store.getState>>;\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,sBAAsB,SAA6C;CACjF,MAAM,QAIF;EACF,IAAA,GAAA,iBAAA,eAAA,CAAkB,QAAQ,QAAQ;EAClC,eAAe,CAAC;CAClB;CAEA,MAAM,aAAaA,qEAAAA,iBAAkC;EACnD,GAAG;EACH,MAAM,kBAAkB,QAAQ,OAAO,IAAI,QAAQ,SAAS;CAC9D,CAAC;CAED,WAAW,QAAQ;CAInB,WAAW,qBAAA,GAAA,YAAA,YAAA,CAA6C;CACxD,WAAW,cAAcC,YAAAA;CACzB,WAAW,OAAO;CAClB,WAAW,aAAa;CACxB,WAAW,gBAAgB;CAE3B,eAAe,OAAsB;EACnC,MAAM,EAAE,SAASC,eAAAA,SAAS,WAAW;EACrC,MAAM,OAAO,KAAK;EAClB,WAAW,cAAc;CAC3B;CAEA,SAAS,eAAe,gBAAgB,CAAC,GAAwC;EAC/E,OAAOC,cAAAA,gBAAgB;GACrB,GAAG,WAAW,QAAQ,SAAS;GAC/B,GAAG;EACL,CAAC;CACH;CAEA,SAAS,WAAW,YAAoB,SAAwB;EAC9D,IAAI,MAAM,cAAc,aAAa;EAErC,MAAM,cAAc,cAAc;EAClC,MAAM,eAAe,eAAe,MAAM,aAAa,CAAqC;CAC9F;CAEA,SAAS,cAAc,YAA0B;EAC/C,IAAI,CAAC,MAAM,cAAc,aAAa;EAGtC,MAAM,WAAW,EAAE,GAAG,MAAM,cAAc;EAC1C,OAAO,SAAS;EAChB,MAAM,gBAAgB;EAGtB,MAAM,eAAe,eAAe,QAAQ,CAAqC;CACnF;CAEA,OAAO;AAGT"}