"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IccPubsubApi = 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 AuthenticationProvider_1 = require("../../icc-x-api/auth/AuthenticationProvider");
const IccRestApiPath_1 = require("./IccRestApiPath");
class IccPubsubApi {
    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;
    }
    /**
     * Offer auth data on previously agreed on secret bucket, data should be encrypted
     * @summary Offer auth data on secret bucket
     * @param body
     * @param bucket
     */
    offerAuth(bucket, body) {
        let _body = null;
        _body = body;
        const _url = this.host + `/pubsub/auth/${encodeURIComponent(String(bucket))}` + '?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/octet-stream'));
        return XHR_1.XHR.sendCommand('PUT', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
            .then((doc) => JSON.parse(JSON.stringify(doc.body)))
            .catch((err) => this.handleError(err));
    }
    /**
     * Publish value with key
     * @summary publish data
     * @param body
     * @param key
     */
    pub(key, body) {
        let _body = null;
        _body = body;
        const _url = this.host + `/pubsub/pub/${encodeURIComponent(String(key))}` + '?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/octet-stream'));
        return XHR_1.XHR.sendCommand('PUT', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
            .then((doc) => JSON.parse(JSON.stringify(doc.body)))
            .catch((err) => this.handleError(err));
    }
    /**
     * Recover auth data from bucket, data should be encrypted
     * @summary Recover auth data from secret bucket
     * @param bucket
     */
    recoverAuth(bucket) {
        let _body = null;
        const _url = this.host + `/pubsub/auth/recover/${encodeURIComponent(String(bucket))}` + '?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) => doc.body)
            .catch((err) => this.handleError(err));
    }
    /**
     * Try to get published data
     * @summary subscribe to data
     * @param key
     */
    sub(key) {
        let _body = null;
        const _url = this.host + `/pubsub/sub/${encodeURIComponent(String(key))}` + '?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) => doc.body)
            .catch((err) => this.handleError(err));
    }
}
exports.IccPubsubApi = IccPubsubApi;
//# sourceMappingURL=IccPubsubApi.js.map