import flow from 'lodash/flow';
import isArray from 'lodash/isArray';
import createSelector from '../../../Utils/Function/createSelector';
import get from 'lodash/get';

export const getConfigurationModes = item => {
    const configurationModes = item && item.configurationModes;
    if (!isArray(configurationModes)) {
        return {};
    }
    return configurationModes.reduce(
        (modeMap, mode) =>
            get(mode, 'itemStatus.item_available', false) ? { ...modeMap, [mode.identifier]: true } : modeMap,
        {}
    );
};

const getConfigurationModeState = ({ configurationMode }) => configurationMode;
const getSelectedMode = ({ selectedMode }) => selectedMode;
const getAvailableModes = ({ availableModes }) => availableModes;

export const getSelectedConfigurationMode = flow(getConfigurationModeState, getSelectedMode);

export const getAvailableConfigurationModes = flow(getConfigurationModeState, getAvailableModes);

export const getConfigurationModeOptions = createSelector(
    getAvailableConfigurationModes,
    getSelectedConfigurationMode,
    (availableModes, selectedMode) => Object.keys(availableModes).filter(mode => mode !== selectedMode)
);

export const isConfigurationModeAvailable = createSelector(
    getAvailableConfigurationModes,
    modules => Object.values(modules).length > 0
);

export const isConfigurationModeSwitchAvailable = createSelector(
    getAvailableConfigurationModes,
    modules => Object.values(modules).length > 1
);

export const isCreatorModeSelected = createSelector(getSelectedConfigurationMode, mode =>
    isCreatorMode(mode)
);

export const isDesignerMode = modeStr => modeStr === 'designer';

export const isCreatorMode = modeStr => modeStr === 'creator';

export const getAvailableLicenses = data =>
    (data && data.license && data.license.split('+').filter(a => a)) || [];
