import {createStore, combineReducers, applyMiddleware, compose} from 'redux';
import thunk from 'redux-thunk';
import actionPromise from "./actionPromise";
import {gql, urlUpload} from "./gql";
import actionGoods from "./actionGoods"
import getData from "./getData";
import {actionSearch} from "./actionsearch";
import actionLogin from "./actionLogin";
import authReducer from "./authReducer";
import {actionAuthLogin, actionAuthLogout} from "./actionAuthLog";
import actionGoodCard from "./actionGoodCard";
import updateImgAction from "./actionUpdateImg";
import {cartReducer, actionCartClear, actionCartDelete, actionCartAdd} from "./cartReducer";
import actionOrder from "./actionOrder";
import actionReg from "./actionReg";
import actionUserFindOne from "./actionFindOneUser";
import actionUserUpdate from "./actionUserUpdate";
import actionSearchUser from "./actionSearchUser";
import actionGoodUpdate from "./actionGoodUpdate";
import {orderUpdateReducer, actionOrderClear, actionOrderAddNewGood, actionOrderAddOne, actionOrderAddOrder, actionOrderDelete} from "./orderUpdateReduser"
import {actionOrdersFind, actionOrderFindOne} from "./actionOrdersFind";
import actionOrderUpdate from "./actionUpdateOrder";
import {wishListReducer, actionWishAdd, actionWishDelete} from "./wishListReducer";
import actionWishes from "./actionWishSearch";


function promiseReducer(state={}, action){

    if (action.type === 'PROMISE'){
        const { name="default", status, payload, error} = action
        if (status){
            return {
                ...state, [name]: {status, payload: (status === 'PENDING' && state[name] && state[name].payload) || payload, error}
            }
        }
    }
    return state;
  }
  
  
  const store = createStore(combineReducers({
      promiseRed: promiseReducer, 
      auth: authReducer,
      basket: cartReducer,
      orderUpdateReducer,
      wishListReducer
  }), compose(applyMiddleware(thunk)))

export {actionPromise, actionWishes, actionOrdersFind,  gql, actionGoods, getData, promiseReducer, store, actionSearch,
   actionLogin, authReducer, actionAuthLogin, actionAuthLogout, urlUpload, actionGoodCard, 
   updateImgAction, actionCartAdd, actionOrderClear, actionCartDelete, actionCartClear, actionOrder,
    actionReg, actionOrderUpdate, actionUserFindOne, wishListReducer, actionWishAdd, actionWishDelete, actionOrderAddNewGood, actionOrderAddOne, orderUpdateReducer, actionOrderAddOrder, actionOrderDelete, actionOrderFindOne, actionUserUpdate, actionSearchUser, actionGoodUpdate};



