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

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

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

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

        function sendPlayerMmConstructsSet() {
            if (team.length) {
                return ws.sendPlayerMmConstructsSet(team);
            }
            return false;
        }

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

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

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

        return {
            account,
            constructs,
            team,
            sendInstanceJoin,
            sendInstanceState,
            sendInstanceList,
            sendConstructSpawn,
            sendPlayerMmConstructsSet,
            instances,
        };
    },

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

        return {
            setTeam,
        };
    }
);

module.exports = addState(Menu);
