var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { ConfigService, configDefaults } from '../lib/index';
// import { UaasService } from "../lib/uaas/uaas-service";
import { Util } from "../lib";
const singleton = Symbol();
const singletonEnforcer = Symbol();
export class AppService {
    constructor(enforcer) {
        this.baseUrl = '';
        this.loading = true;
        // public uaas: UaasService;
        this.authenticated = false;
        this.errorMessage = '';
        if (enforcer !== singletonEnforcer) {
            throw new Error('Cannot construct singleton');
        }
        this._type = 'AppService';
        // this.uaas = UaasService.instance;
        this.config = ConfigService.instance;
        this.config.setDefaults(configDefaults);
    }
    init(baseUrl) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.config.fetch(this.baseUrl + '/assets/config.json');
            this.baseUrl = baseUrl;
            this.data = {};
            this.data.manifest = yield this.fetchJson(this.baseUrl + '/assets/collection-manifest.json');
            this.data.mockData = yield this.fetchJson(this.baseUrl + '/assets/data/mock-data.json');
            this.data.dwpData = yield this.fetchJson(this.baseUrl + '/assets/data/mock-dwp.json');
            this.data.swaggerData = yield this.fetchJson(this.baseUrl + '/assets/data/uaasApiSwagger.json');
            // let swaggerData = await this.fetchJson(this.baseUrl + '/assets/data/mock-swagger.json');
            this.collectionManifest = this.data.manifest;
            this.parseComponents();
            this.initHistory();
            if (document.location.hostname.match('localhost'))
                this.config.set('dev', true);
            return this.data;
        });
    }
    // todo: set router classes on body
    initHistory() {
        if (typeof window !== 'undefined' && window.history) {
            // window.addEventListener("pushstate", function(e) {
            //     console.log(JSON.parse(window.history.state));
            // });
            // window.addEventListener("popstate", function(e) {
            //     console.log(JSON.parse(window.history.state));
            // });
        }
    }
    parseComponents() {
        let allComps = Util.copy(this.data.manifest.components);
        let components = [];
        allComps.map((c) => {
            let tag = c.tag;
            let ignore = false;
            this.config.get('componentsIgnorePatterns').forEach((p) => {
                if (tag.match(p)) {
                    ignore = true;
                }
            });
            if (!ignore) {
                components.push(c);
            }
        });
        this.components = components;
    }
    fetchJson(url) {
        return __awaiter(this, void 0, void 0, function* () {
            const response = yield fetch(url);
            const json = yield response.json();
            return json;
        });
    }
    showFlashMessage(message, type, duration = 2000) {
        document.querySelector('ema-flash-message').show(message, type, duration);
    }
    static get instance() {
        if (!this[singleton]) {
            this[singleton] = new AppService(singletonEnforcer);
        }
        return this[singleton];
    }
    get type() {
        return this._type;
    }
    set type(value) {
        this._type = value;
    }
}
//# sourceMappingURL=app-service.js.map