(function () {
    "use strict";

    function normalize(text) {
        return (text || "")
            .toLowerCase()
            .replace(/[-_]+/g, " ")
            .replace(/[^a-z0-9 ]/g, " ")
            .replace(/\s+/g, " ")
            .trim();
    }

    function getScore(item, query) {

        var title = normalize(item.title);
        var keywords = normalize(item.keywords);
        var url = normalize(item.url);

        var terms = normalize(query)
            .split(" ")
            .filter(Boolean);

        if (!terms.length) return 0;

        var score = 0;

        for (var i = 0; i < terms.length; i++) {

            var term = terms[i];

            if (
                title.indexOf(term) === -1 &&
                keywords.indexOf(term) === -1 &&
                url.indexOf(term) === -1
            ) {
                return 0;
            }

            if (title.indexOf(term) !== -1) score += 10;
            if (keywords.indexOf(term) !== -1) score += 5;
            if (url.indexOf(term) !== -1) score += 2;
        }

        return score;
    }


    function createResult(item) {

        var link = document.createElement("a");

        link.href = item.url.replace(/\.html$/, "");

        link.style.cssText = `
            display:block !important;
            width:100% !important;
            box-sizing:border-box !important;
            padding:16px 18px !important;
            margin:4px 0 !important;
            background:#ffffff !important;
            color:#111827 !important;
            text-decoration:none !important;
            border:1px solid #e5e7eb !important;
            border-radius:10px !important;
            opacity:1 !important;
            visibility:visible !important;
            text-shadow:none !important;
            font-family:Arial,sans-serif !important;
        `;


        var title = document.createElement("div");

        title.textContent = item.title;

        title.style.cssText = `
            display:block !important;
            color:#111827 !important;
            background:transparent !important;
            font-size:17px !important;
            font-weight:700 !important;
            line-height:1.4 !important;
            margin-bottom:7px !important;
            opacity:1 !important;
            visibility:visible !important;
            text-shadow:none !important;
        `;


        var url = document.createElement("div");

        url.textContent = item.url.replace(".html", "");

        url.style.cssText = `
            display:block !important;
            color:#6b7280 !important;
            background:transparent !important;
            font-size:13px !important;
            font-weight:400 !important;
            opacity:1 !important;
            visibility:visible !important;
            text-shadow:none !important;
        `;


        link.appendChild(title);
        link.appendChild(url);


        link.addEventListener("mouseenter", function () {

            link.style.background = "#f3f4f6";

        });


        link.addEventListener("mouseleave", function () {

            link.style.background = "#ffffff";

        });


        return link;
    }


    function initializeSearch(searchBox) {

        var input =
            searchBox.querySelector(".eit-search-input");

        var button =
            searchBox.querySelector(".eit-search-btn");

        var results =
            searchBox.querySelector(".eit-search-results");


        if (!input || !results) return;


        var data = window.SITE_SEARCH_DATA || [];


        results.style.cssText = `
            display:none;
            position:absolute !important;
            top:calc(100% + 10px) !important;
            left:0 !important;
            right:auto !important;
            width:100% !important;
            min-width:300px !important;
            max-height:450px