/**
 * Balances Generators Definitions
 *
 * This file contains generator functions for each block defined in
 * balances_blocks.js. Each generator translates a Blockly block
 * into a JSON object representing balance-related operations.
 */

export function defineBalancesGenerators() {

    // Ensure the base generator is initialized and 'createOperationObject' is available
    if (!Blockly.JSON || !Blockly.JSON.createOperationObject) {
        console.error("Blockly.JSON or createOperationObject not initialized. Please ensure 'json_base_generator.js' is loaded first.");
        return;
    }

    /**
     * Helper Function: processValuesInput
     * Processes the 'VALUES' input and returns an array of values.
     *
     * @param {Blockly.Block} block - The block containing the 'VALUES' input.
     * @returns {Array} - An array of values from the 'VALUES' input.
     */
    function processValuesInput(block) {
        const valuesInputBlock = block.getInputTargetBlock('VALUES');
        let values = [];

        if (valuesInputBlock) {
            const valuesJson = Blockly.JSON.blockToCode(valuesInputBlock);

            if (Array.isArray(valuesJson)) {
                values = valuesJson;
            } else if (valuesJson !== null && valuesJson !== undefined) {
                values = [valuesJson];
            }
        }

        return values;
    }

    /**
     * Generator for 'starting_balance' Block
     */
    Blockly.JSON['starting_balance'] = function(block) {
        const valuesArray = processValuesInput(block);

        // Create operation object
        const operationObject = Blockly.JSON.createOperationObject('starting_balance', {});

        // Include unused values if any
        const outputValues = [operationObject, ...valuesArray];

        // Output as dynamic_value
        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };

    /**
     * Generator for 'current_balance' Block
     */
    Blockly.JSON['current_balance'] = function(block) {
        const valuesArray = processValuesInput(block);

        // Create operation object
        const operationObject = Blockly.JSON.createOperationObject('current_balance', {});

        // Include unused values if any
        const outputValues = [operationObject, ...valuesArray];

        // Output as dynamic_value
        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };

    /**
     * Generator for 'available_balance' Block
     */
    Blockly.JSON['available_balance'] = function(block) {
        const valuesArray = processValuesInput(block);

        // Create operation object
        const operationObject = Blockly.JSON.createOperationObject('available_balance', {});

        // Include unused values if any
        const outputValues = [operationObject, ...valuesArray];

        // Output as dynamic_value
        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };

    /**
     * Generator for 'available_strategy_balance' Block
     */
    Blockly.JSON['available_strategy_balance'] = function(block) {
        const valuesArray = processValuesInput(block);

        // Create operation object
        const operationObject = Blockly.JSON.createOperationObject('available_strategy_balance', {});

        // Include unused values if any
        const outputValues = [operationObject, ...valuesArray];

        // Output as dynamic_value
        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };

    /**
     * Generator for 'set_available_strategy_balance' Block
     *
     * This is a statement block, so it generates an action object and handles 'NEXT' connections.
     */
    Blockly.JSON['set_available_strategy_balance'] = function(block) {

        const values = Blockly.JSON.processNumericInput(block,'BALANCE',0);

        const balance = values !== undefined ? values : 0;

        const json = {
            type: 'set_available_strategy_balance',
            inputs: { BALANCE: balance },
//            skipAdditionalParsing: true
        };

        // No need to handle 'NEXT' input; sequencing is managed by _blockToJson

        return json; // Return the action object directly
    };
}
