#include "..\script_component.hpp"

/*
 * Function: forge_client_db_fnc_saveToTempDB
 * Author: J. Schmidt
 * 
 * Description:
 * Saves data to temporary mission storage (not persisted between sessions)
 *
 * Arguments:
 * 0: _collectionName - Collection name <STRING>
 * 1: _data - Data to save <HASHMAP, ARRAY, ANY>
 * 2: _key - Key to save under <STRING>
 *
 * Return Value:
 * Success <BOOL>
 */

params [
    ["_collectionName", "", [""]],
    ["_data", nil, [createHashMap, [], "", 0, true]],
    ["_key", "", [""]]
];

private _database = call FUNC(verifyDB);
private _collection = _database call ["getCollection", [_collectionName]];

if (isNil "_collection") then {
    _collection = _database call ["createCollection", [_collectionName, []]];
};

private _tempStorageVar = QGVAR(tempDB);
private _tempStorage = missionNamespace getVariable [_tempStorageVar, createHashMap];
private _tempCollection = _tempStorage getOrDefault [_collectionName, createHashMap];

_tempCollection set [_key, _data];
_tempStorage set [_collectionName, _tempCollection];
missionNamespace setVariable [_tempStorageVar, _tempStorage];

true