(function () {
    'use strict';

    var root = document.querySelector('[data-diar-r25-reports]');
    if (!root) return;

    var tabButtons = Array.prototype.slice.call(root.querySelectorAll('[data-r25-tab]'));
    var panels = Array.prototype.slice.call(root.querySelectorAll('[data-r25-panel]'));
    var detailButtons = Array.prototype.slice.call(root.querySelectorAll('[data-r25-detail]'));
    var detailPanels = Array.prototype.slice.call(root.querySelectorAll('[data-r25-detail-panel]'));
    var filterForm = root.querySelector('[data-report-filter]');
    var exportLinks = Array.prototype.slice.call(root.querySelectorAll('[data-fast-export]'));

    function show(element, visible) {
        element.hidden = !visible;
        element.classList.toggle('is-active', visible);
        element.setAttribute('aria-hidden', visible ? 'false' : 'true');
    }

    function activateTab(name, updateHash) {
        var found = false;
        tabButtons.forEach(function (button) {
            var active = button.getAttribute('data-r25-tab') === name;
            button.classList.toggle('is-active', active);
            button.setAttribute('aria-selected', active ? 'true' : 'false');
            if (active) found = true;
        });
        if (!found) name = 'overview';
        panels.forEach(function (panel) {
            show(panel, panel.getAttribute('data-r25-panel') === name);
        });
        if (updateHash && window.history && window.history.replaceState) {
            window.history.replaceState(null, '', '#report-' + name);
        }
    }

    function activateDetail(name) {
        var found = false;
        detailButtons.forEach(function (button) {
            var active = button.getAttribute('data-r25-detail') === name;
            button.classList.toggle('is-active', active);
            button.setAttribute('aria-selected', active ? 'true' : 'false');
            if (active) found = true;
        });
        if (!found) name = 'daily';
        detailPanels.forEach(function (panel) {
            show(panel, panel.getAttribute('data-r25-detail-panel') === name);
        });
    }

    function tabFromHash() {
        var value = (window.location.hash || '').replace(/^#report-/, '');
        return value || 'overview';
    }

    tabButtons.forEach(function (button) {
        button.setAttribute('role', 'tab');
        button.addEventListener('click', function () {
            activateTab(button.getAttribute('data-r25-tab') || 'overview', true);
        });
    });

    detailButtons.forEach(function (button) {
        button.setAttribute('role', 'tab');
        button.addEventListener('click', function () {
            activateDetail(button.getAttribute('data-r25-detail') || 'daily');
        });
    });

    exportLinks.forEach(function (link) {
        link.addEventListener('click', function () {
            if (link.classList.contains('is-loading')) return;
            link.classList.add('is-loading');
            link.setAttribute('aria-busy', 'true');
            window.setTimeout(function () {
                link.classList.remove('is-loading');
                link.removeAttribute('aria-busy');
            }, 4000);
        });
    });

    if (filterForm) {
        filterForm.addEventListener('submit', function () {
            var button = filterForm.querySelector('button[type="submit"]');
            if (button) {
                button.disabled = true;
                button.setAttribute('aria-busy', 'true');
            }
        });
    }

    window.addEventListener('hashchange', function () {
        activateTab(tabFromHash(), false);
    });
    window.addEventListener('pageshow', function () {
        exportLinks.forEach(function (link) {
            link.classList.remove('is-loading');
            link.removeAttribute('aria-busy');
        });
    });

    activateDetail('daily');
    activateTab(tabFromHash(), false);
}());
