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 } from '../../../lib/index';
import { Wordpress } from '../../../lib/cms/wordpress';
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('loginShouldOccur', (e) => {
            this.login(e.detail.username, e.detail.password);
        });
    }
    componentWillLoad() {
        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;
                console.log(this.wordpress);
            });
        });
    }
    componentDidLoad() {
    }
    getData() {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.wordpress.getPosts().then((res) => {
                this.posts = res;
                this.wordpress.saveLocal();
            });
            return;
        });
    }
    login(username, password) {
        console.log(username, password);
        // this.wordpress.login(username, password).then((res) => {
        //     this.loginModal['hide']();
        //     this.authenticated = true;
        // });
    }
    logout() {
        // this.wordpress.logout().then(() => {
        //     this.authenticated = false;
        // })
    }
    onMenuClick(item) {
        return __awaiter(this, void 0, void 0, function* () {
            this.currentPost = yield this.wordpress.getPost(item.object_id, item.object);
        });
    }
    showLoginModal() {
        this.loginModal['show']();
    }
    // @Listen('loginShouldOccur')
    // onLoginShouldOccur(e) {
    //     console.log(e);
    // }
    render() {
        if (this.model && this.match && this.match.params.pid) {
            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
        },
        "loading": {
            "state": true
        },
        "match": {
            "type": "Any",
            "attr": "match"
        },
        "model": {
            "state": true
        }
    }; }
    static get style() { return "/**style-placeholder:ema-app-wordpress:**/"; }
}
//# sourceMappingURL=app-wordpress.js.map