// makeSelectLocationState expects a plain JS object for the routing state
import { createSelector } from 'reselect';
import { fromJS } from 'immutable';
import _ from 'lodash';
const selectCapDomain = (state) => state.get('cap');

const makeSelectAuthenticated = () => createSelector(
  selectCapDomain,
  (globalState) => globalState.toJS()
);

const makeSelectMessage = () => createSelector(
  selectCapDomain,
  (globalState) => globalState.get('messages').toJS()
);

const makeSelectMetaEntities = () => createSelector(
  selectCapDomain,
  (globalState) => globalState.get('metaEntities')
);

const isLoadingMetaEntities = () => createSelector(
  selectCapDomain,
  (globalState) => globalState.get('fetchingSchema')
);
const setInjectedTags = () => createSelector(
  selectCapDomain,
  (globalState = fromJS({})) => {
    let injectedTags = globalState.get('injectedTags') || {};
    const metaEntities = globalState.get('metaEntities') || {};
    const { tags } = metaEntities;
    const customTags = tags && (tags.custom || {});
    const extendedTags = tags && (tags.extended || {});
    const loyaltyTags = tags && (tags.loyaltyTags || {});
    return (injectedTags = _.merge({}, injectedTags, customTags, extendedTags, loyaltyTags));
  }
);

// const makeSelectLocationState = () => {
//   let prevRoutingState;
//   let prevRoutingStateJS;

//   return (state) => {
//     const routingState = state.get('route'); // or state.route

//     if (!routingState.equals(prevRoutingState)) {
//       prevRoutingState = routingState;
//       prevRoutingStateJS = routingState.toJS();
//     }

//     return prevRoutingStateJS;
//   };
// };

const makeSelectOrgId = () => createSelector(
  selectCapDomain,
  (globalState) => globalState.get('orgID')
);

const makeSelectUser = () => createSelector(
  selectCapDomain,
  (globalState) => globalState.get('user')
);
const selectCurrentOrgDetails = () => createSelector(
  selectCapDomain,
  (globalState) => {
    const currentOrgDetails = globalState.get('currentOrgDetails');
    return currentOrgDetails ? currentOrgDetails.toJS() : null;
  }
);
const selectLiquidStateDetails = () => createSelector(
  selectCapDomain,
  (globalState) => globalState.get('fetchingLiquidTags')
);

const selectMetaDataStatus = () => createSelector(
  selectCapDomain,
  (globalState) => globalState.get('metaDataStatus')
);

const makeSelectFetchingSchema = () => createSelector(
  selectCapDomain,
  (globalState) => globalState.get('fetchingSchema')
);

const makeSelectFetchingSchemaError = () => createSelector(
  selectCapDomain,
  (globalState) => globalState.get('fetchingSchemaError')
);

const makeSelectLoyaltyPromotionDisplay = () => createSelector(
  selectCapDomain,
  (globalState) => ({
    disableRelatedTags: globalState.get('disableLoyaltyPromotionRelatedTags'),
    childTagsToDisable: globalState.get('tagsToDisable') || [],
    parentTagstoDisable: globalState.get('parentTagsToDisable') || [],
  }),
);

const makeSelectDemoVideoAndLink = () =>
  createSelector(selectCapDomain, (substate) => ({
    demoVideoAndLinkJSON: substate.get('demoVideoAndLinkJSON'),
    demoVideoAndLinkJSONStatus: substate.get('demoVideoAndLinkJSONStatus'),
    demoVideoAndLinkJSONError: substate.get('demoVideoAndLinkJSONError'),
  }));

export {
  // makeSelectLocationState,
  makeSelectAuthenticated,
  makeSelectMessage,
  makeSelectOrgId,
  makeSelectMetaEntities,
  makeSelectUser,
  selectCurrentOrgDetails,
  isLoadingMetaEntities,
  makeSelectFetchingSchema,
  setInjectedTags,
  makeSelectLoyaltyPromotionDisplay,
  makeSelectFetchingSchemaError,
  makeSelectDemoVideoAndLink,
  selectLiquidStateDetails,
  selectMetaDataStatus,
};
