/*
	Author: Jeremy Espinosa
	Filename: captcha.js
	Description: Integrating google recaptcha, can also show one or more captcha in a page
	Dependencies:
		- google captcha API
	How to use:
*/

;(function($) {
    'use strict'

    B2C.captcha = {
        init: function(option) {
            // set default
            this._default = {
                target: '.js-captcha',
                targetCaptcha: ['js-captcha-item'],
                config: {
                    sitekey: '6LfCurIUAAAAAHFPeyuOMjE55VJZcmqBxdJt5LbD'
                }
            }

            // combine the default and option pass
            this._option = $.extend(true, this._default, option)

            if (!$(this._option.target).length) return
            // render the capthca
            this.onloadCallback()
        },

        onloadCallback: function() {
            const self = this

            window.onloadCallback = function() {
                // to have multiple recaptcha in a page
                for (let i = 0; i < self._option.targetCaptcha.length; i++) {
                    grecaptcha.render(
                        document.getElementById(self._option.targetCaptcha[i]),
                        {
                            sitekey: self._option.config.sitekey
                        }
                    )
                }
            }
        }
    }

    const defaultCaptcha = Object.create(B2C.captcha)

    defaultCaptcha.init()
})(jQuery)
