import reduxCrud from 'lib/redux-crud';
import moment from "moment";

import { fetchRequest, createRequest, updateRequest, deleteRequest } from 'lib/crud-backend';
import { settingsSelector } from 'utils/settings';
import { injectPeriodParams } from 'containers/Params';


export const reduxKey = "incomeDetail";

//build API
let api = reduxCrud(reduxKey, { 
	prepareFunction: (unit) => {
		let m = moment();
		return {
			currency: unit.currency,
			currencySymbol: unit.currencySymbol,

			subjectName: unit.name,
			subjectIdNumber: unit.idNumber,
			subjectTaxNumber: unit.taxNumber,
			subjectAddress: unit.address,

			incomeDate: m.format("YYYY-MM-DD"),

			totalAmount: 0,
			totalPayable: 0
		}
	},
	fetchRequest: fetchRequest(({ unitId, id }) => `/api/${unitId}/income/${id}`),
	createRequest: createRequest(({ unitId }) => `/api/${unitId}/income`),
	updateRequest: updateRequest(({ unitId, id }) => `/api/${unitId}/income/${id}`),
	deleteRequest: deleteRequest(({ unitId, id }) => `/api/${unitId}/income/${id}`)
}); 

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

export const prepareEntity = api.actions.prepareEntity;

export const saveEntity = api.actions.saveEntity;

export const deleteEntity = api.actions.deleteEntity;

export const selector = settingsSelector(injectPeriodParams(api.selector));

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