(function () {
    'use strict';

    var backdrop = document.querySelector('[data-fate-mobile-bar-backdrop]');
    if (!backdrop) {
        return;
    }

    var profileBtn = document.querySelector('.fate-mobile-dock__profile');
    var searchBtn = document.querySelector('[data-fate-mobile-search-trigger]');
    var memberSheet = document.querySelector('.fate-mobile-bar__sheet--member');
    var searchSheet = document.querySelector('.fate-mobile-bar__sheet--search');
    var searchInput = document.getElementById('fate-mobile-search-q');

    var openSheet = null;
    var openTrigger = null;

    function getCloseButtons(sheet) {
        return sheet ? sheet.querySelectorAll('[data-fate-mobile-bar-close]') : [];
    }

    function setTriggerExpanded(trigger, expanded) {
        if (!trigger) {
            return;
        }
        trigger.setAttribute('aria-expanded', expanded ? 'true' : 'false');
    }

    function closeSheetNow(instant) {
        if (!openSheet) {
            return;
        }

        var sheetRef = openSheet;
        var triggerRef = openTrigger;

        openSheet = null;
        openTrigger = null;

        sheetRef.classList.remove('is-open');
        setTriggerExpanded(triggerRef, false);
        document.body.classList.remove('fate-mobile-bar-open');

        if (triggerRef === profileBtn && profileBtn) {
            profileBtn.classList.remove('is-active');
        }
        if (triggerRef === searchBtn && searchBtn) {
            searchBtn.classList.remove('is-active');
        }

        if (instant) {
            sheetRef.hidden = true;
            backdrop.hidden = true;
            return;
        }

        window.setTimeout(function () {
            if (!sheetRef.classList.contains('is-open')) {
                sheetRef.hidden = true;
                backdrop.hidden = true;
            }
        }, 220);
    }

    function openSheetNow(sheet, trigger) {
        if (!sheet) {
            return;
        }

        if (openSheet && openSheet !== sheet) {
            closeSheetNow(true);
        }

        sheet.hidden = false;
        backdrop.hidden = false;
        requestAnimationFrame(function () {
            sheet.classList.add('is-open');
        });

        openSheet = sheet;
        openTrigger = trigger || null;
        setTriggerExpanded(trigger, true);
        document.body.classList.add('fate-mobile-bar-open');

        if (trigger === profileBtn && profileBtn) {
            profileBtn.classList.add('is-active');
        }
        if (trigger === searchBtn && searchBtn) {
            searchBtn.classList.add('is-active');
        }

        if (sheet === searchSheet && searchInput) {
            window.setTimeout(function () {
                searchInput.focus();
                if (typeof searchInput.select === 'function' && searchInput.value) {
                    searchInput.select();
                }
            }, 180);
        }
    }

    function toggleSheet(sheet, trigger) {
        if (openSheet === sheet) {
            closeSheetNow();
            return;
        }
        openSheetNow(sheet, trigger);
    }

    if (profileBtn && memberSheet) {
        profileBtn.addEventListener('click', function () {
            toggleSheet(memberSheet, profileBtn);
        });

        getCloseButtons(memberSheet).forEach(function (btn) {
            btn.addEventListener('click', closeSheetNow);
        });
    }

    if (searchBtn && searchSheet) {
        searchBtn.addEventListener('click', function () {
            toggleSheet(searchSheet, searchBtn);
        });

        getCloseButtons(searchSheet).forEach(function (btn) {
            btn.addEventListener('click', closeSheetNow);
        });
    }

    backdrop.addEventListener('click', closeSheetNow);

    document.addEventListener('keydown', function (ev) {
        if (ev.key === 'Escape' && openSheet) {
            closeSheetNow();
        }
    });
})();
