(function () {
    'use strict';

    var body = document.body;
    var drawer = document.getElementById('sideDrawer');
    var backdrop = document.getElementById('drawerBackdrop');
    var openButton = document.getElementById('openDrawer');
    var closeButton = document.getElementById('closeDrawer');

    function openDrawer() {
        if (!drawer || !backdrop || !openButton) {
            return;
        }

        drawer.classList.add('is-open');
        drawer.setAttribute('aria-hidden', 'false');
        backdrop.hidden = false;
        openButton.setAttribute('aria-expanded', 'true');
        body.classList.add('drawer-open');
    }

    function closeDrawer() {
        if (!drawer || !backdrop || !openButton) {
            return;
        }

        drawer.classList.remove('is-open');
        drawer.setAttribute('aria-hidden', 'true');
        backdrop.hidden = true;
        openButton.setAttribute('aria-expanded', 'false');
        body.classList.remove('drawer-open');
    }

    if (openButton) {
        openButton.addEventListener('click', openDrawer);
    }

    if (closeButton) {
        closeButton.addEventListener('click', closeDrawer);
    }

    if (backdrop) {
        backdrop.addEventListener('click', closeDrawer);
    }

    document.addEventListener('keydown', function (event) {
        if (event.key === 'Escape') {
            closeDrawer();
        }
    });

    var jackpot = document.querySelector('[data-jackpot]');

    if (jackpot) {
        var rawValue = String(
            jackpot.getAttribute('data-jackpot') || '0'
        ).replace(/,/g, '');

        var currentValue = Number(rawValue);

        if (Number.isFinite(currentValue)) {
            window.setInterval(function () {
                currentValue += Math.floor(Math.random() * 900) + 100;

                jackpot.textContent = 'IDR ' + currentValue.toLocaleString(
                    'en-US',
                    {
                        minimumFractionDigits: 2,
                        maximumFractionDigits: 2
                    }
                );
            }, 4500);
        }
    }
}());
