import app from 'store/app';
import account from 'store/account';
import auth from 'store/auth';
import file from 'store/file';
import workspace from 'store/workspace';
import messagerie from 'store/messagerie';
import booking from 'store/booking';

import camelCase from 'lodash/camelCase';

import get from 'lodash/get';

const actionsBuilder = (dispatch, actions: string[]) => {
  const resources = [app, account, file, workspace, auth, messagerie, booking];

  const actionsList = {};

  resources.forEach(resource => {
    actions.forEach(action => {
      let resourceAction = get(resource, `actions.${action}.action`);
      const resourcePrefix = get(resource, `constants.prefix`);
      if (!resourceAction) resourceAction = get(resource, `actions.${action}.request.action`);

      if (resourceAction && resourcePrefix) {
        actionsList[camelCase(`${resourcePrefix}.${action}`)] = data =>
          dispatch(resourceAction(data));
      }
    });
  });

  return actionsList;
};

export default actionsBuilder;
