use crate::components::html::{script, Script};
use crate::{rumtk_web_get_config, ComponentResult, RUMWebTemplate, RUMWebTemplateSafe, SharedAppState, TextMap, DEFAULT_SCRIPT_IMPORT, DEFAULT_TEXT};
use rumtk_core::strings::RUMString;
use std::sync::LazyLock;
static DEFAULT_GCAPTCHA_JS_URL: LazyLock<RUMString> = LazyLock::new(|| "https://www.google.com/recaptcha/enterprise.js".to_string());
#[derive(RUMWebTemplate, Debug, Clone)]
#[template(
source = "
{{import|safe}}
<div
class='g-recaptcha'
data-sitekey='{{site_key}}'
data-action='LOGIN'
data-theme='{{theme}}'
data-callback='enableSubmitButton'
data-expired-callback='disableSubmitButton'
>
</div>
<br/>
<script>
function enableSubmitButton() {
document.getElementById('{{widget_id}}').disabled = false;
}
function disableSubmitButton() {
document.getElementById('{{widget_id}}').disabled = true;
}
</script>
",
ext = "html"
)]
pub struct GoogleCaptcha {
theme: RUMString,
widget_id: RUMString,
import: Script,
site_key: RUMString,
}
impl RUMWebTemplateSafe for GoogleCaptcha {}
pub fn gcaptcha<'a>(widget_id: &str, captcha_config: &TextMap, state: SharedAppState) -> ComponentResult<GoogleCaptcha> {
let theme = rumtk_web_get_config!(state).theme.clone();
let site_key = match captcha_config.get("site-key") {
Some(key_site) => key_site.clone(),
None => DEFAULT_TEXT.to_string()
};
let import_url = match captcha_config.get("import-url") {
Some(import_url) => import_url.clone(),
None => (*DEFAULT_GCAPTCHA_JS_URL).clone(),
};
let import = script(DEFAULT_SCRIPT_IMPORT, &import_url, false)?;
Ok(GoogleCaptcha {
theme,
widget_id: widget_id.to_string(),
import,
site_key,
})
}