#include "..\script_component.hpp"

/*
 * File: fnc_extCall.sqf
 * Author: IDSolutions
 * Date: 2026-01-03
 * Last Update: 2026-01-03
 * Public: No
 *
 * Description:
 *     Call Forge Server extension.
 *
 * Parameter(s):
 *     0: Function name to call <STRING>
 *     1: Arguments to pass to function <ARRAY>
 *
 * Returns:
 *     Extension result <ARRAY>
 *     Success <BOOL>
 *
 * Example(s):
 *     ["icom:connect", ["127.0.0.1:9090", "server_1"]] call forge_x_component_fnc_extCall params ["_result", "_isSuccess"];
 */

params [["_function", "", [""]], ["_arguments", [], [[]]]];

["INFO", format ["Calling function: %1", _function], nil, nil] call EFUNC(common,log);
("forge_server" callExtension [_function, _arguments]) params ["_result", "_extCode", "_armaCode"];

private _success = true;

if (_armaCode != 0 && _armaCode != 301) then {
    _success = false;
    private _armaCodeMessage = createHashMapFromArray [
        [101, "SYNTAX_ERROR_WRONG_PARAMS_SIZE"],
        [102, "SYNTAX_ERROR_WRONG_PARAMS_TYPE"],
        [201, "PARAMS_ERROR_TOO_MANY_ARGS"],
        // [301, "EXECUTION_WARNING_TAKES_TOO_LONG"],
        [400, "EXTENSION_LOAD_FAILED"],
        [403, "EXTENSION_BLOCKED_BY_BATTLEYE"],
        [404, "EXTENSION_NOT_FOUND"]
    ] getOrDefault [_armaCode, format ["UNKNOWN_%1", _armaCode]];
    ["WARNING", format ["Arma error: %1", _armaCodeMessage], nil, nil] call EFUNC(common,log);
};

if (_extCode != 0) then {
    _success = false;
    if (_extCode == -1) exitWith { ["WARNING", "Extension not available", nil, nil] call EFUNC(common,log); };
    if (_extCode == 9) exitWith { ["WARNING", format ["Extension error: %1", _result], nil, nil] call EFUNC(common,log); };

    ["WARNING", format ["Extension error: %1", _extCode], nil, nil] call EFUNC(common,log);
};

[_result, _success]
