"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.IccDeviceApi = 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 Device_1 = require("../model/Device");
const DocIdentifier_1 = require("../model/DocIdentifier");
const IdWithRev_1 = require("../model/IdWithRev");
const PaginatedListDevice_1 = require("../model/PaginatedListDevice");
const AuthenticationProvider_1 = require("../../icc-x-api/auth/AuthenticationProvider");
const IccRestApiPath_1 = require("./IccRestApiPath");
class IccDeviceApi {
    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;
    }
    /**
     * Name, last name, date of birth, and gender are required. After creation of the device and obtaining the ID, you need to create an initial delegation.
     * @summary Create a device
     * @param body
     */
    createDevice(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/device` + '?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 Device_1.Device(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * One of Name or Last name+First name, Nihii, and Public key are required.
     * @summary Create a device
     * @param body
     * @param groupId
     */
    createDeviceInGroup(groupId, body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/device/inGroup/${encodeURIComponent(String(groupId))}` + '?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 Device_1.Device(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Returns the id and _rev of created devices
     * @summary Create devices in bulk
     * @param body
     */
    createDevices(body) {
        let _body = null;
        _body = body;
        const _url = this.host + `/device/batch` + '?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) => doc.body.map((it) => new IdWithRev_1.IdWithRev(it)))
            .catch((err) => this.handleError(err));
    }
    /**
     * Returns the id and _rev of created devices
     * @summary Create devices in bulk
     * @param body
     */
    createDevices1(body) {
        let _body = null;
        _body = body;
        const _url = this.host + `/device/bulk` + '?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) => doc.body.map((it) => new IdWithRev_1.IdWithRev(it)))
            .catch((err) => this.handleError(err));
    }
    /**
     * Response contains the id/rev of deleted device.
     * @summary Delete device.
     * @param deviceId
     */
    deleteDevice(deviceId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/device/${encodeURIComponent(String(deviceId))}` + '?ts=' + new Date().getTime();
            let headers = this.headers;
            return XHR_1.XHR.sendCommand('DELETE', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new DocIdentifier_1.DocIdentifier(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Response is an array containing the id/rev of deleted devices.
     * @summary Delete devices.
     * @param body
     */
    deleteDevices(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/device/delete/batch` + '?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) => doc.body.map((it) => new DocIdentifier_1.DocIdentifier(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Deleting a device. Response is an array containing the id of deleted device.
     * @summary Delete a device
     * @param groupId
     * @param deviceIds
     */
    deleteDevicesInGroup(groupId, deviceIds) {
        let _body = null;
        const _url = this.host + `/device/inGroup/${encodeURIComponent(String(groupId))}/${encodeURIComponent(String(deviceIds))}` + '?ts=' + new Date().getTime();
        let headers = this.headers;
        return XHR_1.XHR.sendCommand('DELETE', _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));
    }
    filterDevicesBy(startDocumentId, limit, body, collectTiming = false) {
        let _body = null;
        _body = body;
        const _url = this.host +
            `/device/filter` +
            '?ts=' +
            new Date().getTime() +
            (startDocumentId ? '&startDocumentId=' + encodeURIComponent(String(startDocumentId)) : '') +
            (limit ? '&limit=' + encodeURIComponent(String(limit)) : '');
        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(), undefined, false, collectTiming ? ['x-filter-timing-*'] : [])
            .then((doc) => Object.assign(new PaginatedListDevice_1.PaginatedListDevice(doc.body), collectTiming ? { responseHeaders: doc.responseHeaders } : {}))
            .catch((err) => this.handleError(err));
    }
    /**
     * It gets device administrative data.
     * @summary Get Device
     * @param deviceId
     */
    getDevice(deviceId) {
        let _body = null;
        const _url = this.host + `/device/${encodeURIComponent(String(deviceId))}` + '?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 Device_1.Device(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 deviceId
     */
    getDeviceAesExchangeKeysForDelegate(deviceId) {
        let _body = null;
        const _url = this.host + `/device/${encodeURIComponent(String(deviceId))}/aesExchangeKeys` + '?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) => JSON.parse(JSON.stringify(doc.body)))
            .catch((err) => this.handleError(err));
    }
    /**
     * It gets device administrative data.
     * @summary Get devices by id
     * @param body
     */
    getDevices(body) {
        let _body = null;
        _body = body;
        const _url = this.host + `/device/byIds` + '?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) => doc.body.map((it) => new Device_1.Device(it)))
            .catch((err) => this.handleError(err));
    }
    /**
     * General information about the device
     * @summary Get devices by their IDs
     * @param body
     * @param groupId
     */
    getDevicesInGroup(groupId, body) {
        let _body = null;
        _body = body;
        const _url = this.host + `/device/inGroup/${encodeURIComponent(String(groupId))}/byIds` + '?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) => doc.body.map((it) => new Device_1.Device(it)))
            .catch((err) => this.handleError(err));
    }
    matchDevicesBy(body, collectTiming = false) {
        let _body = null;
        _body = body;
        const _url = this.host + `/device/match` + '?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(), 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));
    }
    /**
     * No particular return value. It's just a message.
     * @summary Modify a Device.
     * @param body
     * @param groupId
     */
    modifyDeviceInGroup(groupId, body) {
        let _body = null;
        _body = body;
        const _url = this.host + `/device/inGroup/${encodeURIComponent(String(groupId))}` + '?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 Device_1.Device(doc.body))
            .catch((err) => this.handleError(err));
    }
    /**
     * Returns the updated device
     * @summary Modify a device
     * @param body
     */
    updateDevice(body) {
        let _body = null;
        _body = body;
        const _url = this.host + `/device` + '?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 Device_1.Device(doc.body))
            .catch((err) => this.handleError(err));
    }
    /**
     * Returns the id and _rev of modified devices
     * @summary Modify devices in bulk
     * @param body
     */
    updateDevices(body) {
        let _body = null;
        _body = body;
        const _url = this.host + `/device/batch` + '?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) => doc.body.map((it) => new IdWithRev_1.IdWithRev(it)))
            .catch((err) => this.handleError(err));
    }
    /**
     * Returns the id and _rev of modified devices
     * @summary Modify devices in bulk
     * @param body
     */
    updateDevices1(body) {
        let _body = null;
        _body = body;
        const _url = this.host + `/device/bulk` + '?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) => doc.body.map((it) => new IdWithRev_1.IdWithRev(it)))
            .catch((err) => this.handleError(err));
    }
}
exports.IccDeviceApi = IccDeviceApi;
//# sourceMappingURL=IccDeviceApi.js.map