"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IccDoctemplateXApi = void 0;
const icc_api_1 = require("../icc-api");
const binary_utils_1 = require("./utils/binary-utils");
const XHR_1 = require("../icc-api/api/XHR");
const AuthenticationProvider_1 = require("./auth/AuthenticationProvider");
// noinspection JSUnusedGlobalSymbols
class IccDoctemplateXApi extends icc_api_1.IccDoctemplateApi {
    constructor(host, headers, crypto, authenticationProvider = new AuthenticationProvider_1.NoAuthenticationProvider(), fetchImpl = typeof window !== 'undefined'
        ? window.fetch
        : typeof self !== 'undefined'
            ? self.fetch
            : fetch) {
        super(host, headers, authenticationProvider, fetchImpl);
        this.crypto = crypto;
        this.authenticationProvider = authenticationProvider;
        this.fetchImpl = fetchImpl;
    }
    /**
     * Creates a new instance of document template with initialised metadata (not persisted in the database).
     * @param user the current user, used to set the owner.
     * @param template the template content as a string, which will be converted to an ArrayBuffer attachment.
     * @param c optional initial data to merge into the document template. Metadata such as id, creation date, and guid
     * will be automatically generated if not provided.
     * @return a new DocumentTemplate instance with populated metadata fields.
     */
    newInstance(user, template, c) {
        return new Promise((resolve, reject) => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j;
            const documentTemplate = Object.assign(Object.assign({}, (c !== null && c !== void 0 ? c : {})), { _type: 'org.taktik.icure.entities.DocumentTemplate', id: (_a = c === null || c === void 0 ? void 0 : c.id) !== null && _a !== void 0 ? _a : this.crypto.primitives.randomUuid(), owner: (_b = c === null || c === void 0 ? void 0 : c.owner) !== null && _b !== void 0 ? _b : user.id, created: (_c = c === null || c === void 0 ? void 0 : c.created) !== null && _c !== void 0 ? _c : new Date().getTime(), modified: (_d = c === null || c === void 0 ? void 0 : c.modified) !== null && _d !== void 0 ? _d : new Date().getTime(), guid: (_e = c === null || c === void 0 ? void 0 : c.guid) !== null && _e !== void 0 ? _e : this.crypto.primitives.randomUuid(), group: (_f = c === null || c === void 0 ? void 0 : c.group) !== null && _f !== void 0 ? _f : null, specialty: (_g = c === null || c === void 0 ? void 0 : c.specialty) !== null && _g !== void 0 ? _g : null, attachment: (_h = c === null || c === void 0 ? void 0 : c.attachment) !== null && _h !== void 0 ? _h : (0, binary_utils_1.string2ua)(template), mainUti: (_j = c === null || c === void 0 ? void 0 : c.mainUti) !== null && _j !== void 0 ? _j : 'public.plain-text' });
            if (documentTemplate.group && documentTemplate.group.guid == null) {
                documentTemplate.group.guid = this.crypto.primitives.randomUuid();
            }
            //sauver l doctemplate vide
            if (template) {
                //save attachement
            }
            return resolve(documentTemplate);
        });
    }
    /**
     * @deprecated not implemented. Always rejects with an error.
     * @param ownerId the id of the owner.
     * @return always rejects.
     */
    // noinspection JSUnusedLocalSymbols
    findAllByOwnerId(ownerId) {
        return new Promise(function (resolve, reject) {
            reject(console.log('findByHCPartyPatientSecretFKeys not implemented in document API!'));
        });
    }
    /**
     * Builds the URL for downloading a document template attachment.
     * @param documentId the id of the document template.
     * @param attachmentId the id of the attachment.
     * @return the fully qualified URL to the attachment resource.
     */
    // noinspection JSUnusedGlobalSymbols
    getAttachmentUrl(documentId, attachmentId) {
        return (this.host + '/doctemplate/{documentId}/attachment/{attachmentId}'.replace('{documentId}', documentId).replace('{attachmentId}', attachmentId));
    }
    /**
     * Retrieves the text content of a document template attachment. Decodes binary responses as UTF-8 text, and
     * returns text/plain, text/html, or text/xml content directly. Returns false for unsupported content types.
     * @param documentTemplateId the id of the document template.
     * @param attachmentId the id of the attachment.
     * @return the attachment text content as a string, or false if the content type is unsupported.
     */
    getAttachmentText(documentTemplateId, attachmentId) {
        const _body = null;
        const _url = this.host +
            '/doctemplate/{documentTemplateId}/attachmentText/{attachmentId}'
                .replace('{documentTemplateId}', documentTemplateId + '')
                .replace('{attachmentId}', attachmentId + '') +
            '?ts=' +
            new Date().getTime();
        return XHR_1.XHR.sendCommand('GET', _url, this.headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
            .then((doc) => {
            if (doc.contentType.startsWith('application/octet-stream')) {
                const enc = new TextDecoder('utf-8');
                const arr = new Uint8Array(doc.body);
                return enc.decode(arr);
            }
            else if (doc.contentType.startsWith('text/plain') || doc.contentType.startsWith('text/html') || doc.contentType.startsWith('text/xml')) {
                return doc.body;
            }
            else {
                return false;
            }
        })
            .catch((err) => this.handleError(err));
    }
}
exports.IccDoctemplateXApi = IccDoctemplateXApi;
//# sourceMappingURL=icc-doctemplate-x-api.js.map