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



