// trrc.js
import { TRRCApp } from './core/trrc-app.js';

window.ACC = window.ACC || {};
ACC.modules = ACC.modules || {};

ACC.modules.trrc = {

    /**
     * Mounts TRRCApp on a given DOM element.
     * @param {HTMLElement} el - The container DOM element.
     * @param {Object} opts - Extra options (apiUrl, tab, ...)
     */
    mount(el, opts = {}) {
        if (!el) {
            console.error('TRRC: mount failed → element is missing.');
            return null;
        }

        // Read API URL (priority: opts → data-api → fallback)
        const apiUrl =
            opts.apiUrl ||
            el.dataset.api ||
            '/wp-json/acc/v1/trrc/tree';

        // Extract tab (may come from TreeView → TabManager)
        const tab = opts.tab || null;

        if (!tab) {
            console.warn('TRRC: mount called WITHOUT tab. userContext will be undefined.');
        }

        // Instantiate TRRCApp
        let app;
        try {
            app = new TRRCApp({
                element: el,
                apiUrl: apiUrl,
                tab: tab,
            });
        } catch (err) {
            console.error('TRRC: Failed to create TRRCApp:', err);
            return null;
        }

        // Constructor already triggers loading internally
        return app;
    }
};
