"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.IccDoctemplateApi = 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 DocumentTemplate_1 = require("../model/DocumentTemplate");
const AuthenticationProvider_1 = require("../../icc-x-api/auth/AuthenticationProvider");
const IccRestApiPath_1 = require("./IccRestApiPath");
const PaginatedListDocumentTemplate_1 = require("../model/PaginatedListDocumentTemplate");
class IccDoctemplateApi {
    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;
    }
    /**
     * Returns an instance of created document template.
     * @summary Create a document template with the current user
     * @param body
     */
    createDocumentTemplate(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/doctemplate` + '?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 DocumentTemplate_1.DocumentTemplate(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Deletes a batch of document templates.
     *
     * @param documentTemplateIds a ListOfIds containing the ids of the document templates to delete.
     * @return a Promise that will resolve in an array of DocIdentifiers of the successfully deleted document templates.
     */
    deleteDocumentTemplates(documentTemplateIds) {
        return __awaiter(this, void 0, void 0, function* () {
            return XHR_1.XHR.sendCommand('POST', this.host + `/doctemplate/delete/batch` + '?ts=' + new Date().getTime(), this.headers.filter((h) => h.header !== 'Content-Type').concat(new XHR_1.XHR.Header('Content-Type', 'application/json')), documentTemplateIds, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new DocIdentifier_1.DocIdentifier(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @deprecated use {@link listAllDocumentTemplatesWithPagination} instead.
     * @summary Gets all document templates for all users
     */
    listAllDocumentTemplates() {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/doctemplate/find/all?ts=${new Date().getTime()}&limit=1000000`;
            const headers = this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => { var _a; return (_a = new PaginatedListDocumentTemplate_1.PaginatedListDocumentTemplate(doc.body).rows) !== null && _a !== void 0 ? _a : []; })
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Gets all document templates for all users with pagination.
     * @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 PaginatedListDocumentTemplate.
     */
    listAllDocumentTemplatesWithPagination(startKey, startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/doctemplate/find/all?ts=${new Date().getTime()}` +
                (!!startKey ? `&startKey=${encodeURIComponent(startKey)}` : '') +
                (!!startDocumentId ? `&startDocumentId=${encodeURIComponent(startDocumentId)}` : '') +
                (!!limit ? `&limit=${limit}` : '');
            const headers = this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListDocumentTemplate_1.PaginatedListDocumentTemplate(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Gets all document templates for current user
     */
    listDocumentTemplates(loadAttachment) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/doctemplate` + '?ts=' + new Date().getTime() +
                (loadAttachment !== undefined ? '&loadAttachment=' + encodeURIComponent(String(loadAttachment)) : '');
            let headers = this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new DocumentTemplate_1.DocumentTemplate(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Gets all document templates by Type
     * @param documentTypeCode
     */
    findDocumentTemplatesByDocumentType(documentTypeCode, loadAttachment) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/doctemplate/byDocumentType/${encodeURIComponent(String(documentTypeCode))}` + '?ts=' + new Date().getTime() +
                (loadAttachment !== undefined ? '&loadAttachment=' + encodeURIComponent(String(loadAttachment)) : '');
            let headers = this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new DocumentTemplate_1.DocumentTemplate(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Gets all document templates by Type For currentUser
     * @param documentTypeCode
     */
    listDocumentTemplatesByDocumentTypeForCurrentUser(documentTypeCode, loadAttachment) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/doctemplate/byDocumentTypeForCurrentUser/${encodeURIComponent(String(documentTypeCode))}` + '?ts=' + new Date().getTime() +
                (loadAttachment !== undefined ? '&loadAttachment=' + encodeURIComponent(String(loadAttachment)) : '');
            let headers = this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new DocumentTemplate_1.DocumentTemplate(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Gets all document templates
     * @param specialityCode
     */
    listDocumentTemplatesBySpeciality(specialityCode, loadAttachment) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/doctemplate/bySpecialty/${encodeURIComponent(String(specialityCode))}` + '?ts=' + new Date().getTime() +
                (loadAttachment !== undefined ? '&loadAttachment=' + encodeURIComponent(String(loadAttachment)) : '');
            let headers = this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new DocumentTemplate_1.DocumentTemplate(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Download a the document template attachment
     * @param documentTemplateId
     * @param attachmentId
     */
    getAttachmentText(documentTemplateId, attachmentId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/doctemplate/${encodeURIComponent(String(documentTemplateId))}/attachmentText/${encodeURIComponent(String(attachmentId))}` +
                '?ts=' +
                new Date().getTime();
            let headers = 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 document template
     * @param documentTemplateId
     */
    getDocumentTemplate(documentTemplateId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/doctemplate/${encodeURIComponent(String(documentTemplateId))}` + '?ts=' + new Date().getTime();
            let headers = this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new DocumentTemplate_1.DocumentTemplate(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Download a the document template attachment
     * @param documentTemplateId
     * @param attachmentId
     */
    getDocumentTemplateAttachment(documentTemplateId, attachmentId) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/doctemplate/${encodeURIComponent(String(documentTemplateId))}/attachment/${encodeURIComponent(String(attachmentId))}` +
                '?ts=' +
                new Date().getTime();
            let headers = 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));
        });
    }
    /**
     *
     * @summary Creates a document's attachment
     * @param body
     * @param documentTemplateId
     */
    setDocumentTemplateAttachment(documentTemplateId, body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/doctemplate/${encodeURIComponent(String(documentTemplateId))}/attachment` + '?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/octet-stream'));
            return XHR_1.XHR.sendCommand('PUT', _url, headers, body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new DocumentTemplate_1.DocumentTemplate(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Creates a document's attachment
     * @param body
     * @param documentTemplateId
     */
    setDocumentTemplateAttachmentJson(documentTemplateId, body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/doctemplate/${encodeURIComponent(String(documentTemplateId))}/attachmentJson` + '?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/octet-stream'));
            return XHR_1.XHR.sendCommand('PUT', _url, headers, body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new DocumentTemplate_1.DocumentTemplate(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns an instance of created document template.
     * @summary Modify a document template with the current user
     * @param body
     * @param documentTemplateId
     */
    updateDocumentTemplate(documentTemplateId, body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/doctemplate/${encodeURIComponent(String(documentTemplateId))}` + '?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 DocumentTemplate_1.DocumentTemplate(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
}
exports.IccDoctemplateApi = IccDoctemplateApi;
//# sourceMappingURL=IccDoctemplateApi.js.map