document.addEventListener('alpine:init', () => {
    Alpine.data('HF_PRE_APP', () => ({
        nonce: hfngPREData.values.nonce,
        burnNonce: hfngPREData.values.burnNonce,

        blockRestGuests:hfngPREData.values.block_rest_guests,
        blockRestUsers:hfngPREData.values.block_rest_users,
        removeRestLinks:hfngPREData.values.remove_rest_links,       
        loginObfuscation:hfngPREData.values.login_obfuscation,
        disableOmbedDiscovery: hfngPREData.values.disable_oembed_discovery,
        removeXPingBack : hfngPREData.values.remove_x_pingback,
        blockAuthor:hfngPREData.values.block_author,
        blockXmlRpcPingBack:hfngPREData.values.block_xmlrpc_pingback,
        
        myResult: '',
        myColor: 'hfng-success',           
        featureEnabled: hfngPREData.values.feature_enabled,
        checkbox_on: hfngPREData.values.checkbox_on,
        checkbox_off: hfngPREData.values.checkbox_off,

        saveSettings() {
            fetch(hfngPREData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_PRE_save_settings',
                    block_rest_guests:this.blockRestGuests,
                    block_rest_users:this.blockRestUsers,
                    remove_rest_links:this.removeRestLinks,                   
                    login_obfuscation:this.loginObfuscation,                    
                    block_author:this.blockAuthor,
                    remove_x_pingback:this.removeXPingBack,
                    disable_oembed_discovery:this.disableOmbedDiscovery,                    
                    block_xmlrpc_pingback:this.blockXmlRpcPingBack,
        
                    security: this.nonce,
                    burn: this.burnNonce 
                })
            })
            .then(res => res.json())
           .then(res => {                
                    this.myResult = res.message;
                    this.myColor = (res.warning)? 'hfng-error':'hfng-success';
                    if (res.result === 'OK'){                                          
                        if(res.burn) {                        
                                this.burnNonce = res.burn; 
                        }
                    }  else {
                    this.myColor = 'hfng-error';
                }
                setTimeout(() => this.myResult = '', 2000);
            });
        },
    }))
});
