"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var StoreEntity_1 = require("../Core/StoreEntity");
var RouteOptimization_consts_1 = require("./RouteOptimization.consts");
var RouteOptimizationService = /** @class */ (function () {
    function RouteOptimizationService(session) {
        var _this = this;
        this.optimizationProcessCallbacks = {};
        this.registerToOptimization = function (uuid, callbacks) {
            if (Object.keys(_this.optimizationProcessCallbacks).length === 0) {
                _this.realTime.on(RouteOptimization_consts_1.OptimizationEvents.IN_PROGRESS, _this.onOptimizationInProgress);
                _this.realTime.on(RouteOptimization_consts_1.OptimizationEvents.DONE, _this.optimizationDone);
            }
            if (!_this.optimizationProcessCallbacks[uuid]) {
                _this.optimizationProcessCallbacks[uuid] = callbacks;
            }
        };
        this.unregisterFromOptimization = function (request_uuid) {
            delete _this.optimizationProcessCallbacks[request_uuid];
            if (Object.keys(_this.optimizationProcessCallbacks).length === 0) {
                _this.realTime.off(RouteOptimization_consts_1.OptimizationEvents.IN_PROGRESS, _this.onOptimizationInProgress);
                _this.realTime.off(RouteOptimization_consts_1.OptimizationEvents.DONE, _this.optimizationDone);
            }
        };
        this.onOptimizationInProgress = function (payload) {
            var _a;
            (_a = _this.optimizationProcessCallbacks[payload.requestUuid]) === null || _a === void 0 ? void 0 : _a.onInProgress(payload);
        };
        this.optimizationDone = function (payload) {
            var _a, _b;
            if (payload.error) {
                (_a = _this.optimizationProcessCallbacks[payload.request_uuid]) === null || _a === void 0 ? void 0 : _a.onFailed(payload);
            }
            else {
                (_b = _this.optimizationProcessCallbacks[payload.request_uuid]) === null || _b === void 0 ? void 0 : _b.onDone(payload);
            }
            _this.unregisterFromOptimization(payload.request_uuid);
        };
        this.realTime = new StoreEntity_1.RealTimeHandler(session.persistentConnection);
    }
    return RouteOptimizationService;
}());
exports.default = RouteOptimizationService;
//# sourceMappingURL=RouteOptimizationService.js.map