import { getSelectedDesignArea } from './Selectors';
import { selectDesignView } from '../DesignView/Actions';
import { createActionWithGlobalState } from '../../../Utils/Redux/createActionWithGlobalState';

export const actionTypes = {
    SELECT_DESIGN_AREA: 'DesignArea/SELECT_DESIGN_AREA',
    CLEAR_DESIGN_AREA_SELECTION: 'DesignArea/CLEAR_DESIGN_AREA_SELECTION',
    TOGGLE_HIGHLIGHT: 'DesignArea/TOGGLE_HIGHLIGHT',
    SET_PREVIOUS_EDITED_DESIGN_AREA: 'DesignArea/SET_PREVIOUS_EDITED_DESIGN_AREA',
    RECALCULATE_DESIGN_AREA: 'DesignArea/RECALCULATE_DESIGN_AREA',
};

export const selectDesignArea = designAreaIdentifier => (dispatch, getState) => {
    dispatch({
        type: actionTypes.SELECT_DESIGN_AREA,
        designAreaIdentifier,
    });
    const designArea = getSelectedDesignArea(getState());
    dispatch(selectDesignView(designArea.designView.identifier));
};

export const clearDesignAreaSelection = (force = false) => (dispatch, getState) => {
    const notEmpty = Object.keys(getState().designArea.selectedDesignArea).length > 0;
    if (force || notEmpty) {
        dispatch({
            type: actionTypes.CLEAR_DESIGN_AREA_SELECTION,
            force,
        });
    }
};

export const setPreviousEditedDesignAreaIdentier = designAreaIdentifier => ({
    type: actionTypes.SET_PREVIOUS_EDITED_DESIGN_AREA,
    designAreaIdentifier,
});

export const toggleHighlight = showHighlight => ({
    type: actionTypes.TOGGLE_HIGHLIGHT,
    showHighlight,
});

export const recalculateDesignArea = () =>
    createActionWithGlobalState({
        type: actionTypes.RECALCULATE_DESIGN_AREA,
    });
