"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IccRecoveryDataApi = void 0;
const XHR_1 = require("../XHR");
const RecoveryData_1 = require("../../model/internal/RecoveryData");
const icc_x_api_1 = require("../../../icc-x-api");
const IccRestApiPath_1 = require("../IccRestApiPath");
const DocIdentifier_1 = require("../../model/DocIdentifier");
const Content_1 = require("../../model/Content");
class IccRecoveryDataApi {
    constructor(host, headers, authenticationProvider, fetchImpl) {
        this.host = (0, IccRestApiPath_1.iccRestApiPath)(host);
        this.headers = Object.keys(headers).map((k) => new XHR_1.XHR.Header(k, headers[k]));
        this.authenticationProvider = !!authenticationProvider ? authenticationProvider : new icc_x_api_1.NoAuthenticationProvider();
        this.fetchImpl = fetchImpl;
    }
    setHeaders(h) {
        this.headers = h;
    }
    handleError(e) {
        throw e;
    }
    createRecoveryData(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/recoverydata` + '?ts=' + new Date().getTime();
            let headers = this.headers;
            headers = headers.filter((h) => h.header !== 'Content-Type').concat(new XHR_1.XHR.Header('Content-Type', 'application/json'));
            return XHR_1.XHR.sendCommand('POST', _url, headers, body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new RecoveryData_1.RecoveryData(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    getRecoveryData(recoveryDataId) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/recoverydata/${encodeURIComponent(String(recoveryDataId))}` + '?ts=' + new Date().getTime();
            let headers = this.headers;
            headers = headers.filter((h) => h.header !== 'Content-Type').concat(new XHR_1.XHR.Header('Content-Type', 'application/json'));
            return XHR_1.XHR.sendCommand('GET', _url, headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new RecoveryData_1.RecoveryData(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    deleteRecoveryData(recoveryDataId) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/recoverydata/${encodeURIComponent(String(recoveryDataId))}` + '?ts=' + new Date().getTime();
            return XHR_1.XHR.sendCommand('DELETE', _url, this.headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new DocIdentifier_1.DocIdentifier(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    deleteAllRecoveryDataForRecipient(recipientId) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/recoverydata/forRecipient/${encodeURIComponent(String(recipientId))}` + '?ts=' + new Date().getTime();
            let headers = this.headers;
            headers = headers.filter((h) => h.header !== 'Content-Type').concat(new XHR_1.XHR.Header('Content-Type', 'application/json'));
            return XHR_1.XHR.sendCommand('DELETE', _url, headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new Content_1.Content(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    deleteAllRecoveryDataOfTypeForRecipient(type, recipientId) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/recoverydata/forRecipient/${encodeURIComponent(String(recipientId))}/ofType/${encodeURIComponent(String(type))}` +
                '?ts=' +
                new Date().getTime();
            return XHR_1.XHR.sendCommand('DELETE', _url, this.headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new Content_1.Content(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
}
exports.IccRecoveryDataApi = IccRecoveryDataApi;
//# sourceMappingURL=IccRecoveryDataApi.js.map