/**
 * Check if a textarea has an RTE instance
 */
function hasRTE(textarea) {
    return textarea !== null && 'rte' in textarea && typeof textarea.rte === 'object';
}
/**
 * Execute the RTE ready callback with context
 * @param rte - The RTE instance that has loaded
 * @param context - Execution context containing callback, once flag, and state
 */
function executeCallback(rte, context) {
    if (context.once && context.hasExecuted) {
        return;
    }
    try {
        context.callback(rte);
        context.hasExecuted = true;
        if (context.once && context.subscription) {
            context.subscription.remove();
            context.subscription = null;
        }
    }
    catch (error) {
        console.error('onRTEReady callback failed', error);
    }
}
/**
 * Execute callback when a specific RTE is fully loaded
 * @param callback - Function called with RTE instance when ready
 * @param target - Textarea element or selector for the specific RTE
 * @param options - Optional configuration
 * @param options.once - Auto-cleanup after first fire (default: true)
 * @returns Cleanup function to unsubscribe
 */
export default function onRTEReady(callback, target, options) {
    const { once = true } = options || {};
    // Resolve target to textarea element
    const targetTextarea = typeof target === 'string' ? document.querySelector(target) : target;
    if (!(targetTextarea instanceof HTMLTextAreaElement)) {
        return () => { }; // No-op
    }
    // Context shared by all executeCallback invocations
    const context = {
        callback,
        once,
        hasExecuted: false,
        subscription: null,
    };
    // Already loaded?
    if (hasRTE(targetTextarea) && targetTextarea.rte.loaded) {
        executeCallback(targetTextarea.rte, context);
        if (once) {
            return () => { }; // Already executed, no cleanup needed
        }
    }
    const pubSub = window.PubSub;
    if (!pubSub || typeof pubSub.subscribe !== 'function') {
        // PubSub not available: fail gracefully
        return () => { };
    }
    context.subscription = pubSub.subscribe('rteLoaded', (rte) => {
        if (rte.ctx === targetTextarea) {
            executeCallback(rte, context);
        }
    });
    return () => context.subscription?.remove();
}
//# sourceMappingURL=onRTEReady.js.map