(function () {
    'use strict';

    function addHighlights(header, highlights) {
        if (!header || header.querySelector('.wnc-pricing-highlights') || !Array.isArray(highlights)) {
            return;
        }

        var list = document.createElement('div');
        list.className = 'wnc-pricing-highlights';

        highlights.forEach(function (highlight) {
            var item = document.createElement('span');
            item.className = 'wnc-pricing-highlight';
            item.textContent = highlight;
            list.appendChild(item);
        });

        header.appendChild(list);
    }

    function applyBranding() {
        if (typeof window.wncPricingBrand !== 'object' || !window.wncPricingBrand) {
            return;
        }

        var app = document.getElementById('fs_pricing_app');
        if (!app) {
            return;
        }

        app.classList.add('wnc-branded-pricing');

        var title = app.querySelector('.fs-page-title h1');
        var subtitle = app.querySelector('.fs-page-title h3');
        var pluginHeader = app.querySelector('.fs-plugin-title-and-logo');
        var logo = app.querySelector('.fs-plugin-logo');

        if (title) {
            title.textContent = window.wncPricingBrand.heading;
        }

        if (subtitle) {
            subtitle.textContent = window.wncPricingBrand.subheading;
        }

        if (logo && window.wncPricingBrand.logoUrl) {
            logo.src = window.wncPricingBrand.logoUrl;
            logo.alt = 'Where Next';
        }

        addHighlights(pluginHeader, window.wncPricingBrand.highlights);
    }

    function init() {
        applyBranding();

        var wrapper = document.getElementById('fs_pricing_wrapper');
        if (!wrapper || typeof MutationObserver === 'undefined') {
            return;
        }

        new MutationObserver(applyBranding).observe(wrapper, {
            childList: true,
            subtree: true
        });
    }

    if ('loading' === document.readyState) {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
}());
