import { ConfigService, Wordpress } from '@ema/core';
export class AppWordpress {
    constructor() {
        this.loading = true;
        this.authenticated = false;
        this.config = ConfigService.instance;
        this.wordpress = new Wordpress();
        this.loginModal = document.getElementById('login-modal');
        document.addEventListener('loginFormSubmit', (e) => {
            this.login(e.detail.username, e.detail.password);
        });
    }
    componentWillLoad() {
        let router = document.getElementsByTagName('ema-router')[0];
        router.getState().then((routerState) => {
            this.currentPostId = routerState.context.params.pid;
        });
        this.wordpress.init(this.config.get('wordpress')['wordpress-dev-local']).then((arg) => {
            this.getData().then(() => {
                this.authenticated = this.wordpress.authenticated;
                let frontpage = this.wordpress.settings.frontpage;
                this.currentPost = this.wordpress.model.collections[frontpage.type][frontpage.id];
                this.model = this.wordpress.model;
                this.loading = false;
            });
        });
    }
    componentDidLoad() {
    }
    async getData() {
        await this.wordpress.getPosts().then((res) => {
            this.posts = res;
            this.wordpress.saveLocal();
        });
        return;
    }
    login(username, password) {
        console.log(username, password);
    }
    logout() {
    }
    async onMenuClick(item) {
        this.currentPost = await this.wordpress.getPost(item.object_id, item.object);
    }
    showLoginModal() {
        this.loginModal['show']();
    }
    render() {
        if (this.model && this.currentPostId) {
            return (h("div", { class: 'ema-app-wordpress' },
                !this.authenticated &&
                    h("button", { class: "btn btn-primary float-right", id: "login-button", onClick: () => this.showLoginModal() }, "login"),
                this.authenticated &&
                    h("button", { class: "btn btn-primary float-right", id: "logout-button", onClick: () => this.logout() }, "logout"),
                h("ema-post", { "data-json": JSON.stringify(this.currentPost) }),
                h("footer", null,
                    h("nav", { class: "navbar navbar-expand-md fixed-bottom" },
                        h("div", { class: "collapse navbar-collapse", id: "navbarCollapse" },
                            h("ul", { class: "navbar-nav" }, this.model.menus[this.wordpress.settings.mainMenu].items.map(item => h("li", null,
                                h("a", { onClick: (e) => this.onMenuClick(item), class: "nav-link" }, item.title)))))))));
        }
    }
    static get is() { return "ema-app-wordpress"; }
    static get properties() { return {
        "authenticated": {
            "state": true
        },
        "currentPost": {
            "state": true
        },
        "currentPostId": {
            "state": true
        },
        "loading": {
            "state": true
        },
        "model": {
            "state": true
        }
    }; }
    static get style() { return "/**style-placeholder:ema-app-wordpress:**/"; }
}
