(function () {
    'use strict';

    function activateTab(root, target) {
        root.querySelectorAll('[data-cgy-tab-target]').forEach(function (tab) {
            var active = tab.getAttribute('data-cgy-tab-target') === target;
            tab.classList.toggle('is-active', active);
            tab.setAttribute('aria-selected', active ? 'true' : 'false');
        });

        root.querySelectorAll('[data-cgy-tab-panel]').forEach(function (panel) {
            var active = panel.getAttribute('data-cgy-tab-panel') === target;
            panel.classList.toggle('is-active', active);
            panel.hidden = !active;
        });
    }

    function initialize(root) {
        root.querySelectorAll('[data-cgy-tab-target]').forEach(function (tab) {
            tab.addEventListener('click', function () {
                activateTab(root, tab.getAttribute('data-cgy-tab-target'));
            });
        });

        activateTab(root, root.getAttribute('data-cgy-default-tab') || 'automation');
    }

    document.querySelectorAll('[data-cgy-tabs]').forEach(initialize);
}());
