// @license magnet:?xt=urn:btih:90dc5c0be029de84e523b9b3922520e79e0e6f08&dn=cc0.txt CC0-1.0
/****************************************************************************
 * Planar Vagabond's Guide to the Multiverse (planarvagabond.com)
 *
 * Copyright 2023-2024 Eric Woodward
 * Source released under CC0 Public Domain License v1.0
 * https://www.planarvagabond.com/licenses/cc0/
 * http://creativecommons.org/publicdomain/zero/1.0/
 ****************************************************************************/

export default (() => {
    // we load this library via "module" to guarantee baseline ES6 functionality

    // check for loaded libraries
    if (!window.Cookies) return;

    if (window.dayjs) dayjs.extend(window.dayjs_plugin_relativeTime);

    // Indicate JS is loaded
    document.documentElement.className =
        document.documentElement.className.replace('no-js', 'js');

    // Enable cached fonts ASAP
    /*
    if (!!Cookies.get('fonts_loaded')) {
        document.documentElement.className += ' js-hasFontsLoaded';
    }
    */

    document.addEventListener('DOMContentLoaded', () => {
        let to = null;
        to = setTimeout(() => {
            // Lazy-Load Media
            if (typeof loadMedia === 'function') {
                loadMedia('.js-lazyLoader', null, true);
            }

            // Add relative dates via dayjs
            if (window.dayjs) {
                const times = document.getElementsByTagName('time');

                [].forEach.call(times, (the_time) => {
                    let pub_time = the_time.getAttribute('datetime');
                    if (the_time.className.indexOf('js-noRelativeTime') === -1)
                        the_time.innerHTML +=
                            ' <span class="js-momentTime">(' +
                            dayjs(pub_time).from(dayjs()) +
                            ')</span>';
                    the_time.classList.add('isDone');
                });
            }

            // put url in DuckDuckGo search on 404 page
            if (document.documentElement.className.indexOf('is404') > -1) {
                document.getElementById('searchQuery').value =
                    window.location.pathname
                        .replace(/\\.html?$/, '')
                        .replace(/\//g, ' ');
            }

            // Add site to DuckDuckGo search
            document
                .getElementById('searchForm')
                .addEventListener('submit', (e) => {
                    document.getElementById('searchQuery').value +=
                        ' site:' + window.location.hostname;
                });

            // Make table captions into collapsable toggles
            document
                .querySelectorAll('.dividedTableWrapper > table > caption')
                .forEach((caption) => {
                    const theClass = 'js-hideContent',
                        wrapper = caption.closest('.dividedTableWrapper');

                    // removes inline-styled override from markdown-it-multimd-table
                    caption.removeAttribute('style');

                    if (!wrapper) return;

                    wrapper.classList.add('js-toggleable');

                    caption.addEventListener('click', (e) => {
                        wrapper.classList.toggle(theClass);
                    });
                });

            // Add shadow to toolDetails that have extra content
            // I know - ALL OF THIS CODE just for that little effect
            const details = document.querySelectorAll('.toolDetails'),
                setBottomShadow = (detail) => {
                    const {
                            clientHeight = 0,
                            offsetHeight = 0,
                            scrollHeight = 0,
                            scrollTop = 0,
                        } = detail ?? {},
                        currentScroll =
                            scrollTop / (scrollHeight - offsetHeight);

                    if (clientHeight === scrollHeight) return;

                    detail.style.boxShadow = `rgba(0, 0, 0, 0.35) 0px -${
                        50 * (1 - currentScroll)
                    }px 36px -28px inset`;
                };

            let isWaiting;
            const throttle = (callback, time) => {
                if (isWaiting) return;
                isWaiting = true;
                setTimeout(() => {
                    callback();
                    isWaiting = false;
                }, time);
            };

            window.addEventListener(
                'resize',
                () =>
                    throttle(
                        () =>
                            details.forEach((detail) =>
                                setBottomShadow(detail)
                            ),
                        250
                    ),
                { passive: true }
            );

            details.forEach((detail) => {
                detail.addEventListener(
                    'scroll',
                    () => throttle(() => setBottomShadow(detail), 250),
                    { passive: true }
                );
                setBottomShadow(detail);
            });

            details.forEach((detail) => setBottomShadow(detail));

            clearTimeout(to);
        }, 1);
    });
})();
// @license-end
