"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.IccReceiptApi = 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 Receipt_1 = require("../model/Receipt");
const AuthenticationProvider_1 = require("../../icc-x-api/auth/AuthenticationProvider");
const IccRestApiPath_1 = require("./IccRestApiPath");
const EntityBulkShareResult_1 = require("../model/requests/EntityBulkShareResult");
class IccReceiptApi {
    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 receipt
     * @param body
     */
    createReceipt(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/receipt` + '?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 Receipt_1.Receipt(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Deletes a batch of receipts.
     *
     * @param receiptIds a ListOfIds containing the ids of the receipts to delete.
     * @return a Promise that will resolve in an array of DocIdentifiers of the successfully deleted receipts.
     */
    deleteReceipts(receiptIds) {
        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 + `/receipt/delete/batch` + '?ts=' + new Date().getTime(), headers, receiptIds, 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 receipt by id.
     *
     * @param receiptId the id of the receipt to delete.
     * @return a Promise that will resolve in the DocIdentifier of the deleted receipt.
     */
    deleteReceipt(receiptId) {
        return __awaiter(this, void 0, void 0, function* () {
            return XHR_1.XHR.sendCommand('DELETE', this.host + `/receipt/${encodeURIComponent(receiptId)}` + '?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 Gets a receipt
     * @param receiptId
     */
    getReceipt(receiptId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/receipt/${encodeURIComponent(String(receiptId))}` + '?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 Receipt_1.Receipt(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Get an attachment
     * @param receiptId
     * @param attachmentId
     * @param enckeys
     */
    getReceiptAttachment(receiptId, attachmentId, enckeys) {
        return __awaiter(this, void 0, void 0, function* () {
            if (enckeys) {
                throw new Error('Server-side encryption is not supported anymore.');
            }
            let _body = null;
            const _url = this.host +
                `/receipt/${encodeURIComponent(String(receiptId))}/attachment/${encodeURIComponent(String(attachmentId))}` +
                '?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));
        });
    }
    /**
     *
     * @summary Gets a receipt
     * @param ref
     */
    listByReference(ref) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/receipt/byRef/${encodeURIComponent(String(ref))}` + '?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 Receipt_1.Receipt(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Updates a receipt
     * @param body
     */
    modifyReceipt(body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/receipt` + '?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 Receipt_1.Receipt(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @deprecated use setReceiptAttachmentForBlobType instead
     */
    setReceiptAttachment(receiptId, receiptRev, blobType, enckeys, body) {
        return __awaiter(this, void 0, void 0, function* () {
            if (enckeys) {
                throw new Error('Server-side encryption is not supported anymore.');
            }
            if (!body) {
                throw new Error('Attachment content is requred');
            }
            return this.setReceiptAttachmentForBlobType(receiptId, receiptRev, blobType, body);
        });
    }
    /**
     * @summary Creates a receipt's attachment
     * @param receiptId id of a receipt
     * @param receiptRev rev of the receipt
     * @param blobType receipt blob type
     * @param body content of the attachment, already encrypted
     */
    setReceiptAttachmentForBlobType(receiptId, receiptRev, blobType, body) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!receiptRev)
                throw new Error('Receipt rev is required');
            let _body = body;
            const _url = this.host +
                `/receipt/${encodeURIComponent(String(receiptId))}/attachment/${encodeURIComponent(String(blobType))}` +
                '?ts=' +
                new Date().getTime() +
                '&rev=' +
                receiptRev;
            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 Receipt_1.Receipt(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Creates a receipt's data attachment with compression metadata
     * @param receiptId id of a receipt
     * @param blobType receipt blob type
     * @param rev revision of the receipt
     * @param body content of the attachment
     * @param compressionAlgorithm the compression algorithm used on the client side, if any
     * @param triedCompressionAlgorithmsVersion a version string for the compression algorithms tried
     * @param realDataSize the real size of the data after decryption and decompression
     */
    setReceiptDataAttachment(receiptId, blobType, rev, body, compressionAlgorithm, triedCompressionAlgorithmsVersion, realDataSize) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!rev)
                throw new Error('Receipt rev is required');
            const _url = this.host +
                `/receipt/${encodeURIComponent(String(receiptId))}/dataattachment/${encodeURIComponent(String(blobType))}` +
                '?ts=' +
                new Date().getTime() +
                '&rev=' +
                encodeURIComponent(String(rev)) +
                (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' && h.header !== 'Content-Length')
                .concat(new XHR_1.XHR.Header('Content-Type', 'application/octet-stream'))
                .concat(new XHR_1.XHR.Header('Content-Length', String(body.byteLength)));
            return XHR_1.XHR.sendCommand('PUT', _url, headers, body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new Receipt_1.Receipt(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Get a receipt's attachment by blob type
     * @param receiptId id of the receipt
     * @param blobType the blob type of the attachment to retrieve
     */
    getReceiptAttachmentByBlobType(receiptId, blobType) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/receipt/${encodeURIComponent(String(receiptId))}/attachment/ofType/${encodeURIComponent(String(blobType))}` +
                '?ts=' +
                new Date().getTime();
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body)
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @internal this method is for internal use only and may be changed without notice
     */
    bulkShareReceipt(request) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + '/receipt/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, Receipt_1.Receipt)))
                .catch((err) => this.handleError(err));
        });
    }
}
exports.IccReceiptApi = IccReceiptApi;
//# sourceMappingURL=IccReceiptApi.js.map