System.register(["@angular/core", "@angular/common/http", "rxjs", "@stratusjs/runtime/stratus", "lodash"], function (exports_1, context_1) {
    "use strict";
    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);
    };
    var core_1, http_1, rxjs_1, stratus_1, lodash_1, BackendService;
    var __moduleName = context_1 && context_1.id;
    return {
        setters: [
            function (core_1_1) {
                core_1 = core_1_1;
            },
            function (http_1_1) {
                http_1 = http_1_1;
            },
            function (rxjs_1_1) {
                rxjs_1 = rxjs_1_1;
            },
            function (stratus_1_1) {
                stratus_1 = stratus_1_1;
            },
            function (lodash_1_1) {
                lodash_1 = lodash_1_1;
            }
        ],
        execute: function () {
            BackendService = class BackendService {
                constructor(http) {
                    this.http = http;
                    this.title = 'backend';
                    this.uid = lodash_1.default.uniqueId('sa_backend_service_');
                    stratus_1.Stratus.Instances[this.uid] = this;
                    this.cache = {};
                }
                get(url) {
                    if (!lodash_1.default.isString(url)) {
                        console.error('url is not a string:', url);
                        return;
                    }
                    if (url in this.cache) {
                        return this.cache[url].observable;
                    }
                    const now = new Date();
                    const query = this.cache[url] = {
                        fetched: now.valueOf(),
                        observable: new rxjs_1.Observable((subscriber) => this.getData(url, subscriber))
                    };
                    return query.observable;
                }
                getData(url, subscriber) {
                    if (!lodash_1.default.isString(url)) {
                        console.error('url is not a string:', url);
                        return;
                    }
                    if (!(url in this.cache)) {
                        return;
                    }
                    const query = this.cache[url];
                    if ('data' in query) {
                        subscriber.next(query.data);
                        subscriber.complete();
                        return;
                    }
                    this.http
                        .get(url, { observe: 'response' })
                        .subscribe((response) => {
                        query.data = response;
                        subscriber.next(response);
                        subscriber.complete();
                    });
                }
            };
            BackendService = __decorate([
                core_1.Injectable({
                    providedIn: 'root'
                }),
                __metadata("design:paramtypes", [http_1.HttpClient])
            ], BackendService);
            exports_1("BackendService", BackendService);
        }
    };
});

//# sourceMappingURL=backend.service.js.map
