"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var BringgException_1 = require("../Core/BringgException");
var RouteGenerator_1 = require("../Core/RouteGenerator");
var StoreEntity_1 = require("../Core/StoreEntity");
var Route_1 = require("../Services/Route");
var SessionBasedService_1 = require("../Services/SessionBasedService");
var ResourceUploadApi = /** @class */ (function (_super) {
    __extends(ResourceUploadApi, _super);
    function ResourceUploadApi(session) {
        var _this = _super.call(this, session.config.getApiEndpoint(), session) || this;
        _this.prepare_resource = new Route_1.Route('/resources/prepare', 1 /* HttpMethod.POST */);
        _this.post_process_resource = new Route_1.Route('/resources/{:id}/process', 1 /* HttpMethod.POST */);
        _this.get_process_resource = new Route_1.Route('/resources/{:id}/process', 0 /* HttpMethod.GET */);
        _this.realTime = new StoreEntity_1.RealTimeHandler(session.persistentConnection);
        _this.processedCallbacks = [];
        return _this;
    }
    ResourceUploadApi.prototype.prepare = function (type, body) {
        return this.handleAuthorizedRequest({
            route: this.prepare_resource,
            extractor: RouteGenerator_1.defaultExtractor,
            exceptionProducer: BringgException_1.BringgException.serviceException('Failed to call prepare resource'),
            payload: __assign({ type: type }, body)
        });
    };
    ResourceUploadApi.prototype.process = function (resourceId, doneEvent) {
        this.realTime.on(doneEvent, this.onProcessed.bind(this));
        return this.handleAuthorizedRequest({
            route: this.post_process_resource,
            extractor: RouteGenerator_1.defaultExtractor,
            exceptionProducer: BringgException_1.BringgException.serviceException('Failed to call resource process'),
            payload: null,
            routeParams: { id: resourceId }
        });
    };
    ResourceUploadApi.prototype.progress = function (resourceId) {
        return this.handleAuthorizedRequest({
            route: this.get_process_resource,
            extractor: RouteGenerator_1.defaultExtractor,
            exceptionProducer: BringgException_1.BringgException.serviceException('Failed to get resource process'),
            routeParams: { id: resourceId }
        });
    };
    ResourceUploadApi.prototype.registerOnProcessed = function (resourceId, callback) {
        var _this = this;
        this.processedCallbacks.push({ callback: callback, resourceId: resourceId });
        return function () {
            return _this.processedCallbacks.splice(_this.processedCallbacks.map(function (processedCallbacks) { return processedCallbacks.callback; }).indexOf(callback), 1);
        };
    };
    ResourceUploadApi.prototype.onProcessed = function (data) {
        this.processedCallbacks.forEach(function (processedCallback) {
            if (data.operation_id === processedCallback.resourceId) {
                processedCallback.callback(data);
            }
        });
    };
    return ResourceUploadApi;
}(SessionBasedService_1.SessionBasedService));
exports.default = ResourceUploadApi;
//# sourceMappingURL=ResourceUploadApi.js.map