"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.IccPlaceApi = 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 Place_1 = require("../model/Place");
const AuthenticationProvider_1 = require("../../icc-x-api/auth/AuthenticationProvider");
const IccRestApiPath_1 = require("./IccRestApiPath");
const PaginatedListPlace_1 = require("../model/PaginatedListPlace");
const ConflictResolutionResult_1 = require("../model/ConflictResolutionResult");
class IccPlaceApi {
    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 place
     * @param body
     */
    createPlace(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/place` + '?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 Place_1.Place(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Deletes a batch of places.
     *
     * @param placeIds a ListOfIds containing the ids of the places to delete.
     * @return a Promise that will resolve in an array of DocIdentifiers of the successfully deleted places.
     */
    deletePlaces(placeIds) {
        return __awaiter(this, void 0, void 0, function* () {
            return XHR_1.XHR.sendCommand('POST', this.host + `/place/delete/batch` + '?ts=' + new Date().getTime(), this.headers.filter((h) => h.header !== 'Content-Type').concat(new XHR_1.XHR.Header('Content-Type', 'application/json')), placeIds, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
                .then((doc) => doc.body.map((it) => new DocIdentifier_1.DocIdentifier(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Gets a place
     * @param placeId
     */
    getPlace(placeId) {
        return __awaiter(this, void 0, void 0, function* () {
            let _body = null;
            const _url = this.host + `/place/${encodeURIComponent(String(placeId))}` + '?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 Place_1.Place(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @deprecated use {@link getPlacesWithPagination} instead.
     * @summary Gets all places
     */
    getPlaces() {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/place?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 PaginatedListPlace_1.PaginatedListPlace(doc.body).rows) !== null && _a !== void 0 ? _a : []; })
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * @summary Gets all places with pagination.
     * @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 PaginatedListPlace.
     */
    getPlacesWithPagination(startDocumentId, limit) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host +
                `/place?ts=${new Date().getTime()}` +
                (!!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 PaginatedListPlace_1.PaginatedListPlace(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     *
     * @summary Modifies an place
     * @param body
     */
    modifyPlace(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/place` + '?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 Place_1.Place(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Retrieves the ids of all the places that currently have conflicting revisions and therefore need to
     * be resolved.
     * @summary List the ids of the places that have conflicts.
     * @return the ids of the places with unresolved conflicts.
     */
    getConflictingEntitiesIds() {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/place/conflicts` + '?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));
        });
    }
    /**
     * Retrieves all the conflicting revisions of the place with the given id (the current revision is not
     * included). The returned entities are still encrypted.
     * @summary Get the conflicting revisions of a place.
     * @param entityId the id of the place to retrieve the conflicts for.
     * @return the conflicting revisions of the place.
     */
    getConflictsForEntity(entityId) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/place/conflicts/${encodeURIComponent(String(entityId))}` + '?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.map((it) => new Place_1.Place(it)))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Resolves a conflict by declaring which revision of the place should be kept as winner and which
     * conflicting revisions should be purged. The provided document must already be encrypted.
     * @summary Declare the winning revision of a conflicting place.
     * @param body the {@link ConflictResolutionRequest} carrying the winning revision and the conflicts to purge.
     * @return the {@link ConflictResolutionResult} with the saved winner and the conflicts that are still unresolved.
     */
    declareConflictWinner(body) {
        return __awaiter(this, void 0, void 0, function* () {
            const _url = this.host + `/place/conflicts/winner` + '?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 ConflictResolutionResult_1.ConflictResolutionResult(doc.body, (x) => new Place_1.Place(x)))
                .catch((err) => this.handleError(err));
        });
    }
}
exports.IccPlaceApi = IccPlaceApi;
//# sourceMappingURL=IccPlaceApi.js.map