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

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

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

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

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

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

        function sendVboxCombine() {
            return console.log('combine', combiner);
            return ws.sendVboxDiscard(instance.instance);
        }

        function sendVboxReclaim(v) {
            return console.log('reclaim', v);
            return ws.sendVboxDiscard(instance.instance);
        }

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

    function receiveDispatch(dispatch, { combiner }) {
        function addToCombiner(v) {
            combiner.push(v);
            if (combiner.length > 3) {
                dispatch(actions.setCombiner([v]));
            }
            dispatch(actions.setCombiner(combiner));
        }

        return { addToCombiner };
    }

);

module.exports = addState(Vbox);
