/**
 * Custom javascript for the entire website.
 * Add to this file for javascript that will be ran globally.
 */


$(document).ready(() => {
    activateTooltips();
    pageLoadMotionEffects();

    // $("select").select2({theme: "bootstrap-5", minimumResultsForSearch: -1})

    $("#headerTitle").text(document.title.split(" | ")[0]);
});


function activateTooltips() {
    // Enable all tooltips on the page
    const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
    const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
}

/**
 * Returns boolean if the string is empty or only contains whitespace.
 * 
 * @param {string} string - The string to check against.
 * @returns {boolean} - The result as boolean
*/
function isEmptyOrSpaces(string) {
    return string === null || string.match(/^ *$/) !== null;
}

function pageLoadMotionEffects() {
    var useMotion = localStorage.getItem("motion");

    if (useMotion === null) {
        useMotion = true
    }

    setMotionEffects(useMotion);
}

function setMotionEffects(enable) {

    localStorage.setItem("motion", enable)

    if (enable) {
        $(".fluid-hover-zoom-off").addClass("fluid-hover-zoom").removeClass("fluid-hover-zoom-off");
    }
    else {
        $(".fluid-hover-zoom").addClass("fluid-hover-zoom-off").removeClass("fluid-hover-zoom");
    }
}

jQuery.expr[':'].icontains = function(a, i, m) {
    return jQuery(a).text().toUpperCase()
        .indexOf(m[3].toUpperCase()) >= 0;
};

const findByKey = (array, key, value) => {
    return array.find(item => item[key] === value);
}

function isEmpty(obj) {
    return Object.keys(obj).length === 0;
}  
  