const { connect } = require('preact-redux');

const Menu = require('./menu.component');
const actions = require('./../actions');

const addState = connect(
    function receiveState(state) {
        const { ws, cryps, selectedCryps, instances } = state;

        function sendInstanceJoin() {
            if (selectedCryps.length) {
                return ws.sendInstanceJoin(selectedCryps);
            }
            return false;
        }

        function sendPlayerMmCrypsSet() {
            if (selectedCryps.length) {
                return ws.sendPlayerMmCrypsSet(selectedCryps);
            }
            return false;
        }

        function sendCrypSpawn(name) {
            return ws.sendCrypSpawn(name);
        }

        function sendPlayerState(instance) {
            return ws.sendPlayerState(instance.instance);
        }

        return {
            cryps,
            selectedCryps,
            sendInstanceJoin,
            sendPlayerState,
            sendCrypSpawn,
            sendPlayerMmCrypsSet,
            instances,
        };
    },

    function receiveDispatch(dispatch) {
        function setSelectedCryps(crypIds) {
            dispatch(actions.setSelectedCryps(crypIds));
        }

        return {
            setSelectedCryps,
        };
    }
);

module.exports = addState(Menu);
