var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { HttpClient, HttpBackend, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import { AbstractAppConfig } from '../../../app.config';
var DocumentStoreService = /** @class */ (function () {
    function DocumentStoreService(appConfig, httpClient, handler) {
        this.appConfig = appConfig;
        this.httpClient = httpClient;
        this.anonymousHttpClient = new HttpClient(handler);
    }
    DocumentStoreService.prototype.downloadUrl = function (docId) {
        var url = this.baseUrl();
        return this.httpClient.get(this.baseUrl() + "/" + docId);
    };
    DocumentStoreService.prototype.newUploadUrl = function (metadata) {
        var url = this.baseUrl();
        var body = this.appConfig.secureDocumentOnUpload() !== 'false' ? { metadata: metadata } : {};
        return this.httpClient.post(this.baseUrl(), body);
    };
    DocumentStoreService.prototype.upload = function (file, upload, metadata) {
        if (metadata === void 0) { metadata = { filename: undefined }; }
        var filename = metadata.filename;
        var headers = this.uploadHeaders(this.appConfig.secureDocumentOnUpload() !== 'false' ? metadata : { filename: filename });
        return this.anonymousHttpClient.put(upload.upload_url, file, { headers: headers })
            .pipe(map(function (response) { return upload.id; }));
    };
    DocumentStoreService.prototype.baseUrl = function () {
        if (this.appConfig.getAttachmentStoreUrl()) {
            return this.appConfig.getAttachmentStoreUrl() + '/attachments';
        }
        return this.appConfig.getDocumentStoreUrl() + '/documents';
    };
    DocumentStoreService.prototype.uploadHeaders = function (metadata) {
        var headers = {};
        switch (this.appConfig.getAttachmentStoreProvider()) {
            case 'azure-blob':
                headers = Object.assign({}, headers, this.azureBlobHeaders(), this.metadataHeaders('x-ms-meta-', metadata));
                break;
            case 'google-storage':
                headers = Object.assign({}, headers, this.metadataHeaders('x-goog-meta-', metadata));
                break;
        }
        return new HttpHeaders(headers);
    };
    DocumentStoreService.prototype.azureBlobHeaders = function () {
        return {
            'x-ms-blob-type': 'BlockBlob',
        };
    };
    DocumentStoreService.prototype.metadataHeaders = function (prefix, metadata) {
        return Object.keys(metadata)
            .map(function (key) { return [prefix + key, metadata[key]]; })
            .reduce(function (acc, _a) {
            var key = _a[0], value = _a[1];
            acc[key] = value;
            return acc;
        }, {});
    };
    DocumentStoreService = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [AbstractAppConfig,
            HttpClient,
            HttpBackend])
    ], DocumentStoreService);
    return DocumentStoreService;
}());
export { DocumentStoreService };
//# sourceMappingURL=document-store.service.js.map