import reduxList from 'lib/redux-list';
import { listRequest, listResponse } from 'lib/list-backend';
import { settingsSelector } from 'utils/settings';
import createReducer, { safe } from 'lib/reducer';
import backendRequest from 'lib/backend';


let reducer = createReducer({
    list: {
        fresh: true,
    },
    summary: {},
    suppliers: {}
});


//build API
let api = reduxList('expenseList', { 
    reducer,
	fetchRequest: listRequest(({ unitId }) => `/api/${unitId}/expenses`),
	fetchResponse: listResponse()
}); 


//export actions
export const fetchList = api.actions.fetchList;

export function fetchSummary(unitId, filter = {}) {
    return {
        type: 'expenseList/FETCH_SUMMARY',
        payload: backendRequest('/api/' + unitId + '/expenses/summary', 'GET', { params: { ...filter } })
    };
};

reducer.handleAction('expenseList/FETCH_SUMMARY_PENDING', (state, action) => 
    state.setIn(["summary", "status"], "pending")
);
reducer.handleAction('expenseList/FETCH_SUMMARY_FULFILLED', (state, action) => 
    state
        .setIn(["summary", "status"], "success")
        .setIn(["summary", "data"], action.payload.body)
);
reducer.handleAction('expenseList/FETCH_SUMMARY_REJECTED', (state, action) => 
    state.setIn(["summary", "status"], "failed")
);


export function fetchSuppliers(unitId, filter = {}) {
    return {
        type: 'expenseList/FETCH_SUPPLIERS',
        payload: backendRequest('/api/' + unitId + '/expenses/suppliers', 'GET', { params: { ...filter } })
    };
};

reducer.handleAction('expenseList/FETCH_SUPPLIERS_PENDING', (state, action) => 
    state.setIn(["suppliers", "status"], "pending")
);
reducer.handleAction('expenseList/FETCH_SUPPLIERS_FULFILLED', (state, action) => 
    state
        .setIn(["suppliers", "status"], "success")
        .setIn(["suppliers", "data"], action.payload.body)
);
reducer.handleAction('expenseList/FETCH_SUPPLIERS_REJECTED', (state, action) => 
    state.setIn(["suppliers", "status"], "failed")
);


export function fetchTimeline(unitId, filter = {}) {
    return {
        type: 'expenseList/FETCH_TIMELINE',
        payload: backendRequest('/api/' + unitId + '/expenses/timeline', 'GET', { params: { ...filter } })
    };
};

reducer.handleAction('expenseList/FETCH_TIMELINE_PENDING', (state, action) => 
    state.setIn(["timeline", "status"], "pending")
);
reducer.handleAction('expenseList/FETCH_TIMELINE_FULFILLED', (state, action) => 
    state
        .setIn(["timeline", "status"], "success")
        .setIn(["timeline", "data"], action.payload.body)
);
reducer.handleAction('expenseList/FETCH_TIMELINE_REJECTED', (state, action) => 
    state.setIn(["timeline", "status"], "failed")
);


//unit selector
export const selector = settingsSelector((state) => {
    if (!state || !state.get) {
        return {};
    }

    let periodParams = safe(state.getIn(["params", "period"]));
    let expenses = safe(state.get("expenseList"));

    return { ...expenses, periodParams };
});


//export reducer
export default reducer.export();
