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


        return { setCombiner, setReclaiming, setInfo };
    }

);

module.exports = addState(Vbox);
