"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.IccDocumentApi = 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 Document_1 = require("../model/Document");
const ListOfIds_1 = require("../model/ListOfIds");
const AuthenticationProvider_1 = require("../../icc-x-api/auth/AuthenticationProvider");
const IccRestApiPath_1 = require("./IccRestApiPath");
const EntityBulkShareResult_1 = require("../model/requests/EntityBulkShareResult");
const PaginatedListDocument_1 = require("../model/PaginatedListDocument");
class IccDocumentApi {
    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;
    }
    /**
     * Creates a document and returns an instance of created document afterward
     * @summary Create a document
     * @param body
     */
    createDocument(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/document` + '?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 Document_1.Document(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Deletes a document's attachment and returns the modified document instance afterward
     * @summary Delete a document's attachment
     * @param documentId
     */
    deleteAttachment(documentId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/document/${encodeURIComponent(String(documentId))}/attachment` + '?ts=' + new Date().getTime();
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('DELETE', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new Document_1.Document(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Deletes a batch of documents and returns the list of deleted document ids.
     *
     * @param documentIds a ListOfIds containing the ids of the documents to delete.
     * @return a Promise that will resolve in an array of DocIdentifier of the successfully deleted documents.
     */
    deleteDocuments(documentIds) {
        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 + `/document/delete/batch` + '?ts=' + new Date().getTime(), headers, documentIds, 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 document by id.
     *
     * @param documentId the id of the document to delete.
     * @return a DocIdentifier of the document.
     */
    deleteDocument(documentId) {
        return __awaiter(this, void 0, void 0, function* () {
            return XHR_1.XHR.sendCommand('DELETE', this.host + `/document/${encodeURIComponent(documentId)}` + '?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 Purges a single document, also purging any attachment linked to it.
     *
     * @param document the document to purge.
     * @return a DocIdentifier of the document.
     */
    purgeDocument(document) {
        return __awaiter(this, void 0, void 0, function* () {
            if (document.id == null || document.rev == null) {
                throw new Error('Cannot purge document with null id or rev');
            }
            return XHR_1.XHR.sendCommand('DELETE', this.host + `/document/purge/${encodeURIComponent(document.id)}?ts=${new Date().getTime()}&rev=${encodeURIComponent(document.rev)}`, yield this.headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new DocIdentifier_1.DocIdentifier(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Keys must be delimited by coma
     * @deprecated use {@link findDocumentIdsByDataOwnerSecretForeignKey} instead.
     * @summary List documents found By type, By Healthcare Party and secret foreign keys.
     * @param documentTypeCode
     * @param hcPartyId
     * @param secretFKeys
     */
    findByTypeHCPartyMessageSecretFKeys(documentTypeCode, hcPartyId, secretFKeys) {
        return __awaiter(this, void 0, void 0, function* () {
            const body = new ListOfIds_1.ListOfIds({ ids: secretFKeys });
            const _url = this.host +
                `/document/byHcPartySecretForeignKeys` +
                '?ts=' +
                new Date().getTime() +
                (documentTypeCode ? '&documentTypeCode=' + encodeURIComponent(String(documentTypeCode)) : '') +
                (hcPartyId ? '&hcPartyId=' + encodeURIComponent(String(hcPartyId)) : '');
            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 Document_1.Document(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Keys must be delimited by commas.
     * @deprecated use {@link findDocumentIdsByDataOwnerSecretForeignKey} instead.
     * @summary List documents found By Healthcare Party and secret foreign keys.
     * @param hcPartyId
     * @param secretFKeys
     */
    findDocumentsByHCPartyPatientForeignKeys(hcPartyId, secretFKeys) {
        return __awaiter(this, void 0, void 0, function* () {
            const body = new ListOfIds_1.ListOfIds({ ids: secretFKeys });
            const _url = this.host +
                `/document/byHcPartySecretForeignKeys` +
                '?ts=' +
                new Date().getTime() +
                (hcPartyId ? '&hcPartyId=' + encodeURIComponent(String(hcPartyId)) : '');
            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 Document_1.Document(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary List documents found By Healthcare Party and a single secret foreign key with pagination.
     * @deprecated use {@link findDocumentIdsByDataOwnerSecretForeignKey} instead.
     * @param hcPartyId the healthcare party id.
     * @param secretFKey the secret foreign key.
     * @param startKey the startKey provided by the previous page or undefined for the first page.
     * @param startDocumentId the startDocumentId provided by the previous page or undefined for the first page.
     * @param limit the number of elements that the page should contain.
     * @return a promise that will resolve in a PaginatedListDocument.
     */
    findDocumentsByHCPartyPatientForeignKey(hcPartyId, secretFKey, startKey, startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/document/byHcPartySecretForeignKey?ts=${new Date().getTime()}` +
                `&hcPartyId=${encodeURIComponent(hcPartyId)}` +
                `&secretFKey=${encodeURIComponent(secretFKey)}` +
                (!!startKey ? `&startKey=${encodeURIComponent(startKey)}` : '') +
                (!!startDocumentId ? `&startDocumentId=${encodeURIComponent(startDocumentId)}` : '') +
                (!!limit ? `&limit=${limit}` : '');
            const headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListDocument_1.PaginatedListDocument(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    findDocumentIdsByDataOwnerSecretForeignKey(dataOwnerId, secretForeignKeys, startDate, endDate, descending) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/document/byDataOwnerPatientCreated?ts=${new Date().getTime()}` +
                `&dataOwnerId=${encodeURIComponent(dataOwnerId)}` +
                (!!startDate ? `&startDate=${encodeURIComponent(startDate)}` : '') +
                (!!endDate ? `&endDate=${encodeURIComponent(endDate)}` : '') +
                (!!descending ? `&descending=${descending}` : '');
            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, { ids: secretForeignKeys }, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => JSON.parse(JSON.stringify(it))))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Keys must be delimited by commas.
     * @summary List documents with no delegation
     * @param limit
     */
    findWithoutDelegation(limit) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/document/woDelegation` + '?ts=' + new Date().getTime() + (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) => doc.body.map((it) => new Document_1.Document(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns the document corresponding to the identifier passed in the request
     * @summary Get a document
     * @param documentId
     */
    getDocument(documentId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/document/${encodeURIComponent(String(documentId))}` + '?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 Document_1.Document(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @deprecated use getMainDocumentAttachment instead
     */
    getDocumentAttachment(documentId, attachmentId, enckeys, fileName) {
        return __awaiter(this, void 0, void 0, function* () {
            if (enckeys) {
                throw new Error('Server-side encryption of attachment is not allowed anymore');
            }
            return this.getMainDocumentAttachment(documentId);
        });
    }
    /**
     * Get the main attachment of a document
     * @param documentId id of the document
     * @return the content of the main attachment for the document (if any)
     */
    getMainDocumentAttachment(documentId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/document/${encodeURIComponent(String(documentId))}/attachment` + '?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)
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Get the main attachment of a document forcing usage of `application/octet-stream` in Content-Type
     * @param documentId id of the document
     * @return the content of the main attachment for the document (if any)
     */
    getRawMainDocumentAttachment(documentId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/document/${encodeURIComponent(String(documentId))}/attachment` + '?ts=' + new Date().getTime();
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, 'application/octet-stream', this.authenticationProvider.getAuthService())
                .then((doc) => doc.body)
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns the first document corresponding to the externalUuid passed in the request
     * @summary Get a document
     * @param externalUuid
     */
    getDocumentByExternalUuid(externalUuid) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/document/externaluuid/${encodeURIComponent(String(externalUuid))}` + '?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 Document_1.Document(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns a list of document corresponding to the identifiers passed in the body
     * @summary Get a batch of document
     * @param body
     */
    getDocuments(body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/document/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, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new Document_1.Document(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns a list of document corresponding to the externalUuid passed in the request
     * @summary Get all documents with externalUuid
     * @param externalUuid
     */
    getDocumentsByExternalUuid(externalUuid) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/document/externaluuid/${encodeURIComponent(String(externalUuid))}/all` + '?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 Document_1.Document(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Updates the document and returns an instance of the modified document afterward
     * @summary Update a document
     * @param body
     */
    modifyDocument(body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/document` + '?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 Document_1.Document(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns the modified documents.
     * @summary Update a batch of documents
     * @param body
     */
    modifyDocuments(body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/document/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('PUT', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new Document_1.Document(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Creates or updates a main attachment for a document and returns the modified document instance afterward
     * @param documentId id of the document
     * @param documentRev revision of the document
     * @param body content of the attachment (must be compatible with XHR body)
     * @param utis an array of UTIs for the attachment. The first element will be considered as the main UTI for the document. If provided and non-empty
     * overrides existing values.
     * @param dataIsEncrypted set this to true if the body you provided is containing encrypted data. This helps to have the appropriate content type
     * for the attachment. Defaults is false
     * @param compressionAlgorithm the client-side compression algorithm used on the data, if any (e.g. 'lzma2'). Null/undefined if no compression was applied.
     * @param triedCompressionAlgorithmsVersion a version string indicating which compression algorithms were tried by the SDK, used to decide whether
     * re-compression with newer algorithms should be attempted on read.
     * @param realDataSize the size in bytes of the original data before compression and encryption, used for informational purposes.
     */
    setMainDocumentAttachment(documentId, documentRev, body, utis, dataIsEncrypted, compressionAlgorithm, triedCompressionAlgorithmsVersion, realDataSize) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!documentRev)
                throw new Error('Document rev is required');
            let _body = body;
            const _url = this.host +
                `/document/${encodeURIComponent(String(documentId))}/attachment` +
                '?ts=' +
                new Date().getTime() +
                '&rev=' +
                encodeURIComponent(String(documentRev)) +
                (utis ? utis.map((x) => '&utis=' + encodeURIComponent(String(x))).join('') : '') +
                (dataIsEncrypted ? '&encrypted=' + encodeURIComponent(String(dataIsEncrypted)) : '') +
                (compressionAlgorithm ? '&compressionAlgorithm=' + encodeURIComponent(String(compressionAlgorithm)) : '') +
                (triedCompressionAlgorithmsVersion
                    ? '&triedCompressionAlgorithmsVersion=' + encodeURIComponent(String(triedCompressionAlgorithmsVersion))
                    : '') +
                (realDataSize != null ? '&realDataSize=' + encodeURIComponent(String(realDataSize)) : '');
            let headers = yield this.headers;
            headers = headers.filter((h) => h.header !== 'Content-Type').concat(new XHR_1.XHR.Header('Content-Type', 'application/octet-stream'));
            return XHR_1.XHR.sendCommand('PUT', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new Document_1.Document(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @deprecated use setMainDocumentAttachment instead
     */
    setDocumentAttachmentBody(documentId, documentRev, enckeys, body, utis) {
        if (enckeys) {
            throw new Error('Server-side encryption of attachment is not allowed anymore');
        }
        if (!body) {
            throw new Error('body is now required.');
        }
        return this.setMainDocumentAttachment(documentId, documentRev, body, utis);
    }
    /**
     * @deprecated use setMainDocumentAttachment instead
     */
    setDocumentAttachment(documentId, documentRev, enckeys, body, utis) {
        if (enckeys) {
            throw new Error('Server-side encryption of attachment is not allowed anymore');
        }
        if (!body) {
            throw new Error('body is now required.');
        }
        return this.setMainDocumentAttachment(documentId, documentRev, body, utis);
    }
    /**
     * @deprecated use setMainDocumentAttachment instead
     */
    setDocumentAttachmentMulti(attachment, documentRev, documentId, enckeys) {
        if (enckeys) {
            throw new Error('Server-side encryption of attachment is not allowed anymore');
        }
        return this.setMainDocumentAttachment(documentId, documentRev, attachment);
    }
    /**
     * Creates or updates a secondary attachment for a document and returns the modified document instance afterward
     * @summary Creates or modifies a secondary attachment for a document
     * @param documentId id of the document to update
     * @param key Key of the secondary attachment to update
     * @param rev Revision of the latest known version of the document. If the revision does not match the current version of the document the method
     * will fail with CONFLICT status
     * @param attachment
     * @param utis an array of UTIs for the attachment. If provided and non-empty overrides existing values.
     * @param dataIsEncrypted set this to true if the body you provided is containing encrypted data. This helps to have the appropriate content type
     * for the attachment. Defaults is false
     * @param compressionAlgorithm the client-side compression algorithm used on the data, if any (e.g. 'lzma'). Null/undefined if no compression was applied.
     * @param triedCompressionAlgorithmsVersion a version string indicating which compression algorithms were tried by the SDK, used to decide whether
     * re-compression with newer algorithms should be attempted on read.
     * @param realDataSize the size in bytes of the original data before compression and encryption, used for informational purposes.
     * @return the updated document
     */
    setSecondaryAttachment(documentId, key, rev, attachment, utis, dataIsEncrypted, compressionAlgorithm, triedCompressionAlgorithmsVersion, realDataSize) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/document/` +
                encodeURIComponent(String(documentId)) +
                '/secondaryAttachments/' +
                encodeURIComponent(String(key)) +
                '?ts=' +
                new Date().getTime() +
                (rev ? '&rev=' + encodeURIComponent(String(rev)) : '') +
                (utis ? utis.map((x) => '&utis=' + encodeURIComponent(String(x))).join('') : '') +
                (dataIsEncrypted ? '&encrypted=' + encodeURIComponent(String(dataIsEncrypted)) : '') +
                (compressionAlgorithm ? '&compressionAlgorithm=' + encodeURIComponent(String(compressionAlgorithm)) : '') +
                (triedCompressionAlgorithmsVersion
                    ? '&triedCompressionAlgorithmsVersion=' + encodeURIComponent(String(triedCompressionAlgorithmsVersion))
                    : '') +
                (realDataSize != null ? '&realDataSize=' + encodeURIComponent(String(realDataSize)) : '');
            let headers = yield this.headers;
            headers = headers.filter((h) => h.header !== 'Content-Type').concat(new XHR_1.XHR.Header('Content-Type', 'application/octet-stream'));
            return XHR_1.XHR.sendCommand('PUT', _url, headers, attachment, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new Document_1.Document(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Get the secondary attachment with the provided key for a document
     * @summary Retrieve a secondary attachment of a document
     * @param documentId id of the document
     * @param key Key of the secondary attachment to retrieve
     * @param fileName
     * @return the content of the attachment
     */
    getSecondaryAttachment(documentId, key, fileName) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/document/${encodeURIComponent(String(documentId))}/secondaryAttachments/${encodeURIComponent(String(key))}` +
                '?ts=' +
                new Date().getTime() +
                (fileName ? '&fileName=' + encodeURIComponent(String(fileName)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body)
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Deletes a document's attachment and returns the modified document instance afterward
     * @summary Delete a document's attachment
     * @param documentId id of the document
     * @param key Key of the secondary attachment to delete
     * @param rev Revision of the latest known version of the document. If the revision does not match the current version of the document the method will fail with CONFLICT status
     * @return the updated document
     */
    deleteSecondaryAttachment(documentId, key, rev) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/document/${encodeURIComponent(String(documentId))}/secondaryAttachments/${encodeURIComponent(String(key))}` +
                '?ts=' +
                new Date().getTime() +
                (rev ? '&rev=' + encodeURIComponent(String(rev)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('DELETE', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new Document_1.Document(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    // TODO single request multi-attachment update
    /**
     * @internal this method is for internal use only and may be changed without notice
     */
    bulkShareDocument(request) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + '/document/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, Document_1.Document)))
                .catch((err) => this.handleError(err));
        });
    }
    matchDocumentsBy(body_1) {
        return __awaiter(this, arguments, void 0, function* (body, collectTiming = false) {
            const _url = this.host + `/document/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.IccDocumentApi = IccDocumentApi;
//# sourceMappingURL=IccDocumentApi.js.map