// app/modules/loyalty/static/merchant/js/loyalty-merchant-settings.js
// noqa: js-006 - async init pattern is safe, loadData has try/catch

const merchantSettingsViewLog = window.LogConfig.loggers.merchantSettingsView || window.LogConfig.createLogger('merchantSettingsView');

function merchantLoyaltyMerchantSettings() {
    return {
        ...data(),
        currentPage: 'settings',

        settings: null,
        loading: false,
        error: null,

        async init() {
            merchantSettingsViewLog.info('=== MERCHANT LOYALTY SETTINGS VIEW PAGE INITIALIZING ===');
            if (window._merchantLoyaltyMerchantSettingsInitialized) return;
            window._merchantLoyaltyMerchantSettingsInitialized = true;

            this.loadMenuConfig();
            await this.loadSettings();
            merchantSettingsViewLog.info('=== MERCHANT LOYALTY SETTINGS VIEW PAGE INITIALIZATION COMPLETE ===');
        },

        async loadSettings() {
            this.loading = true;
            this.error = null;

            try {
                const response = await apiClient.get('/merchants/loyalty/settings');
                if (response) {
                    this.settings = response;
                    merchantSettingsViewLog.info('Settings loaded');
                }
            } catch (error) {
                if (error.status === 404) {
                    merchantSettingsViewLog.info('No settings found');
                    this.settings = null;
                } else {
                    merchantSettingsViewLog.error('Failed to load settings:', error);
                    this.error = error.message || 'Failed to load settings';
                }
            } finally {
                this.loading = false;
            }
        },
    };
}

if (!window.LogConfig.loggers.merchantSettingsView) {
    window.LogConfig.loggers.merchantSettingsView = window.LogConfig.createLogger('merchantSettingsView');
}
merchantSettingsViewLog.info('Merchant loyalty settings view module loaded');
