import reduxCrud from 'lib/redux-crud';
import { fetchRequest, updateRequest } from 'lib/crud-backend';
import { settingsSelector } from 'utils/settings';

import { unitI18n } from "utils/settings";

export const reduxKey = "settingsLocale";

//build API
let api = reduxCrud(reduxKey, { 
    fetchRequest: fetchRequest(({ unitId, id }) => `/api/${unitId}/settings/local`),
    updateRequest: updateRequest(({ unitId, id }) => `/api/${unitId}/settings/local`),

    onSaveSuccess: (state, action) => {
        let unit = action.payload.body;
        //TODO
        let i18n = unitI18n(unit);
        global.i18n = i18n;
        

        return state;
    }
}); 

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

export const saveEntity = api.actions.saveEntity;

export const selector = api.selector;

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