/**
 * Trade Metrics Generators Definitions
 *
 * This file contains generator functions for each block defined in
 * trade_metrics_blocks.js. Each generator translates a Blockly block into a JSON
 * object representing trade metrics retrieval operations.
 */

export function defineTradeMetricsGenerators() {

    // 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;
    }

    /**
     * Generator for 'active_trades' Block
     */
    Blockly.JSON['active_trades'] = function(block) {
        const operationObject = Blockly.JSON.createOperationObject('active_trades', {});

        // Handle additional values
        const outputValues = [operationObject];
        const valuesArray = Blockly.JSON.processValuesInput(block);
        if (valuesArray.length > 0) {
            outputValues.push(...valuesArray);
        }

        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };

    /**
     * Generator for 'total_trades' Block
     */
    Blockly.JSON['total_trades'] = function(block) {
        const operationObject = Blockly.JSON.createOperationObject('total_trades', {});

        // Handle additional values
        const outputValues = [operationObject];
        const valuesArray = Blockly.JSON.processValuesInput(block);
        if (valuesArray.length > 0) {
            outputValues.push(...valuesArray);
        }

        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };

    /**
     * Generator for 'last_trade_details' Block
     */
    Blockly.JSON['last_trade_details'] = function(block) {
        const details = block.getFieldValue('DETAILS');
        const validDetails = ['price', 'volume', 'direction'];
        const validatedDetails = validDetails.includes(details) ? details : 'price';

        const operationObject = Blockly.JSON.createOperationObject('last_trade_details', {
            details: validatedDetails
        });

        // Handle additional values
        const outputValues = [operationObject];
        const valuesArray = Blockly.JSON.processValuesInput(block);
        if (valuesArray.length > 0) {
            outputValues.push(...valuesArray);
        }

        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };

    /**
     * Generator for 'average_entry_price' Block
     */
    Blockly.JSON['average_entry_price'] = function(block) {
        const operationObject = Blockly.JSON.createOperationObject('average_entry_price', {});

        // Handle additional values
        const outputValues = [operationObject];
        const valuesArray = Blockly.JSON.processValuesInput(block);
        if (valuesArray.length > 0) {
            outputValues.push(...valuesArray);
        }

        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };

    /**
     * Generator for 'unrealized_profit_loss' Block
     */
    Blockly.JSON['unrealized_profit_loss'] = function(block) {
        const operationObject = Blockly.JSON.createOperationObject('unrealized_profit_loss', {});

        // Handle additional values
        const outputValues = [operationObject];
        const valuesArray = Blockly.JSON.processValuesInput(block);
        if (valuesArray.length > 0) {
            outputValues.push(...valuesArray);
        }

        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };

    /**
     * Generator for 'user_active_trades' Block
     */
    Blockly.JSON['user_active_trades'] = function(block) {
        const operationObject = Blockly.JSON.createOperationObject('user_active_trades', {});

        // Handle additional values
        const outputValues = [operationObject];
        const valuesArray = Blockly.JSON.processValuesInput(block);
        if (valuesArray.length > 0) {
            outputValues.push(...valuesArray);
        }

        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };
}
