"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.IccPatientApi = 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 Content_1 = require("../model/Content");
const DataOwnerRegistrationSuccess_1 = require("../model/DataOwnerRegistrationSuccess");
const DocIdentifier_1 = require("../model/DocIdentifier");
const IdWithRev_1 = require("../model/IdWithRev");
const PaginatedListPatient_1 = require("../model/PaginatedListPatient");
const PaginatedListString_1 = require("../model/PaginatedListString");
const Patient_1 = require("../model/Patient");
const AuthenticationProvider_1 = require("../../icc-x-api/auth/AuthenticationProvider");
const IccRestApiPath_1 = require("./IccRestApiPath");
const EntityBulkShareResult_1 = require("../model/requests/EntityBulkShareResult");
class IccPatientApi {
    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;
    }
    /**
     * Returns the id and _rev of created patients
     * @summary Create patients in bulk
     * @param body
     */
    bulkCreatePatients(body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/patient/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 IdWithRev_1.IdWithRev(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns the id and _rev of modified patients
     * @summary Modify patients in bulk
     * @param body
     */
    bulkUpdatePatients(body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/patient/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 IdWithRev_1.IdWithRev(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns the count of patients shared directly with the provided hcp (ignoring hierarchies).
     * WARNING: the current implementation can count some patients twice if they have both security metadata and legacy
     * delegations from before v8.
     */
    countOfPatients(hcPartyId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/patient/hcParty/${encodeURIComponent(String(hcPartyId))}/count` + '?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 Content_1.Content(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Name, last name, date of birth, and gender are required. After creation of the patient and obtaining the ID, you need to create an initial delegation.
     * @summary Create a patient
     * @param body
     */
    createPatient(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/patient` + '?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 Patient_1.Patient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Deletes a batch of patients.
     *
     * @param patientIds a ListOfIds containing the ids of the patients to delete.
     * @return a Promise that will resolve in an array of DocIdentifiers of the successfully deleted patients.
     */
    deletePatients(patientIds) {
        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 + `/patient/delete/batch` + '?ts=' + new Date().getTime(), headers, patientIds, 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 patient by id.
     *
     * @param patientId the id of the patient to delete.
     * @return a Promise that will resolve in the DocIdentifier of the deleted patient.
     */
    deletePatient(patientId) {
        return __awaiter(this, void 0, void 0, function* () {
            return XHR_1.XHR.sendCommand('DELETE', this.host + `/patient/${encodeURIComponent(patientId)}` + '?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));
        });
    }
    filterPatientsBy(startKey_1, startDocumentId_1, limit_1, skip_1, sort_1, desc_1, body_1) {
        return __awaiter(this, arguments, void 0, function* (startKey, startDocumentId, limit, skip, sort, desc, body, collectTiming = false) {
            let _body = null;
            _body = body;
            const _url = this.host +
                `/patient/filter` +
                '?ts=' +
                new Date().getTime() +
                (startKey ? '&startKey=' + encodeURIComponent(String(startKey)) : '') +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
                (limit ? '&limit=' + encodeURIComponent(String(limit)) : '') +
                (skip ? '&skip=' + encodeURIComponent(String(skip)) : '') +
                (sort ? '&sort=' + encodeURIComponent(String(sort)) : '') +
                (desc ? '&desc=' + encodeURIComponent(String(desc)) : '');
            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 PaginatedListPatient_1.PaginatedListPatient(doc.body), collectTiming ? { responseHeaders: doc.responseHeaders } : {}))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Get Paginated List of Patients sorted by Access logs descending
     * @param userId A User ID
     * @param accessType The type of access (COMPUTER or USER)
     * @param startDate The start search epoch
     * @param startKey The start key for pagination
     * @param startDocumentId A patient document ID
     * @param limit Number of rows
     */
    findByAccessLogUserAfterDate(userId, accessType, startDate, startKey, startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/byAccess/${encodeURIComponent(String(userId))}` +
                '?ts=' +
                new Date().getTime() +
                (accessType ? '&accessType=' + encodeURIComponent(String(accessType)) : '') +
                (startDate ? '&startDate=' + encodeURIComponent(String(startDate)) : '') +
                (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 PaginatedListPatient_1.PaginatedListPatient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Get the patient having the provided externalId
     * @param externalId A external ID
     */
    findByExternalId(externalId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/patient/byExternalId/${encodeURIComponent(String(externalId))}` + '?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 Patient_1.Patient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns a list of patients along with next start keys and Document ID. If the nextStartKey is Null it means that this is the last page.
     * @summary Find patients for the current user (HcParty)
     * @param healthcarePartyId HealthcareParty Id. If not set, will use user&#x27;s hcpId
     * @param filterValue Optional value for filtering results
     * @param startKey The start key for pagination: a JSON representation of an array containing all the necessary components to form the Complex Key&#x27;s startKey
     * @param startDocumentId A patient document ID
     * @param limit Number of rows
     * @param sortDirection Optional value for providing a sorting direction (&#x27;asc&#x27;, &#x27;desc&#x27;). Set to &#x27;asc&#x27; by default.
     */
    findByNameBirthSsinAuto(healthcarePartyId, filterValue, startKey, startDocumentId, limit, sortDirection) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/byNameBirthSsinAuto` +
                '?ts=' +
                new Date().getTime() +
                (healthcarePartyId ? '&healthcarePartyId=' + encodeURIComponent(String(healthcarePartyId)) : '') +
                (filterValue ? '&filterValue=' + encodeURIComponent(String(filterValue)) : '') +
                (startKey ? '&startKey=' + encodeURIComponent(String(startKey)) : '') +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
                (limit ? '&limit=' + encodeURIComponent(String(limit)) : '') +
                (sortDirection ? '&sortDirection=' + encodeURIComponent(String(sortDirection)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListPatient_1.PaginatedListPatient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Provides a paginated list of patients with duplicate name for an hecparty
     * @param hcPartyId Healthcare party id
     * @param startKey The start key for pagination, depends on the filters used
     * @param startDocumentId A patient document ID
     * @param limit Number of rows
     */
    findDuplicatesByName(hcPartyId, startKey, startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/duplicates/name` +
                '?ts=' +
                new Date().getTime() +
                (hcPartyId ? '&hcPartyId=' + encodeURIComponent(String(hcPartyId)) : '') +
                (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('POST', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListPatient_1.PaginatedListPatient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Provides a paginated list of patients with duplicate ssin for an hecparty
     * @param hcPartyId Healthcare party id
     * @param startKey The start key for pagination, depends on the filters used
     * @param startDocumentId A patient document ID
     * @param limit Number of rows
     */
    findDuplicatesBySsin(hcPartyId, startKey, startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/duplicates/ssin` +
                '?ts=' +
                new Date().getTime() +
                (hcPartyId ? '&hcPartyId=' + encodeURIComponent(String(hcPartyId)) : '') +
                (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('POST', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListPatient_1.PaginatedListPatient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns a list of patients
     * @summary Filter patients for the current user (HcParty)
     * @param firstName The first name
     * @param lastName The last name
     * @param dateOfBirth The date of birth
     */
    fuzzySearch(firstName, lastName, dateOfBirth) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/fuzzy` +
                '?ts=' +
                new Date().getTime() +
                (firstName ? '&firstName=' + encodeURIComponent(String(firstName)) : '') +
                (lastName ? '&lastName=' + encodeURIComponent(String(lastName)) : '') +
                (dateOfBirth ? '&dateOfBirth=' + encodeURIComponent(String(dateOfBirth)) : '');
            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 Patient_1.Patient(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * It gets patient administrative data.
     * @summary Get patient
     * @param patientId
     */
    getPatient(patientId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/patient/${encodeURIComponent(String(patientId))}` + '?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 Patient_1.Patient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * (key, value) of the map is as follows: (ID of the owner of the encrypted AES key, encrypted AES keys)
     * @summary Get the HcParty encrypted AES keys indexed by owner.
     * @param patientId
     */
    getPatientAesExchangeKeysForDelegate(patientId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/patient/${encodeURIComponent(String(patientId))}/aesExchangeKeys` + '?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) => JSON.parse(JSON.stringify(doc.body)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * It gets patient administrative data based on the identifier (root & extension) parameters.
     * @summary Get patient by identifier
     * @param hcPartyId
     * @param id
     * @param system
     */
    getPatientByHealthcarepartyAndIdentifier(hcPartyId, id, system) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/${encodeURIComponent(String(hcPartyId))}/${encodeURIComponent(String(id))}` +
                '?ts=' +
                new Date().getTime() +
                (system ? '&system=' + encodeURIComponent(String(system)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new Patient_1.Patient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * This endpoint is used to recover all keys that have already been created and that can be used to share information with this patient. It returns a map with the following structure: ID of the owner of the encrypted AES key -> encrypted AES key. The returned encrypted AES keys will have to be decrypted using the patient's private key.                  {                     \"hcparty 1 delegator ID\": \"AES hcparty key (encrypted using patient public RSA key)\"                     \"hcparty 2 delegator ID\": \"other AES hcparty key (encrypted using patient public RSA key)\"                 }
     * @summary Get the patient (identified by patientId) hcparty keys. Those keys are AES keys (encrypted) used to share information between HCPs and a patient.
     * @param patientId The patient Id for which information is shared
     */
    getPatientHcPartyKeysForDelegate(patientId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/patient/${encodeURIComponent(String(patientId))}/keys` + '?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) => JSON.parse(JSON.stringify(doc.body)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * It gets patient administrative data.
     * @summary Get patients by id
     * @param body
     */
    getPatients(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/patient/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 Patient_1.Patient(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns a list of deleted patients, within the specified time period, if any.
     * @summary Find deleted patients
     * @param startDate Filter deletions after this date (unix epoch), included
     * @param endDate Filter deletions before this date (unix epoch), included
     * @param startKey the startKey provided by the previous page or undefined for the first page.
     * @param desc Descending
     * @param startDocumentId A patient document ID
     * @param limit Number of rows
     */
    listDeletedPatients(startDate, endDate, desc, startKey, startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/patient/deleted/byDate` +
                '?ts=' +
                new Date().getTime() +
                (startDate ? '&startDate=' + encodeURIComponent(startDate) : '') +
                (endDate ? '&endDate=' + encodeURIComponent(endDate) : '') +
                (desc ? '&desc=' + encodeURIComponent(desc) : '') +
                (startKey ? '&startKey=' + encodeURIComponent(startKey) : '') +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(startDocumentId) : '') +
                (limit ? '&limit=' + encodeURIComponent(limit) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListPatient_1.PaginatedListPatient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns a list of deleted patients, by name and/or firstname prefix, if any.
     * @summary Find deleted patients
     * @param firstName First name prefix
     * @param lastName Last name prefix
     */
    listDeletedPatientsByName(firstName, lastName) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/deleted/by_name` +
                '?ts=' +
                new Date().getTime() +
                (firstName ? '&firstName=' + encodeURIComponent(String(firstName)) : '') +
                (lastName ? '&lastName=' + encodeURIComponent(String(lastName)) : '');
            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 Patient_1.Patient(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns a list of patients that have been merged after the provided date
     * @summary List patients that have been merged towards another patient
     * @param date
     */
    listOfMergesAfter(date) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/patient/merges/${encodeURIComponent(String(date))}` + '?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 Patient_1.Patient(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns a list of patients that have been modified after the provided date
     * @summary List patients that have been modified after the provided date
     * @param date
     * @param startKey The start key for pagination the date of the first element of the new page
     * @param startDocumentId A patient document ID
     * @param limit Number of rows
     */
    listOfPatientsModifiedAfter(date, startKey, startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/modifiedAfter/${encodeURIComponent(String(date))}` +
                '?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 PaginatedListPatient_1.PaginatedListPatient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @deprecated This method is not intuitive and will be removed in a future version
     * Currently there is no direct replacement that can do everything this method does.
     * If you need all patients unsorted or sorted by ssin/date of birth use `PatientByHcPartyFilter`/`PatientByHcPartyDateOfBirthFilter`
     * with {@link matchPatientsBy} to get the ids of all the patients shared directly with an HCP (ignoring hierarchies).
     * Note that on databases with many patients (>50k) the matches request could take various seconds as it is not
     * paginated.
     * If you want to use pagination or need to sort by name instead you can use the {@link listPatientsSortedByName}.
     * Usage of PatientByHcPartyNameFilter or PatientByHcPartyNameContainsFuzzyFilter as a replacement of this method is
     * discouraged as the sorting done by those filter is unintuitive.
     * Contact us if your use case is not covered by the suggested alternatives.
     *
     * This method returns all the patients (with pagination) that are shared directly with the parent of {@link hcPartyId},
     * or if undefined the current user's parent. Note that this doesn't allow to get patients that are shared only with
     * the current user and not its parent.
     */
    listPatients(hcPartyId, sortField, startKey, startDocumentId, limit, sortDirection) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient` +
                '?ts=' +
                new Date().getTime() +
                (hcPartyId ? '&hcPartyId=' + encodeURIComponent(String(hcPartyId)) : '') +
                (sortField ? '&sortField=' + encodeURIComponent(String(sortField)) : '') +
                (startKey ? '&startKey=' + encodeURIComponent(String(startKey)) : '') +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
                (limit ? '&limit=' + encodeURIComponent(String(limit)) : '') +
                (sortDirection ? '&sortDirection=' + encodeURIComponent(String(sortDirection)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListPatient_1.PaginatedListPatient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Get all patients shared directly with the provided hcparty (ignoring hierarchies) sorted by lastName, firstName.
     */
    listPatientsSortedByName(hcPartyId, startKey, startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/byHcPartyId` +
                '?ts=' +
                new Date().getTime() +
                (hcPartyId ? '&hcPartyId=' + encodeURIComponent(String(hcPartyId)) : '') +
                (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 PaginatedListPatient_1.PaginatedListPatient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns a list of patients along with next start keys and Document ID. If the nextStartKey is Null it means that this is the last page.
     * @summary List patients for a specific HcParty or for the current HcParty
     * @param hcPartyId
     * @param sortField Optional value for sorting results by a given field (&#x27;name&#x27;, &#x27;ssin&#x27;, &#x27;dateOfBirth&#x27;). Specifying this deactivates filtering
     * @param startKey The start key for pagination: a JSON representation of an array containing all the necessary components to form the Complex Key&#x27;s startKey
     * @param startDocumentId A patient document ID
     * @param limit Number of rows
     * @param sortDirection Optional value for providing a sorting direction (&#x27;asc&#x27;, &#x27;desc&#x27;). Set to &#x27;asc&#x27; by default.
     */
    listPatientsByHcParty(hcPartyId, sortField, startKey, startDocumentId, limit, sortDirection) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/hcParty/${encodeURIComponent(String(hcPartyId))}` +
                '?ts=' +
                new Date().getTime() +
                (sortField ? '&sortField=' + encodeURIComponent(String(sortField)) : '') +
                (startKey ? '&startKey=' + encodeURIComponent(String(startKey)) : '') +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
                (limit ? '&limit=' + encodeURIComponent(String(limit)) : '') +
                (sortDirection ? '&sortDirection=' + encodeURIComponent(String(sortDirection)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListPatient_1.PaginatedListPatient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns a list of patients along with next start keys and Document ID. If the nextStartKey is Null it means that this is the last page.
     * @summary List patients by pages for a specific HcParty
     * @param hcPartyId Healthcare party id
     * @param startKey The page first id
     * @param startDocumentId A patient document ID
     * @param limit Page size
     */
    listPatientsIds(hcPartyId, startKey, startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/byHcPartyId` +
                '?ts=' +
                new Date().getTime() +
                (hcPartyId ? '&hcPartyId=' + encodeURIComponent(String(hcPartyId)) : '') +
                (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 PaginatedListString_1.PaginatedListString(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns a list of patients along with next start keys and Document ID. If the nextStartKey is Null it means that this is the last page.
     * @summary List patients of a specific HcParty or of the current HcParty
     * @param hcPartyId
     * @param sortField Optional value for sorting results by a given field (&#x27;name&#x27;, &#x27;ssin&#x27;, &#x27;dateOfBirth&#x27;). Specifying this deactivates filtering
     * @param startKey The start key for pagination: a JSON representation of an array containing all the necessary components to form the Complex Key&#x27;s startKey
     * @param startDocumentId A patient document ID
     * @param limit Number of rows
     * @param sortDirection Optional value for providing a sorting direction (&#x27;asc&#x27;, &#x27;desc&#x27;). Set to &#x27;asc&#x27; by default.
     */
    listPatientsOfHcParty(hcPartyId, sortField, startKey, startDocumentId, limit, sortDirection) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/ofHcParty/${encodeURIComponent(String(hcPartyId))}` +
                '?ts=' +
                new Date().getTime() +
                (sortField ? '&sortField=' + encodeURIComponent(String(sortField)) : '') +
                (startKey ? '&startKey=' + encodeURIComponent(String(startKey)) : '') +
                (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
                (limit ? '&limit=' + encodeURIComponent(String(limit)) : '') +
                (sortDirection ? '&sortDirection=' + encodeURIComponent(String(sortDirection)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListPatient_1.PaginatedListPatient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    matchPatientsBy(body_1) {
        return __awaiter(this, arguments, void 0, function* (body, collectTiming = false) {
            const _url = this.host + `/patient/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));
        });
    }
    /**
     *
     * @summary Merge a series of patients into another patient
     * @param toId
     * @param fromIds
     */
    mergeInto(toId, fromIds) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/patient/mergeInto/${encodeURIComponent(String(toId))}/from/${encodeURIComponent(String(fromIds))}` + '?ts=' + new Date().getTime();
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('PUT', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new Patient_1.Patient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * No particular return value. It's just a message.
     * @summary Modify a patient
     * @param body
     */
    modifyPatient(body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host + `/patient` + '?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 Patient_1.Patient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Set a patient referral doctor
     * @param patientId
     * @param referralId The referal id. Accepts &#x27;none&#x27; for referral removal.
     * @param start Optional value for start of referral
     * @param end Optional value for end of referral
     */
    modifyPatientReferral(patientId, referralId, start, end) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host +
                `/patient/${encodeURIComponent(String(patientId))}/referral/${encodeURIComponent(String(referralId))}` +
                '?ts=' +
                new Date().getTime() +
                (start ? '&start=' + encodeURIComponent(String(start)) : '') +
                (end ? '&end=' + encodeURIComponent(String(end)) : '');
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('PUT', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new Patient_1.Patient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Register a new patient into the system
     * @summary Register a patient
     * @param body
     * @param hcPartyId
     * @param groupId
     * @param token
     * @param useShortToken
     */
    registerPatient(hcPartyId, groupId, token, useShortToken, body) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            _body = body;
            const _url = this.host +
                `/patient/register/forHcp/${encodeURIComponent(String(hcPartyId))}/inGroup/${encodeURIComponent(String(groupId))}` +
                '?ts=' +
                new Date().getTime() +
                (token ? '&token=' + encodeURIComponent(String(token)) : '') +
                (useShortToken ? '&useShortToken=' + encodeURIComponent(String(useShortToken)) : '');
            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 DataOwnerRegistrationSuccess_1.DataOwnerRegistrationSuccess(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Response is an array containing the ID of undeleted patient..
     * @summary undelete previously deleted patients
     * @param patientIds
     */
    undeletePatient(patientIds) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/patient/undelete/${encodeURIComponent(String(patientIds))}` + '?ts=' + new Date().getTime();
            let headers = yield this.headers;
            return XHR_1.XHR.sendCommand('PUT', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new DocIdentifier_1.DocIdentifier(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @internal this method is for internal use only and may be changed without notice
     */
    bulkSharePatients(request) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + '/patient/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, Patient_1.Patient)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @internal use mergePatients from the extended api instead
     */
    baseMergePatients(fromId, expectedFromRev, updatedInto) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = updatedInto;
            const _url = this.host +
                `/patient/mergeInto/${encodeURIComponent(String(updatedInto.id))}/from/${encodeURIComponent(String(fromId))}` +
                '?ts=' +
                new Date().getTime() +
                '&expectedFromRev=' +
                encodeURIComponent(String(expectedFromRev));
            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 Patient_1.Patient(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
}
exports.IccPatientApi = IccPatientApi;
//# sourceMappingURL=IccPatientApi.js.map