import * as i2 from '@ngrx/store';
import { createAction, props, createReducer, on, createFeatureSelector, createSelector } from '@ngrx/store';
import { createEntityAdapter } from '@ngrx/entity';
import * as i0 from '@angular/core';
import { PLATFORM_ID, Injectable, Inject } from '@angular/core';
import * as i1 from '@ngrx/effects';
import { createEffect, ofType } from '@ngrx/effects';
import { withLatestFrom, tap } from 'rxjs/operators';
import { LocalStorageKeysEnum } from '@testlocallibrary/shared-kernel/models/enums';
import { isPlatformBrowser } from '@angular/common';

const addToCart = createAction('[Cart] Add Product', props());
const addManyToCart = createAction('[Cart] Add Many Products', props());
const removeFromCart = createAction('[Cart] Remove from Cart', props());
const updateCartItem = createAction('[Cart] Update Cart Item', props());
const resetCart = createAction('[Cart] Reset Cart');
const setAllItems = createAction('[Cart] Set All Items', props());

const adapter = createEntityAdapter();
const initialState = adapter.getInitialState();
const cartReducer = createReducer(initialState, on(addToCart, (state, { product }) => adapter.addOne(product, state)), on(addManyToCart, (state, { products }) => adapter.addMany(products, state)), on(updateCartItem, (state, { productId, updates }) => adapter.updateOne({ id: productId, changes: updates }, state)), on(removeFromCart, (state, { productId }) => adapter.removeOne(productId, state)), on(resetCart, () => initialState), on(setAllItems, (state, { items }) => Array.isArray(items) ? adapter.setAll(items, state) : state));

const selectCartState = createFeatureSelector('cart');
const { selectAll } = adapter.getSelectors();
const selectCartItems = createSelector(selectCartState, selectAll);
const selectCartItemCount = createSelector(selectCartItems, (cartItems) => cartItems?.length);

class CartEffects {
    constructor(actions$, store, platformId) {
        this.actions$ = actions$;
        this.store = store;
        this.platformId = platformId;
        this.updateLocalStorage$ = createEffect(() => this.actions$.pipe(ofType(addToCart, addManyToCart, removeFromCart, resetCart, updateCartItem), withLatestFrom(this.store.select(selectCartItems)), tap(([action, cartItems]) => {
            if (isPlatformBrowser(this.platformId)) {
                localStorage.setItem(LocalStorageKeysEnum.Cart, JSON.stringify({ cart: cartItems }));
            }
        })), { dispatch: false });
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CartEffects, deps: [{ token: i1.Actions }, { token: i2.Store }, { token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CartEffects }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CartEffects, decorators: [{
            type: Injectable
        }], ctorParameters: function () { return [{ type: i1.Actions }, { type: i2.Store }, { type: Object, decorators: [{
                    type: Inject,
                    args: [PLATFORM_ID]
                }] }]; } });

/**
 * Generated bundle index. Do not edit.
 */

export { CartEffects, adapter, addManyToCart, addToCart, cartReducer, initialState, removeFromCart, resetCart, selectAll, selectCartItemCount, selectCartItems, setAllItems, updateCartItem };
//# sourceMappingURL=testlocallibrary-shared-kernel-store.mjs.map
