"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var storage_1 = require("../util/storage");
var Wordpress = /** @class */ (function () {
    function Wordpress() {
        this.model = { menus: {}, params: {}, collections: { post: {} } };
    }
    Wordpress.prototype.init = function (options) {
        var self = this;
        if (typeof window !== 'undefined') {
            window['wp'] = window['wp'] || {};
        }
        return new Promise(function (resolve, reject) {
            self.initApi(options).then(function () {
                if (options.app_server) {
                    self.getParams().then(function () {
                        self.getMenus().then(function () {
                            self.ready = true;
                            resolve();
                        });
                    });
                }
                else {
                    resolve();
                }
            });
        });
    };
    Wordpress.prototype.initApi = function (settings) {
        var self = this;
        this.settings = settings;
        this.endpoint = this.settings.protocol + '//' + this.settings.host + '/index.php/wp-json/';
        return new Promise(function (resolve, reject) {
            if (typeof window !== 'undefined' && !window['WPAPI']) {
                console.error('ERROR: WPAPI is not defined as global');
                reject();
            }
            if (location && location.origin === self.settings.protocol + '//' + self.settings.host) {
                // works only on same domain as wp
                self.discover().then(function () {
                    self.api = new WPAPI({ endpoint: self.endpoint });
                    self.initCustomRoutes();
                    resolve();
                });
            }
            else {
                self.api = new WPAPI({ endpoint: self.endpoint });
                self.initCustomRoutes();
                resolve();
            }
        });
    };
    Wordpress.prototype.initCustomRoutes = function () {
        // app-server plugin
        this.api.params = this.api.registerRoute('app-server/v1', '/params');
        this.api.menus = this.api.registerRoute('app-server/v1', '/menus/(?P<id>[a-zA-Z(-]+)');
        // pods plugin: custom content types
        this.api.apps = this.api.registerRoute('wp/v2', '/apps/(?P<id>\\d+)');
        this.api.iframes = this.api.registerRoute('wp/v2', '/iframes/(?P<id>\\d+)');
    };
    Wordpress.prototype.getPosts = function () {
        var self = this;
        return new Promise(function (resolve, reject) {
            self.api.posts().then(function (posts) {
                var items = [];
                posts.forEach(function (item) {
                    if (item.id) {
                        self.model.collections.post[item.id] = item;
                        items.push(item);
                    }
                });
                resolve(items);
            });
        });
    };
    Wordpress.prototype.getParams = function () {
        var self = this;
        return new Promise(function (resolve, reject) {
            self.api.params().then(function (params) {
                self.model.params = params;
                resolve(params);
            });
        });
    };
    Wordpress.prototype.getPost = function (id, type) {
        var self = this;
        if (!this.model.collections[type]) {
            this.model.collections[type] = {};
        }
        return new Promise(function (resolve, reject) {
            function parse(id, type, object) {
                self.model.collections[type][id] = object;
                resolve(object);
            }
            if (self.settings.cache === true && self.model.collections[type][id]) {
                resolve(self.model.collections[type][id]);
            }
            else {
                if (type === 'post') {
                    self.api.posts().id(id).then(function (res) { return parse(id, type, res); });
                }
                if (type === 'page') {
                    self.api.pages().id(id).then(function (res) { return parse(id, type, res); });
                }
                if (type === 'app') {
                    self.api.apps().id(id).then(function (res) { return parse(id, type, res); });
                }
                if (type === 'iframe') {
                    self.api.iframes().id(id).then(function (res) { return parse(id, type, res); });
                }
            }
        });
    };
    ;
    Wordpress.prototype.getMenu = function (mid) {
        var self = this;
        return new Promise(function (resolve, reject) {
            self.api.menus().id(mid).then(function (menu) {
                self.model.menus[mid] = menu;
                resolve(self.model.menus[mid]);
            });
        });
    };
    Wordpress.prototype.getMenus = function () {
        var self = this;
        return new Promise(function (resolve, reject) {
            var mids = [];
            var i = 0;
            if (!self.model.params.menus.length) {
                resolve(self.model.menus);
                return;
            }
            self.model.params.menus.forEach(function (menu) {
                mids.push(menu.slug);
                // mids.push(menu.term_id);
            });
            mids.forEach(function (mid) {
                self.getMenu(mid).then(function (menu) {
                    self.model.menus[mid] = menu;
                    i++;
                    if (i === mids.length) {
                        resolve(self.model.menus);
                    }
                });
            });
        });
    };
    Wordpress.prototype.saveLocal = function () {
        storage_1.Storage.saveLocalModel(this.model, this.model.params.baseUrl);
    };
    Wordpress.prototype.restoreLocal = function () {
        this.model = storage_1.Storage.restoreLocalModel(this.model.params.baseUrl);
    };
    Wordpress.prototype.discover = function () {
        var self = this;
        var apiPromise = WPAPI.discover(this.settings.protocol + '//' + this.settings.host);
        return new Promise(function (resolve, reject) {
            apiPromise.then(function (site) {
                self.site = site;
                resolve();
            });
        });
    };
    return Wordpress;
}());
exports.Wordpress = Wordpress;
//# sourceMappingURL=wordpress.js.map