/**
 * Safe replacement for theme plugins.js.
 * - Only loads libs when matching elements exist (NodeList.length check).
 * - Uses absolute theme asset URLs (no document.writeln).
 */
(function () {
    'use strict';

    var base = document.documentElement.getAttribute('data-theme-assets-base');
    if (!base) {
        return;
    }
    if (base.charAt(base.length - 1) !== '/') {
        base += '/';
    }

    var hasToast = document.querySelectorAll('[toast-list]').length > 0;
    var hasChoices = document.querySelectorAll('[data-choices]').length > 0;
    var hasFlatpickr = document.querySelectorAll('[data-provider]').length > 0;

    if (!hasToast && !hasChoices && !hasFlatpickr) {
        return;
    }

    function loadScript(src) {
        var script = document.createElement('script');
        script.src = src;
        script.async = false;
        document.head.appendChild(script);
    }

    if (hasToast) {
        loadScript('https://cdn.jsdelivr.net/npm/toastify-js');
    }
    if (hasChoices) {
        loadScript(base + 'libs/choices.js/public/assets/scripts/choices.min.js');
    }
    if (hasFlatpickr) {
        loadScript(base + 'libs/flatpickr/flatpickr.min.js');
    }
})();
