// market_data_blocks.js

/**
 * Market Data Subcategory Blocks Definitions
 *
 * This file defines a set of market data-related blocks for Blockly. These blocks
 * enable users to retrieve various price metrics such as current price, bid price,
 * and ask price for specified symbols. The use of "dynamic_value" types allows for
 * flexible data retrieval without the need for chaining, enhancing the modularity
 * and interconnectivity of the Blockly environment.
 *
 * Ensure that this file is loaded **after** the `json_base_generator.js` and any other
 * necessary Blockly setup files to maintain proper loading order and functionality.
 */
export function defineMarketDataBlocks() {
    // Generate dropdown options based on available data
    const timeframeOptions = bt_data.intervals.map(interval => [interval, interval]);
    const exchangeOptions = window.UI.exchanges.connected_exchanges.map(exchange => [exchange, exchange]);
    const symbolOptions = bt_data.symbols.map(symbol => [symbol, symbol]);

    /**
     * current_price Block
     *
     * Description:
     * Retrieves the current market price for a specified symbol. Connect to a 'source'
     * block to define the symbol whose price is to be fetched.
     * If no source is provided, it defaults to the interface settings.
     */
    Blockly.defineBlocksWithJsonArray([{
        "type": "current_price",
        "message0": "Current Price %1",
        "args0": [
            {
                "type": "input_value",
                "name": "source",
                "check": "dynamic_value",
                "align": "RIGHT",
                "tooltip": "Provide a source or leave empty to use defaults."
            }
        ],
        "output": "dynamic_value",
        "colour": 230,
        "tooltip": "Retrieve the current market price for the specified symbol. If no source is provided, defaults to the interface settings.",
        "helpUrl": "https://example.com/docs/current_price"
    }]);

    /**
     * bid_price Block
     *
     * Description:
     * Retrieves the current bid price for a specified symbol. Connect to a 'source'
     * block to define the symbol whose bid price is to be fetched.
     * If no source is provided, it defaults to the interface settings.
     */
    Blockly.defineBlocksWithJsonArray([{
        "type": "bid_price",
        "message0": "Bid Price %1",
        "args0": [
            {
                "type": "input_value",
                "name": "source",
                "check": "dynamic_value",
                "align": "RIGHT",
                "tooltip": "Provide a source or leave empty to use defaults."
            }
        ],
        "output": "dynamic_value",
        "colour": 230,
        "tooltip": "Retrieve the current bid price for the specified symbol. If no source is provided, defaults to the interface settings.",
        "helpUrl": "https://example.com/docs/bid_price"
    }]);

    /**
     * ask_price Block
     *
     * Description:
     * Retrieves the current ask price for a specified symbol. Connect to a 'source'
     * block to define the symbol whose ask price is to be fetched.
     * If no source is provided, it defaults to the interface settings.
     */
    Blockly.defineBlocksWithJsonArray([{
        "type": "ask_price",
        "message0": "Ask Price %1",
        "args0": [
            {
                "type": "input_value",
                "name": "source",
                "check": "dynamic_value",
                "align": "RIGHT",
                "tooltip": "Provide a source or leave empty to use defaults."
            }
        ],
        "output": "dynamic_value",
        "colour": 230,
        "tooltip": "Retrieve the current ask price for the specified symbol. If no source is provided, defaults to the interface settings.",
        "helpUrl": "https://example.com/docs/ask_price"
    }]);

    /**
     * last_candle_value Block
     *
     * Description:
     * Retrieves a specific part of the last candle (Open, High, Low, Close) from a given source.
     * Generates a JSON object with type, candle_part, and source.
     */
    Blockly.defineBlocksWithJsonArray([{
        "type": "last_candle_value",
        "message0": "Last candle %1 value %2",
        "args0": [
            {
                "type": "field_dropdown",
                "name": "candle_part",
                "options": [
                    ["Open", "open"],
                    ["High", "high"],
                    ["Low", "low"],
                    ["Close", "close"]
                ],
                "tooltip": "Select which part of the candle to retrieve."
            },
            {
                "type": "input_value",
                "name": "source",
                "check": "dynamic_value",
                "align": "RIGHT",
                "tooltip": "Provide a source or leave empty to use defaults."
            }
        ],
        "output": "dynamic_value",
        "colour": 230,
        "tooltip": "Retrieve the specified part (Open, High, Low, Close) of the last candle from the defined data source.",
        "helpUrl": "https://example.com/docs/last_candle_value"
    }]);

    /**
     * source Block
     *
     * Description:
     * Defines the data source based on Time Frame (TF), Exchange (Ex), and Symbol (Sym).
     * This source can be used with blocks like 'last_candle_value' to specify where to retrieve data from.
     */
    Blockly.defineBlocksWithJsonArray([{
        "type": "source",
        "message0": "src: TF %1 Ex %2 Sym %3 %4",
        "args0": [
            {
                "type": "field_dropdown",
                "name": "TF",
                "options": timeframeOptions,
                "align": "RIGHT",
                "tooltip": "Select the Time Frame."
            },
            {
                "type": "field_dropdown",
                "name": "EXC",
                "options": exchangeOptions,
                "align": "RIGHT",
                "tooltip": "Select the Exchange."
            },
            {
                "type": "field_dropdown",
                "name": "SYM",
                "options": symbolOptions,
                "align": "RIGHT",
                "tooltip": "Select the Symbol."
            },
            {
                "type": "input_value",
                "name": "VALUES",
                "check": "dynamic_value",
                "align": "RIGHT"
            }
        ],
        "output": "dynamic_value",  // Changed from 'source' to 'dynamic_value' to support lists
        "colour": 230,
        "tooltip": "Select the data feed source by specifying Time Frame, Exchange, and Symbol. Use this source with blocks like 'Last Candle Value', 'Current Price', 'Bid Price', and 'Ask Price'.",
        "helpUrl": "https://example.com/docs/source"
    }]);
}
