(function (paw, utils, DOMPurify) {
    function PaHostUpgradeNewsComponent($elt, localization) {
        this.$elt = $elt;
        this.news = null;
        this.localization = paw.lang.append({
            linkLabel: '@Loc.HostUpgradeLinkLabel'
        }, localization || {});
        this.isFetchingHostUpgradeNews = false;
        this.lastHost = null;
    }

    PaHostUpgradeNewsComponent.prototype.init = function () {
        
    };

    PaHostUpgradeNewsComponent.prototype.fetchHostUpgradeNews = function (host) {
        var self = this;
        this.isFetchingHostUpgradeNews = true;
        this.lastHost = host;
        this.preloader(utils.value.isSet(this.news));
        return utils.async.get('/PaNews/GetHostUpgradeNews' + qparams({
            host: host
        })).then(function (news) {
            self.news = news;
            if (utils.value.isSet(self.news) && self.lastHost !== self.news.Host) {
                return;
            }
            if (utils.value.isSet(self.news.Message)) {
                self.$elt.html(self.$view());
            }
            else {
                self.clearNews();
            }
        }).catch(function (e) {
            self.clearNews();
            console.error(e);
        }).finally(function () {
            self.isFetchingHostUpgradeNews = false;
        });
    };

    PaHostUpgradeNewsComponent.prototype.$view = function () {
        var markup = [];
        markup.push('<div class="login-hosting-upgrade-notif animated fadeInDown">');
        markup.push('   <div class="login-hu-notif-col">');
        markup.push('      <div class="login-hu-notif-icon login-hu-notif-icon-bell"></div>');
        markup.push('   </div>');
        markup.push('   <div class="login-hu-notif-col login-hu-notif-msg">');
        markup.push('      <div>');
        markup.push('         <span>' + this.news.Message + '</span>');
        markup.push('      </div>');
        if (paw.utils.value.isSet(this.news.URL)) {
            markup.push('      <div>');
            markup.push('         <a href="' + this.news.URL + '" target="_blank">' + this.localization.linkLabel + '</a>');
            markup.push('      </div>');
        }
        markup.push('   </div>');
        markup.push('</div>');
        return DOMPurify.sanitize(markup.join('\n'));
    };

    PaHostUpgradeNewsComponent.prototype.clearNews = function () {
        this.news = null;
        this.$elt.empty();
    };

    PaHostUpgradeNewsComponent.prototype.preloader = function (show) {
        if (show) {
            this.$elt.append('<div class="login-host-upgrade-notif-preloader-holder"><div class="login-host-upgrade-notif-preloader"><i class="fa fa-spinner fa-spin"></div></i></div>');
            return;
        }
        this.$elt.empty();
    };

    paw.com.paNews.PaHostUpgradeNewsComponent = PaHostUpgradeNewsComponent;
})(paw, paw.utils, DOMPurify);