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 { Drupal } from '../../lib/cms/drupal/drupal';
// import * as jDrupal from '../../../lib/vendor/jdrupal.min.js';
export class AppDrupal {
    constructor() {
        this.loading = true;
        this.authenticated = false;
    }
    componentDidLoad() {
        this.drupal = new Drupal();
        this.init(JSON.parse(this.configJson));
    }
    init(config) {
        this.drupal.init(config).then((arg) => {
            this.getData().then(() => {
                this.authenticated = this.drupal.authenticated;
                this.model = this.drupal.model;
                this.loading = false;
                console.log(this.model);
                setTimeout((arg) => {
                    this.loginModal = document.getElementById('drupal-login-modal');
                    document.addEventListener('loginShouldOccur', (e) => {
                        this.login(e.detail.username, e.detail.password);
                    });
                }, 200);
            });
        });
    }
    getData() {
        return __awaiter(this, void 0, void 0, function* () {
            //await this.drupal.jsonApi.getAllCollections();
            yield this.drupal.jsonApi.getCollection('view--view');
            yield this.drupal.nodeLoad(this.drupal.options.frontpage_nid);
            yield this.drupal.menuLoad(this.drupal.options.main_menu, true);
            this.currentNode = yield this.drupal.jsonApi.getNode(this.drupal.options.frontpage_nid);
            // await this.drupal.menuLoad(this.drupal.options.secondary_menu, true);
            return;
        });
    }
    login(username, password) {
        this.drupal.login(username, password).then((res) => {
            this.loginModal['hide']();
            this.authenticated = true;
        });
    }
    logout() {
        this.drupal.logout().then(() => {
            this.authenticated = false;
        });
    }
    onMenuClick(item) {
        return __awaiter(this, void 0, void 0, function* () {
            if (item.link.route_parameters.node) {
                this.currentNode = yield this.drupal.jsonApi.getNode(item.link.route_parameters.node);
            }
        });
    }
    showLoginModal() {
        this.loginModal['show']();
    }
    render() {
        console.log(this.match);
        if (this.model && this.match) {
            return (h("div", { class: 'ema-app-drupal' },
                h("ema-drupal-node", { class: "text-center", "data-json": JSON.stringify(this.currentNode) }),
                !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-modal", { id: 'drupal-login-modal', "modal-title": 'Login', width: '80%', "allow-close": "true" },
                    h("ema-login-form", { errorMessage: '' })),
                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.drupal.options.main_menu].map(item => h("li", null,
                                h("a", { onClick: (e) => this.onMenuClick(item), class: "nav-link" }, item.link.title))))),
                        h("button", { class: "navbar-toggler", type: "button", "data-toggle": "collapse", "data-target": "#navbarCollapse", "aria-controls": "navbarCollapse", "aria-expanded": "false", "aria-label": "Toggle navigation" },
                            h("span", { class: "navbar-toggler-icon" }))))));
        }
    }
    static get is() { return "ema-app-drupal"; }
    static get properties() { return {
        "authenticated": {
            "state": true
        },
        "configJson": {
            "type": "Any",
            "attr": "config-json"
        },
        "currentNode": {
            "state": true
        },
        "loading": {
            "state": true
        },
        "match": {
            "type": "Any",
            "attr": "match"
        },
        "model": {
            "state": true
        }
    }; }
    static get style() { return "/**style-placeholder:ema-app-drupal:**/"; }
}
//# sourceMappingURL=app-drupal.js.map