<template
    color="#d4af37"
    inline="true"
    prev="true"
    next="true"

    tooltip="Connect to WooCommerce system to start receiving product information."
>
    <dummy>
        <label>connect to WooCommerce</label>
    </dummy>

    <statement name="WHEN_READY">
        <label>once product updated do:</label>
    </statement>
</template>

<script>
function wrapStatements(statementsDo, arg) {
    arg = arg || '';
    if (statementsDo.length)
        return 'function(' + arg + ') {\n' + statementsDo + '}';
    else
        return 'function(' + arg + ') {}';
}

function template(block) {
    block.setHelpUrl(Plug.getPuzzlesDocUrl('ECommerce.html#woo_connect'));
}

function code(block) {
    const whenReadyCb = Blockly.JavaScript.statementToCode(block, 'WHEN_READY');

    const fun = Plug.provide('wooGetProductInfo', function(whenReadyCb) {
        // wooGetProductInfo puzzle
        let woo_fun;
        try {
            woo_fun = window.parent.v3d_woo_get_product_info
                || window.parent.parent.v3d_woo_get_product_info || null;
        } catch (e) {
            // blocked cross-origin access
        }

        if (woo_fun) {
            woo_fun(function(info) {
                _pGlob.wooProductInfo = info;
                whenReadyCb();
            });
        } else {
            console.error('connect to WooCommerce puzzle: WooCommerce not found');
        }
    });

    return `${fun}(${wrapStatements(whenReadyCb)});\n`;
}
</script>
