import { HYDRATE } from 'next-redux-wrapper';

import * as actionTypes from '../constants/actionTypes';
import initialState from "./initialState";

const dealsReducer = (state = initialState.deals, action) => 
{
	switch (action.type) 
	{
		case actionTypes.DEALS_LOADED: 
		{
			return {
				...state, 
				loaded: action.data.loaded, 
			};
		}

		case actionTypes.DEALS_LIST: 
		{
			console.log("actionTypes.DEALS_LIST", actionTypes.DEALS_LIST, { action });

			return {
				...state, 
				list: action.data.list, 
				last: action.data.last, 
			};
		}

		case actionTypes.DEAL_LOADED: 
		{
			return {
				...state, 
				deal: { ...state.deal, ...{ loaded: action.data.loaded, } }, 
			};
		}

		case actionTypes.DEAL_OFFERS_LIST: 
		{
			console.log("ACTION", actionTypes.DEAL_OFFERS_LIST, { action });

			const details = state.details;
			details[action.data.deal_id] = { ...details[action.data.deal_id], ...{ offers: action.data.list } };

			return {
				...state, 
				details: details, 
			};
		}

		case actionTypes.DEAL_DOCUMENTS_LIST: 
		{
			console.log("ACTION", actionTypes.DEAL_DOCUMENTS_LIST, { action });

			const details = state.details;
			details[action.data.deal_id] = { ...details[action.data.deal_id], ...{ documents: action.data.documents, uploaded: action.data.uploaded } };

			return {
				...state, 
				details: details, 
			};
		}

		case actionTypes.DEAL_CONTRACTS_LIST: 
		{
			const details = state.details;
			details[action.data.deal_id] = { ...details[action.data.deal_id], ...{ contracts: action.data.list } };

			return {
				...state, 
				details: details, 
			};
		}

		case actionTypes.DEALS_RESET: 
		{
			return {
				loaded: false, 
				list: undefined, 
				deal: {
					loaded: false, 
					offers: {
						list: undefined, 
						filtered: undefined, 
					}, 
					documents: {
						list: undefined, 
					}, 
					contracts: {
						list: undefined, 
					}, 
				}, 
			};
		}

		case actionTypes.DEAL_RESET: 
		{
			return {
				deal: {
					loaded: false, 
					offers: {
						list: undefined, 
						filtered: undefined, 
					}, 
					documents: {
						list: undefined, 
					}, 
					contracts: {
						list: undefined, 
					}, 
				}, 
			};
		}

		default: {
			return state;
		}
	}
};

export default dealsReducer;