"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.IccExchangeDataApi = void 0;
const XHR_1 = require("../XHR");
const icc_x_api_1 = require("../../../icc-x-api");
const ExchangeData_1 = require("../../model/internal/ExchangeData");
const PaginatedListExchangeData_1 = require("../../model/PaginatedListExchangeData");
const IccRestApiPath_1 = require("../IccRestApiPath");
class IccExchangeDataApi {
    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;
    }
    createExchangeData(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/exchangedata` + '?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 ExchangeData_1.ExchangeData(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    modifyExchangeData(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/exchangedata` + '?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('PUT', _url, headers, body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new ExchangeData_1.ExchangeData(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    getExchangeDataById(exchangeDataId) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/exchangedata/${encodeURIComponent(String(exchangeDataId))}` + '?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 ExchangeData_1.ExchangeData(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    getExchangeDataByIds(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/exchangedata/byIds` + '?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) => doc.body.map((it) => new ExchangeData_1.ExchangeData(it)))
                .catch((err) => this.handleError(err));
        });
    }
    getExchangeDataByDelegatorDelegate(delegatorId, delegateId) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/exchangedata/byDelegatorDelegate/${encodeURIComponent(String(delegatorId))}/${encodeURIComponent(String(delegateId))}` +
                '?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) => doc.body.map((it) => new ExchangeData_1.ExchangeData(it)))
                .catch((err) => this.handleError(err));
        });
    }
    getExchangeDataByParticipant(dataOwnerId, startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/exchangedata/byParticipant/${encodeURIComponent(String(dataOwnerId))}` +
                '?ts=' +
                new Date().getTime() +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
                (limit ? '&limit=' + encodeURIComponent(String(limit)) : '');
            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 PaginatedListExchangeData_1.PaginatedListExchangeData(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    getExchangeDataParticipantCounterparts(dataOwnerId, counterpartTypes) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/exchangedata/byParticipant/${encodeURIComponent(String(dataOwnerId))}/counterparts` +
                '?ts=' +
                new Date().getTime() +
                '&counterpartsTypes=' +
                encodeURIComponent(String(counterpartTypes));
            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) => doc.body.map((it) => JSON.parse(JSON.stringify(it))))
                .catch((err) => this.handleError(err));
        });
    }
}
exports.IccExchangeDataApi = IccExchangeDataApi;
//# sourceMappingURL=IccExchangeDataApi.js.map