(function () {
    function normalizeText(value) {
        return (value || "")
            .replace(/\s+/g, " ")
            .replace(/\u00a0/g, " ")
            .trim()
            .toLowerCase();
    }

    function isPlaceholderText(text) {
        if (!text) {
            return true;
        }

        return text === "vidéo" ||
            text === "plus d'information" ||
            text === "plus d’information" ||
            text.indexOf("aucune vidéo fiable") !== -1 ||
            text.indexOf("aucune vidéo source fiable") !== -1;
    }

    function hasUsefulVideo(section) {
        return Boolean(section.querySelector("iframe[src], video source[src], video[src]"));
    }

    function removeEmptyVideoSection() {
        var page = document.querySelector(".MoveAndStudyCore-highschool-program");
        var section;
        var introNode;
        var embedNode;
        var intro;
        var embed;
        var text;

        if (!page) {
            return;
        }

        section = page.querySelector(".mas-highschool-program-video");
        if (!section || hasUsefulVideo(section)) {
            return;
        }

        introNode = section.querySelector("#HSP-Video-Intro");
        embedNode = section.querySelector("#HSP-Video-Embed");
        intro = normalizeText(introNode ? introNode.textContent : "");
        embed = normalizeText(embedNode ? embedNode.textContent : "");
        text = normalizeText(section.textContent);

        if (isPlaceholderText(intro) && isPlaceholderText(embed || text)) {
            section.parentNode.removeChild(section);
        }
    }

    if (document.readyState === "loading") {
        document.addEventListener("DOMContentLoaded", removeEmptyVideoSection);
    } else {
        removeEmptyVideoSection();
    }
}());
