"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.IccAgendaApi = 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 Agenda_1 = require("../model/Agenda");
const DocIdentifier_1 = require("../model/DocIdentifier");
const AuthenticationProvider_1 = require("../../icc-x-api/auth/AuthenticationProvider");
const IccRestApiPath_1 = require("./IccRestApiPath");
const PaginatedListAgenda_1 = require("../model/PaginatedListAgenda");
class IccAgendaApi {
    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 agenda
     * @param body
     */
    createAgenda(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/agenda` + '?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 Agenda_1.Agenda(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Delete a batch of agendas by id.
     *
     * @param agendaIds a ListOfIds containing the ids of the agendas to delete.
     * @return a Promise that will resolve in an array of DocIdentifiers of the successfully deleted agendas.
     */
    deleteAgendas(agendaIds) {
        return __awaiter(this, void 0, void 0, function* () {
            const headers = 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 + `/agenda/delete/batch` + '?ts=' + new Date().getTime(), headers, agendaIds, 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 agenda by id.
     *
     * @param agendaId the id of the agenda to delete.
     * @return a Promise that will resolve in the DocIdentifier of the deleted agenda.
     */
    deleteAgenda(agendaId) {
        return __awaiter(this, void 0, void 0, function* () {
            return XHR_1.XHR.sendCommand('DELETE', this.host + `/agenda/${encodeURIComponent(agendaId)}` + '?ts=' + new Date().getTime(), this.headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new DocIdentifier_1.DocIdentifier(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Gets an agenda
     * @param agendaId
     */
    getAgenda(agendaId) {
        let _body = null;
        const _url = this.host + `/agenda/${encodeURIComponent(String(agendaId))}` + '?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 Agenda_1.Agenda(doc.body))
            .catch((err) => this.handleError(err));
    }
    /**
     * @deprecated use {@link getAgendasWithPagination} instead.
     *
     * @summary Gets all agendas
     */
    getAgendas() {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/agenda` + '?ts=' + new Date().getTime() + '&limit=1000000';
            let 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 PaginatedListAgenda_1.PaginatedListAgenda(doc.body).rows) !== null && _a !== void 0 ? _a : []; })
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary gets all agendas with pagination.
     *
     * @param startDocumentId the startDocumentId provided by the previous page or null if it's the first page.
     * @param limit the size of the page
     * @return a promise that will resolve in a PaginatedListAgenda.
     */
    getAgendasWithPagination(startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/agenda` +
                '?ts=' +
                new Date().getTime() +
                (!!startDocumentId ? `&startDocumentId=${encodeURIComponent(startDocumentId)}` : '') +
                (!!limit ? `&limit=${limit}` : '');
            let headers = this.headers;
            return XHR_1.XHR.sendCommand('GET', _url, headers, null, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => new PaginatedListAgenda_1.PaginatedListAgenda(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Gets all agendas for user
     * @param userId
     */
    getAgendasForUser(userId) {
        let _body = null;
        const _url = this.host + `/agenda/byUser` + '?ts=' + new Date().getTime() + (userId ? '&userId=' + encodeURIComponent(String(userId)) : '');
        let headers = this.headers;
        return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
            .then((doc) => new Agenda_1.Agenda(doc.body))
            .catch((err) => this.handleError(err));
    }
    /**
     *
     * @summary Gets readable agendas for user
     * @param userId
     */
    getReadableAgendasForUser(userId) {
        let _body = null;
        const _url = this.host + `/agenda/readableForUser` + '?ts=' + new Date().getTime() + (userId ? '&userId=' + encodeURIComponent(String(userId)) : '');
        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 Agenda_1.Agenda(it)))
            .catch((err) => this.handleError(err));
    }
    /**
     *
     * @summary Modifies an agenda
     * @param body
     */
    modifyAgenda(body) {
        let _body = null;
        _body = body;
        const _url = this.host + `/agenda` + '?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 Agenda_1.Agenda(doc.body))
            .catch((err) => this.handleError(err));
    }
}
exports.IccAgendaApi = IccAgendaApi;
//# sourceMappingURL=IccAgendaApi.js.map