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, account } = state;

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

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

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

        function sendInstanceState(instance) {
            return ws.sendInstanceState(instance.id);
        }

        function sendInstanceList() {
            return ws.sendAccountInstances();
        }

        return {
            account,
            cryps,
            selectedCryps,
            sendInstanceJoin,
            sendInstanceState,
            sendInstanceList,
            sendCrypSpawn,
            sendPlayerMmCrypsSet,
            instances,
        };
    },

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

        return {
            setSelectedCryps,
        };
    }
);

module.exports = addState(Menu);
