'use strict';

/**
 * Verify whether a value is a Promise by duck-testing for a 'then' function
 */
function isPromise(value) {
    if (!value)
        return false;
    return typeof value === 'object' && typeof value.then === 'function';
}

/**
 * Resolve the result of a function that may be async or not, as a Promise.
 */
function resolvePossiblyAsyncFunction(func, args = []) {
    const res = func(...args);
    if (isPromise(res)) {
        return res;
    }
    return Promise.resolve(res);
}

const GLOBAL_STATE_KEY = Symbol('LMVZ__ariaValidationRuntimeState');
const ARIA_VALIDATION_RUNTIME_CHANGED_EVENT = 'lmvz:aria-validation-runtime-changed';
function getRuntimeState() {
    const globalState = globalThis;
    if (!globalState[GLOBAL_STATE_KEY]) {
        globalState[GLOBAL_STATE_KEY] = {
            enabled: false,
        };
    }
    return globalState[GLOBAL_STATE_KEY];
}
function notifyRuntimeChange() {
    if (typeof window === 'undefined')
        return;
    window.dispatchEvent(new CustomEvent(ARIA_VALIDATION_RUNTIME_CHANGED_EVENT));
}
async function fetchRuntime() {
    const state = getRuntimeState();
    if (!state.enabled)
        return null;
    if (state.runtime)
        return state.runtime;
    if (!state.loadPromise) {
        state.loadPromise = Promise.resolve().then(function () { return require('./index--7IqZZqn.js'); }).catch(() => null);
    }
    const runtime = await state.loadPromise;
    if (runtime) {
        state.runtime = runtime;
    }
    return runtime;
}
async function initializeValidation(...args) {
    const runtime = await fetchRuntime();
    const s = getRuntimeState();
    if (!s.enabled)
        return;
    if (!runtime) {
        console.warn('Failed to load aria validation runtime');
        return;
    }
    runtime.initializeValidationStream(...args);
}
const isAriaValidationEnabled = () => getRuntimeState().enabled;
const useVerboseLogging = () => getRuntimeState().verboseLogging ?? false;
const enableAriaValidation = (options = {}) => {
    const state = getRuntimeState();
    if (state.enabled)
        return;
    Object.assign(state, options);
    state.enabled = true;
    initializeValidation().then(() => {
        notifyRuntimeChange();
    });
};
const disableAriaValidation = () => {
    const state = getRuntimeState();
    if (!state.enabled)
        return;
    state.enabled = false;
    notifyRuntimeChange();
};
function queueValidation(...args) {
    const { enabled, runtime, loadPromise, lazyCondition } = getRuntimeState();
    if (!enabled)
        return;
    if (!lazyCondition)
        return q();
    resolvePossiblyAsyncFunction(lazyCondition).then((shouldEnqueue) => {
        if (!shouldEnqueue)
            return;
        q();
    });
    function q() {
        if (runtime)
            return runtime.enqueueValidation(...args);
        if (!loadPromise) {
            console.warn('neither runtime nor load promise found for aria validation. This should not happen, as enabling aria validation should have triggered the loading of the runtime.');
            return;
        }
        loadPromise
            .then((loadedRuntime) => {
            if (!loadedRuntime?.enqueueValidation) {
                console.error('Failed to load aria validation runtime');
                return;
            }
            loadedRuntime.enqueueValidation(...args);
        })
            .catch(() => {
            console.error('Failed to enqueue validation due to an error while loading the runtime');
        });
    }
}

exports.ARIA_VALIDATION_RUNTIME_CHANGED_EVENT = ARIA_VALIDATION_RUNTIME_CHANGED_EVENT;
exports.disableAriaValidation = disableAriaValidation;
exports.enableAriaValidation = enableAriaValidation;
exports.isAriaValidationEnabled = isAriaValidationEnabled;
exports.queueValidation = queueValidation;
exports.useVerboseLogging = useVerboseLogging;
