// static/admin/js/vendor-themes.js
/**
 * Admin vendor themes selection page
 */

// ✅ Use centralized logger
const vendorThemesLog = window.LogConfig.loggers.vendorTheme;

vendorThemesLog.info('Loading...');

function adminVendorThemes() {
    vendorThemesLog.debug('adminVendorThemes() called');

    return {
        // Inherit base layout state
        ...data(),

        // Set page identifier
        currentPage: 'vendor-theme',

        // State
        loading: false,
        error: '',
        vendors: [],
        selectedVendorCode: '',

        async init() {
            // Guard against multiple initialization
            if (window._adminVendorThemesInitialized) {
                return;
            }
            window._adminVendorThemesInitialized = true;

            // Call parent init first
            const parentInit = data().init;
            if (parentInit) {
                await parentInit.call(this);
            }

            await this.loadVendors();
        },

        async loadVendors() {
            this.loading = true;
            this.error = '';

            try {
                const response = await apiClient.get('/admin/vendors?limit=1000');
                this.vendors = response.vendors || [];
                vendorThemesLog.debug('Loaded vendors:', this.vendors.length);
            } catch (error) {
                vendorThemesLog.error('Failed to load vendors:', error);
                this.error = error.message || 'Failed to load vendors';
            } finally {
                this.loading = false;
            }
        },

        navigateToTheme() {
            if (!this.selectedVendorCode) {
                return;
            }
            window.location.href = `/admin/vendors/${this.selectedVendorCode}/theme`;
        }
    };
}

vendorThemesLog.info('Module loaded');
