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

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

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

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

        function sendPlayerMmCrypsSet() {
            if (team.length) {
                return ws.sendPlayerMmCrypsSet(team);
            }
            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,
            team,
            sendInstanceJoin,
            sendInstanceState,
            sendInstanceList,
            sendCrypSpawn,
            sendPlayerMmCrypsSet,
            instances,
        };
    },

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

        return {
            setTeam,
        };
    }
);

module.exports = addState(Menu);
