$(() => {
    $(".recipeRow").each(function () {
        const iconsColumn = $(this).find(".iconsColumn")
        if (!$(this).data("approbationdate")) {
            const icon = $("<img />")
                .attr({
                    src: recipeNotApprovedIconPath,
                    title: recipeNotApproved
                })
            iconsColumn.append(icon)
        }
    });
});

function closeTabs() {
    $(".recipesList").each(function () {
        $(this).hide();
    });
}

function openTabs() {
    $(".recipesList").each(function () {
        $(this).show();
    });
}

const companyRows = $(".companyTab");

function performSearch(searchString) {
    let found = false;
    let emptySearch = false;

    const recipesContainer = $(".recipesContainer:not(.researchEnabled)");
    if (recipesContainer) recipesContainer.addClass("researchEnabled");

    document.querySelectorAll(".researchResult").forEach(t => {
        t.classList.remove("researchResult");
    });

    companyRows.each(function () {
        const company = $(this);
        const recipeRows = company.find(".recipeRow");

        if (!searchString || !searchString.replace(/\s/g, '').length) { // Si la recherche est vide
            company.addClass("researchResult");

            recipeRows.each(function () {
                $(this).addClass("researchResult");
            });

            found = true;
            emptySearch = true;
        } else if (searchIn(searchString, company.find("h2").text())) { // Si la recherche correspondant à une bannière
            company.addClass("researchResult");

            recipeRows.each(function () {
                $(this).addClass("researchResult");
            });

            found = true;
        } else {    // Sinon on fait une recherche dans les recettes
            recipeRows.each(function () {
                const recipe = $(this);

                $(this).find(".descriptionCell").each(function () {
                    if (searchIn(searchString, $(this).text())) {
                        company.addClass("researchResult");
                        recipe.addClass("researchResult");
                        found = true;
                    }
                });
            });
        }
    });

    if (!found) showMessage(warningMsg, researchNotFound, false);
    else hideMessage(warningMsg);

    if (emptySearch) closeTabs();
    else openTabs();
}
