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

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

const Instance = require('./instance.component');

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

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

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

        return { instance, account, sendInstanceReady, sendVboxApply, activeVar };
    },

    function receiveDispatch(dispatch, { instance, combiner }) {
        function quit() {
            dispatch(actions.setInstance(null));
        }

        function setInfo(item, value) {
            dispatch(actions.setInfo([item, value]));
        }

        function setActiveVar(value) {
            dispatch(actions.setActiveVar(value));
        }

        return { quit, setInfo, setActiveVar };
    }

);

module.exports = addState(Instance);
