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

const actions = require('../actions');

const Vbox = require('./vbox.component');

const addState = connect(
    function receiveState(state) {
        const { ws, instance, combiner, reclaiming, activeVar } = state;

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

        function sendVboxAccept(group, index) {
            return ws.sendVboxAccept(instance.instance, group, index);
        }

        function sendVboxCombine() {
            return ws.sendVboxCombine(instance.instance, combiner);
        }

        function sendVboxReclaim(i) {
            return ws.sendVboxReclaim(instance.instance, i);
        }

        return {
            instance,
            combiner,
            reclaiming,
            activeVar,
            sendVboxAccept,
            sendVboxDiscard,
            sendVboxReclaim,
            sendVboxCombine,
        };
    },

    function receiveDispatch(dispatch) {
        function setCombiner(c) {
            return dispatch(actions.setCombiner(c));
        }

        function setReclaiming(v) {
            return dispatch(actions.setReclaiming(v));
        }

        function setInfo(type, info) {
            return dispatch(actions.setInfo([type, info]));
        }

        function setActiveVar(v) {
            return dispatch(actions.setActiveVar(v));
        }

        return {
            setCombiner,
            setReclaiming,
            setInfo,
            setActiveVar,
        };
    }

);

module.exports = addState(Vbox);
