import { fromEvent, timer } from 'rxjs';
import { DomUtil, PermissionsService, User, Util } from "@ema/core";
import { BaseService } from "@ema/core";
const singleton = Symbol();
const singletonEnforcer = Symbol();
export class AppService {
    constructor(enforcer) {
        this.state = {};
        this.baseUrl = '';
        this.loading = true;
        this.authenticated = false;
        this.errorMessage = '';
        if (enforcer !== singletonEnforcer) {
            throw new Error('Cannot construct singleton');
        }
        this.base = BaseService.instance;
        this.permissions = PermissionsService.instance;
        this.translation = this.base.translation;
        this.config = this.base.config;
        this.user = new User('visitor');
    }
    async init() {
        await this.base.initApp(this.baseUrl + '/assets/config.json', true, '_emaApp');
        await this.permissions.loadRules(this.baseUrl + '/assets/permissions.json', true, 'rules');
        this.base.translation.use(this.config.get('language'));
        this.data = {};
        this.data.manifest = await Util.fetchJson(this.baseUrl + '/assets/collection-manifest.json');
        this.data.mockData = await Util.fetchJson(this.baseUrl + '/assets/data/mock-data.json');
        this.data.dwpData = await Util.fetchJson(this.baseUrl + '/assets/data/mock-dwp.json');
        this.data.swaggerData = await Util.fetchJson(this.baseUrl + '/assets/data/apiSwagger.json');
        this.collectionManifest = this.data.manifest;
        this.parseComponents();
        this.initHistory();
        this.initSketchListeners();
        if (window.document.location.hostname.match('localhost')) {
            this.config.set('dev', true);
            DomUtil.addClass(document.getElementsByTagName('body')[0], 'dev');
        }
        this.state.initialized = true;
        timer(200).subscribe(() => {
            this.initLoginForm();
        });
    }
    initLoginForm() {
        this.loginForm = document.getElementById('app-login-form');
        fromEvent(this.loginForm, 'formSubmit').subscribe((e) => {
            console.log('app-login-form', e.detail);
        });
    }
    initSketchListeners() {
        fromEvent(document, 'sketch-artboard-init').subscribe((event) => {
            console.log('sketch init: ', event.detail.selector);
        });
        fromEvent(document, 'sketch-artboard-update').subscribe((event) => {
            console.log('sketch update: ', event.detail.selector);
        });
        fromEvent(document, 'svgElementClick').subscribe((event) => {
            let id = event.detail.id;
            let el = event.detail.el;
            if (id.match('btn-logo')) {
                this.showFlashMessage('Called from sketch: id=' + id, 'primary', 2000);
            }
            if (id.match('icon-sketch')) {
            }
            console.log('svg: id = ', event.detail.id);
        });
    }
    initHistory() {
        if (typeof window !== 'undefined' && window.history) {
        }
    }
    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;
    }
    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];
    }
}
