import React from 'react';

export function stateReducer(state={active:'',visited:''}, action) {
    console.log(action.type)
    switch (action.type) {
    case action.type:
        return Object.assign({}, state, { 
            active: action.data.active,
            visited:action.data.visited,
            collapse:action.data.collapse
        });
    }
}

export default function handleBreadCrumbAction(initialValue,applyHook) {
    let [state,dispatchVal]=React.useReducer(stateReducer,{
        active:initialValue.active,
        visited:initialValue.active,
        collapse:initialValue.collapse
    })
    function onSelect(e,type,action){
        let modifiedState = {...state};
        modifiedState.active=e
        modifiedState.visited=e
        dispatchVal({type,data:modifiedState},action && action())
        if(applyHook){
            let newState=applyHook(state,modifiedState,type);
            dispatchVal({type,data:newState},action && action())
        }
    }
    
    function onCollapse(e,type,action){
        let modifiedState = {...state};
        modifiedState.collapse=!state.collapse
        dispatchVal({type,data:modifiedState},action && action())
        if(applyHook){
            let newState=applyHook(state,modifiedState,type);
            dispatchVal({type,data:newState},action && action())
        }
    }
    console.log(state,'state');
    return [state.active,state.visited,onSelect,state.collapse,onCollapse]
}