"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.IccSecureDelegationKeyMapApi = void 0;
const EntityBulkShareResult_1 = require("../../model/requests/EntityBulkShareResult");
const XHR_1 = require("../XHR");
const icc_x_api_1 = require("../../../icc-x-api");
const IccRestApiPath_1 = require("../IccRestApiPath");
const SecureDelegationKeyMap_1 = require("../../model/internal/SecureDelegationKeyMap");
/**
 * @internal this class is for internal use only and may be changed without notice.
 */
class IccSecureDelegationKeyMapApi {
    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;
    }
    get headers() {
        return Promise.resolve(this._headers);
    }
    setHeaders(h) {
        this._headers = h;
    }
    handleError(e) {
        throw e;
    }
    getByDelegationKeys(delegationKeys, proofOfAccessHeaders) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = delegationKeys;
            const _url = this.host + `/securedelegationkeymap/bydelegationkeys` + '?ts=' + new Date().getTime();
            let headers = [...(yield this.headers), ...proofOfAccessHeaders];
            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) => doc.body.map((it) => new SecureDelegationKeyMap_1.SecureDelegationKeyMap(it)))
                .catch((err) => this.handleError(err));
        });
    }
    create(delegationKeyMap, proofOfAccessHeader) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = delegationKeyMap;
            const _url = this.host + `/securedelegationkeymap` + '?ts=' + new Date().getTime();
            let headers = [...(yield this.headers), proofOfAccessHeader];
            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 SecureDelegationKeyMap_1.SecureDelegationKeyMap(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    bulkShareSecureDelegationKeyMap(request, proofOfAccessHeaders) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + '/securedelegationkeymap/bulkSharedMetadataUpdate' + '?ts=' + new Date().getTime();
            let headers = [...(yield this.headers), ...proofOfAccessHeaders];
            headers = headers.filter((h) => h.header !== 'Content-Type').concat(new XHR_1.XHR.Header('Content-Type', 'application/json'));
            return XHR_1.XHR.sendCommand('PUT', _url, headers, request, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((x) => new EntityBulkShareResult_1.EntityBulkShareResult(x, SecureDelegationKeyMap_1.SecureDelegationKeyMap)))
                .catch((err) => this.handleError(err));
        });
    }
}
exports.IccSecureDelegationKeyMapApi = IccSecureDelegationKeyMapApi;
//# sourceMappingURL=IccSecureDelegationKeyMapApi.js.map