export default function search() {
    const searchButton = '.js-header-search-button, .js-search-collapse, .js-search-input';
    const openClass = 'open-search';
    const searchInput = document.querySelector('.js-search-input');

    const searchHeaderContainer = document.querySelector('.js-header-search');
    const searchTabbable = searchHeaderContainer ? searchHeaderContainer.querySelectorAll('a[href], button:not([disabled]), .js-search-input') : null;

    window.addEventListener('click', function (event) {
        if (event.target.matches(searchButton)) {
            document.body.classList.add(openClass);
            searchInput.focus();

            setTabIndex('0');
        } else {
            document.body.classList.remove(openClass);

            setTabIndex('-1');
        }
    });

    function setTabIndex(tabindex) {
        if (searchTabbable && searchTabbable.length > 0) {
            searchTabbable.forEach(function (item) {
                item.setAttribute('tabindex', tabindex);
            });
        }
    }
}
