#include "..\script_component.hpp"

/*
 * File: fnc_initLockerStore.sqf
 * Author: IDSolutions
 * Date: 2025-12-17
 * Last Update: 2026-01-30
 * Public: No
 *
 * Description:
 * Initializes the Locker store for managing player locker items.
 * Provides methods for syncing, saving, and applying locker items to the player's locker.
 *
 * Arguments:
 * None
 *
 * Return Value:
 * Locker store object [HASHMAP OBJECT]
 *
 * Example:
 * call forge_server_locker_fnc_initLockerStore
 */

#pragma hemtt ignore_variables ["_self"]
GVAR(LockerRepository) = compileFinal createHashMapFromArray [
    ["#base", EGVAR(common,BaseStore)],
    ["#type", "LockerRepository"],
    ["#create", {
        GVAR(LockerRegistry) = createHashMap;
        ["INFO", "Locker Repository Initialized!"] call EFUNC(common,log);
    }],
    ["get", {
        params [["_uid", "", [""]]];

        private _cached = GVAR(LockerRegistry) getOrDefault [_uid, nil];
        if !(isNil { _cached }) exitWith { _cached };

        ["locker:exists", [_uid]] call EFUNC(extension,extCall) params ["_result", "_isSuccess"];
        if !(_isSuccess) exitWith {
            ["ERROR", format ["Failed to check if locker %1 exists!", _uid]] call EFUNC(common,log);
            createHashMap
        };

        private _finalLocker = createHashMap;

        if (_result == "true") then {
            _finalLocker = _self call ["fetch", ["locker:get", _uid]];
            ["INFO", format ["Found locker for %1", _uid]] call EFUNC(common,log);
        } else {
            ["locker:create", [_uid]] call EFUNC(extension,extCall) params ["_result", "_isSuccess"];
            if !(_isSuccess) exitWith {
                ["ERROR", format ["Failed to create locker for %1!", _uid]] call EFUNC(common,log);
                createHashMap
            };

            ["INFO", format ["Created new locker for %1", _uid]] call EFUNC(common,log);
        };

        GVAR(LockerRegistry) set [_uid, _finalLocker];
        _finalLocker
    }]
];

GVAR(LockerStore) = createHashMapObject [[
    ["#base", GVAR(LockerRepository)],
    ["#type", "LockerStore"],
    ["#create", {
        ["INFO", "Locker Store Initialized!"] call EFUNC(common,log);
    }],
    ["init", {
        params [["_uid", "", [""]]];

        private _locker = _self call ["get", [_uid]];
        private _player = [_uid] call EFUNC(common,getPlayer);

        [CRPC(locker,responseInitLocker), [_locker], _player] call CFUNC(targetEvent);
    }]
]];

GVAR(LockerStore)
