(() => {
    // TenantPilot shim: ensure Livewire interceptMessage callbacks run in a safe order.
    // This prevents Filament's notifications asset (and others) from calling an undefined
    // animation callback when onSuccess fires before onFinish.
    if (typeof window === 'undefined') {
        return;
    }

    const applyShim = () => {
        const Livewire = window.Livewire;

        if (!Livewire || typeof Livewire.interceptMessage !== 'function') {
            return false;
        }

        if (Livewire.__tenantpilotInterceptMessageShimApplied) {
            return true;
        }

        const original = Livewire.interceptMessage.bind(Livewire);

        Livewire.interceptMessage = (handler) => {
            if (typeof handler !== 'function') {
                return original(handler);
            }

            return original((context) => {
                if (!context || typeof context !== 'object') {
                    return handler(context);
                }

                const originalOnFinish = context.onFinish;
                const originalOnSuccess = context.onSuccess;

                if (typeof originalOnFinish !== 'function' || typeof originalOnSuccess !== 'function') {
                    return handler(context);
                }

                const finishCallbacks = [];

                const onFinish = (callback) => {
                    if (typeof callback === 'function') {
                        finishCallbacks.push(callback);
                    }

                    return originalOnFinish(callback);
                };

                const onSuccess = (callback) => {
                    return originalOnSuccess((...args) => {
                        // Ensure any registered finish callbacks are run before success callbacks.
                        // We don't swallow errors; we just stabilize ordering.
                        for (const finishCallback of finishCallbacks) {
                            finishCallback(...args);
                        }

                        if (typeof callback === 'function') {
                            return callback(...args);
                        }
                    });
                };

                return handler({
                    ...context,
                    onFinish,
                    onSuccess,
                });
            });
        };

        Livewire.__tenantpilotInterceptMessageShimApplied = true;

        return true;
    };

    if (applyShim()) {
        return;
    }

    // Livewire may not be initialized yet when this script runs (depending on
    // script tag order). Try again on `livewire:init` and with a short fallback poll.
    const onInit = () => {
        applyShim();
    };

    window.addEventListener('livewire:init', onInit, { once: true });
    document.addEventListener('livewire:init', onInit, { once: true });

    let tries = 0;
    const maxTries = 50;
    const timer = setInterval(() => {
        tries += 1;

        if (applyShim() || tries >= maxTries) {
            clearInterval(timer);
        }
    }, 100);
})();
