"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.IccMessageApi = void 0;
/**
 * iCure Data Stack API Documentation
 * The iCure Data Stack Application API is the native interface to iCure. This version is obsolete, please use v2.
 *
 * OpenAPI spec version: v1
 *
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */
const XHR_1 = require("./XHR");
const DocIdentifier_1 = require("../model/DocIdentifier");
const ListOfIds_1 = require("../model/ListOfIds");
const Message_1 = require("../model/Message");
const PaginatedListMessage_1 = require("../model/PaginatedListMessage");
const AuthenticationProvider_1 = require("../../icc-x-api/auth/AuthenticationProvider");
const IccRestApiPath_1 = require("./IccRestApiPath");
const EntityBulkShareResult_1 = require("../model/requests/EntityBulkShareResult");
class IccMessageApi {
    get headers() {
        return Promise.resolve(this._headers);
    }
    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 AuthenticationProvider_1.NoAuthenticationProvider();
        this.fetchImpl = fetchImpl;
    }
    setHeaders(h) {
        this._headers = h;
    }
    handleError(e) {
        throw e;
    }
    /**
     * @summary Creates a message
     * @param body
     */
    createMessage(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/message` + '?ts=' + new Date().getTime();
            let headers = yield 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 Message_1.Message(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Deletes a batch of messages.
     *
     * @param messageIds a ListOfIds containing the ids of the messages to delete.
     * @return a Promise that will resolve in an array of DocIdentifiers of the messages successfully deleted
     */
    deleteMessages(messageIds) {
        return __awaiter(this, void 0, void 0, function* () {
            const headers = (yield this.headers).filter((h) => h.header !== 'Content-Type').concat(new XHR_1.XHR.Header('Content-Type', 'application/json'));
            return XHR_1.XHR.sendCommand('POST', this.host + `/message/delete/batch` + '?ts=' + new Date().getTime(), headers, messageIds, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new DocIdentifier_1.DocIdentifier(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Deletes a single message by id.
     *
     * @param messageId the id of the message to delete.
     * @return a Promise that will resolve in the DocIdentifier of the deleted message.
     */
    deleteMessage(messageId) {
        return __awaiter(this, void 0, void 0, function* () {
            return XHR_1.XHR.sendCommand('DELETE', this.host + `/message/${encodeURIComponent(messageId)}` + '?ts=' + new Date().getTime(), yield this.headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new DocIdentifier_1.DocIdentifier(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Get all messages (paginated) for current HC Party
     * @param startKey
     * @param startDocumentId
     * @param limit
     */
    findMessages(startKey, startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/message` +
                '?ts=' +
                new Date().getTime() +
                (startKey ? '&startKey=' + encodeURIComponent(String(startKey)) : '') +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
                (limit ? '&limit=' + encodeURIComponent(String(limit)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListMessage_1.PaginatedListMessage(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Get all messages (paginated) for current HC Party and provided from address
     * @param fromAddress
     * @param startKey
     * @param startDocumentId
     * @param limit
     * @param hcpId
     */
    findMessagesByFromAddress(fromAddress, startKey, startDocumentId, limit, hcpId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/message/byFromAddress` +
                '?ts=' +
                new Date().getTime() +
                (fromAddress ? '&fromAddress=' + encodeURIComponent(String(fromAddress)) : '') +
                (startKey ? '&startKey=' + encodeURIComponent(String(startKey)) : '') +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
                (limit ? '&limit=' + encodeURIComponent(String(limit)) : '') +
                (hcpId ? '&hcpId=' + encodeURIComponent(String(hcpId)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListMessage_1.PaginatedListMessage(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @deprecated use {@link listMessageIdsByDataOwnerPatientSentDate} instead.
     * @summary List messages found By Healthcare Party and secret foreign keys.
     * @param body
     */
    findMessagesByHCPartyPatientForeignKeysUsingPost(body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/message/byHcPartySecretForeignKeys` + '?ts=' + new Date().getTime();
            let headers = yield 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)
                .then((doc) => doc.body.map((it) => new Message_1.Message(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Keys must be delimited by commas.
     * @deprecated use {@link listMessageIdsByDataOwnerPatientSentDate} instead.
     * @summary List messages found By Healthcare Party and secret foreign keys.
     * @param secretFKeys
     */
    findMessagesByHCPartyPatientForeignKeys(secretFKeys) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/message/byHcPartySecretForeignKeys` +
                '?ts=' +
                new Date().getTime() +
                (secretFKeys ? '&secretFKeys=' + encodeURIComponent(String(secretFKeys)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new Message_1.Message(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary List Message ids by data owner and a set of secret foreign key. The ids will be sorted by Message sent, in ascending or descending
     * order according to the specified parameter value.
     *
     * @param dataOwnerId the data owner id.
     * @param secretFKeys an array of secret foreign keys.
     * @param startDate a timestamp in epoch milliseconds. If undefined, all the message ids since the beginning of time will be returned.
     * @param endDate a timestamp in epoch milliseconds. If undefined, all the message ids until the end of time will be returned.
     * @param descending whether to return the ids ordered in ascending or descending order by Message sent.
     * @return a promise that will resolve in an Array of Message ids.
     */
    listMessageIdsByDataOwnerPatientSentDate(dataOwnerId, secretFKeys, startDate, endDate, descending) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/message/byDataOwnerPatientSentDate?ts=${new Date().getTime()}` +
                '&dataOwnerId=' +
                encodeURIComponent(dataOwnerId) +
                (!!startDate ? `&startDate=${encodeURIComponent(startDate)}` : '') +
                (!!endDate ? `&endDate=${encodeURIComponent(endDate)}` : '') +
                (!!descending ? `&descending=${descending}` : '');
            const headers = (yield this.headers).filter((h) => h.header !== 'Content-Type').concat(new XHR_1.XHR.Header('Content-Type', 'application/json'));
            const body = new ListOfIds_1.ListOfIds({ ids: secretFKeys });
            return XHR_1.XHR.sendCommand('POST', _url, headers, body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => JSON.parse(JSON.stringify(it))))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Get all messages (paginated) for current HC Party and provided to address
     * @param toAddress
     * @param startKey
     * @param startDocumentId
     * @param limit
     * @param reverse
     * @param hcpId
     */
    findMessagesByToAddress(toAddress, startKey, startDocumentId, limit, reverse, hcpId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/message/byToAddress` +
                '?ts=' +
                new Date().getTime() +
                (toAddress ? '&toAddress=' + encodeURIComponent(String(toAddress)) : '') +
                (startKey ? '&startKey=' + encodeURIComponent(String(startKey)) : '') +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
                (limit ? '&limit=' + encodeURIComponent(String(limit)) : '') +
                (reverse ? '&reverse=' + encodeURIComponent(String(reverse)) : '') +
                (hcpId ? '&hcpId=' + encodeURIComponent(String(hcpId)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListMessage_1.PaginatedListMessage(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Get all messages (paginated) for current HC Party and provided transportGuid
     * @param transportGuid
     * @param received
     * @param startKey
     * @param startDocumentId
     * @param limit
     * @param hcpId
     */
    findMessagesByTransportGuid(transportGuid, received, startKey, startDocumentId, limit, hcpId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/message/byTransportGuid` +
                '?ts=' +
                new Date().getTime() +
                (transportGuid ? '&transportGuid=' + encodeURIComponent(String(transportGuid)) : '') +
                (received ? '&received=' + encodeURIComponent(String(received)) : '') +
                (startKey ? '&startKey=' + encodeURIComponent(String(startKey)) : '') +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
                (limit ? '&limit=' + encodeURIComponent(String(limit)) : '') +
                (hcpId ? '&hcpId=' + encodeURIComponent(String(hcpId)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListMessage_1.PaginatedListMessage(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Get all messages starting by a prefix between two date
     * @param transportGuid
     * @param from
     * @param to
     * @param startKey
     * @param startDocumentId
     * @param limit
     * @param hcpId
     */
    findMessagesByTransportGuidSentDate(transportGuid, from, to, startKey, startDocumentId, limit, hcpId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/message/byTransportGuidSentDate` +
                '?ts=' +
                new Date().getTime() +
                (transportGuid ? '&transportGuid=' + encodeURIComponent(String(transportGuid)) : '') +
                (from ? '&from=' + encodeURIComponent(String(from)) : '') +
                (to ? '&to=' + encodeURIComponent(String(to)) : '') +
                (startKey ? '&startKey=' + encodeURIComponent(String(startKey)) : '') +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
                (limit ? '&limit=' + encodeURIComponent(String(limit)) : '') +
                (hcpId ? '&hcpId=' + encodeURIComponent(String(hcpId)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListMessage_1.PaginatedListMessage(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Get children messages of provided message
     * @param messageId
     */
    getChildrenMessages(messageId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/message/${encodeURIComponent(String(messageId))}/children` + '?ts=' + new Date().getTime();
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new Message_1.Message(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Get children messages of provided message
     * @param body
     */
    getChildrenMessagesOfList(body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/message/children/batch` + '?ts=' + new Date().getTime();
            let headers = yield 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 Message_1.Message(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Gets a message
     * @param messageId
     */
    getMessage(messageId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/message/${encodeURIComponent(String(messageId))}` + '?ts=' + new Date().getTime();
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new Message_1.Message(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Gets batch of messages by their ids.
     * @param messageIds the ids of the messages
     */
    getMessages(messageIds) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/message/byIds` + '?ts=' + new Date().getTime();
            let headers = yield 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, messageIds, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new Message_1.Message(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Get children messages of provided message
     * @param body
     */
    listMessagesByInvoiceIds(body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/message/byInvoice` + '?ts=' + new Date().getTime();
            let headers = yield 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 Message_1.Message(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Get all messages for current HC Party and provided transportGuids
     * @param body
     * @param hcpId
     */
    listMessagesByTransportGuids(hcpId, body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/message/byTransportGuid/list` + '?ts=' + new Date().getTime() + (hcpId ? '&hcpId=' + encodeURIComponent(String(hcpId)) : '');
            let headers = yield 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 Message_1.Message(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Updates a message
     * @param body
     */
    modifyMessage(body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/message` + '?ts=' + new Date().getTime();
            let headers = yield 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 Message_1.Message(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Set read status for given list of messages
     * @param body
     */
    setMessagesReadStatus(body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/message/readstatus` + '?ts=' + new Date().getTime();
            let headers = yield 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) => doc.body.map((it) => new Message_1.Message(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Set status bits for given list of messages
     * @param body
     * @param status
     */
    setMessagesStatusBits(status, body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/message/status/${encodeURIComponent(String(status))}` + '?ts=' + new Date().getTime();
            let headers = yield 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) => doc.body.map((it) => new Message_1.Message(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @internal this method is for internal use only and may be changed without notice
     */
    bulkShareMessages(request) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + '/message/bulkSharedMetadataUpdate' + '?ts=' + new Date().getTime();
            let headers = yield 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, request, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((x) => new EntityBulkShareResult_1.EntityBulkShareResult(x, Message_1.Message)))
                .catch((err) => this.handleError(err));
        });
    }
    filterMessagesBy(body_1, startDocumentId_1, limit_1) {
        return __awaiter(this, arguments, void 0, function* (body, startDocumentId, limit, collectTiming = false) {
            const _url = this.host +
                `/message/filter` +
                '?ts=' +
                new Date().getTime() +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
                (limit ? '&limit=' + encodeURIComponent(String(limit)) : '');
            let headers = yield 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(), undefined, false, collectTiming ? ['x-filter-timing-*'] : [])
                .then((doc) => Object.assign(new PaginatedListMessage_1.PaginatedListMessage(doc.body), collectTiming ? { responseHeaders: doc.responseHeaders } : {}))
                .catch((err) => this.handleError(err));
        });
    }
    matchMessagesBy(body_1) {
        return __awaiter(this, arguments, void 0, function* (body, collectTiming = false) {
            const _url = this.host + `/message/match` + '?ts=' + new Date().getTime();
            let headers = yield 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(), undefined, false, collectTiming ? ['x-filter-timing-*'] : [])
                .then((doc) => Object.assign(doc.body.map((it) => JSON.parse(JSON.stringify(it))), collectTiming ? { responseHeaders: doc.responseHeaders } : {}))
                .catch((err) => this.handleError(err));
        });
    }
}
exports.IccMessageApi = IccMessageApi;
//# sourceMappingURL=IccMessageApi.js.map