/*
 * Copyright (C) 2023 Leonid Nikitin (kor-elf)
 * https://git.kor-elf.net/kor-elf/service-captcha-gui/src/branch/main/LICENSE.md
 */
!function(e){let t=e.querySelectorAll("div.captcha-service-kor-elf");function n(e){e.style.display="none"}function c(t,n,c,o,i){let r=t.querySelector("div.window-captcha__content__body");if(r.querySelectorAll(".loading").length>0)return null;r.innerHTML='<div class="loading"></div>',fetch(n+"/api/v1/captcha",{headers:{"Content-Type":"application/json",Accept:"application/json","public-token":c}}).then(e=>e.json()).then(function(t){let l=t.image_body.width,d=t.image_body.height;r.innerHTML=`<form method="post"><div class="window-captcha__content__body__head"><img src="${t.image_head.base64}" width="100%"></div><p>Выберите значение в том порядке, на котором на картинке выше:</p><div class="window-captcha__content__body__coordinator"><img src="${t.image_body.base64}" width="100%" /></div><input type="hidden" name="captcha_key" class="captcha_key" value="${t.captcha_key}" /><button type="button" class="window-captcha__content__body__button">Я не робот!</button></form>`;let s=r.querySelector("div.window-captcha__content__body__coordinator"),p=s.querySelector("img");p.addEventListener("click",function(t){let n=e.createElement("div");n.style.left=t.offsetX/p.width*100+"%",n.style.top=t.offsetY/p.height*100+"%",n.classList.add("pointer");let c=t.offsetX*(l/p.width),a=t.offsetY*(d/p.height),o=s.querySelectorAll(".pointer").length+1;n.innerHTML=`<span class="pounter__number">${o}</span><input type="hidden" class="x" name="pointer[][x]" value="${Math.round(c)}" /><input type="hidden" class="y" name="pointer[][y]" value="${Math.round(a)}" />`,n.addEventListener("click",function(){n.remove(),s.querySelectorAll(".pointer").forEach(function(e,t){e.querySelector("span.pounter__number").textContent=t+1})}),s.appendChild(n)}),r.querySelector(".window-captcha__content__body__button").addEventListener("click",function(){(function t(n,c,o,i,r){let l=n.querySelector("button.window-captcha__content__body__button");if(l.querySelectorAll(".loading").length>0)return null;let d=e.createElement("span");d.classList.add("loading"),l.appendChild(d);let s={captcha_key:n.querySelector(".captcha_key").value,verification:[]};n.querySelectorAll(".pointer").forEach(function(e){s.verification.push({x:e.querySelector(".x").value,y:e.querySelector(".y").value})}),fetch(c+"/api/v1/captcha",{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json","public-token":o},body:JSON.stringify(s)}).then(e=>{if(429===e.status){a("Вы превысили количество попыток. Обновите каптчу.",l);return}return e.json()}).then(function(t){if(void 0!==t.errors){let n="";for(let c in t.errors)n+=t.errors[c]+"<br>";""===n&&(n=t.message),a(n,l);return}if(void 0===t.captcha_key){a("Произошла ошибка!",l);return}i.querySelector("button.button-open-window-captcha").remove();let o=e.createElement("div");o.innerHTML=`<span class="captcha-verified">Ура!!! Проверку прошли!</span>`,i.appendChild(o),i.querySelector(".window-captcha").remove();let d=e.createElement("input");d.name=r.getAttribute("data-captcha-verified-name"),d.value=t.captcha_key,d.type="hidden",r.appendChild(d)}).catch(e=>{a("Произошла ошибка!",l)}).finally(function(){d.remove()})})(r,n,c,o,i)})}).catch(e=>{r.innerHTML=`<div class="window-captcha__content__body__error">Произошла ошибка, сервис с каптчей не ответил. Попробуйте ещё раз!</div>`})}function a(t,n){let c=e.createElement("div");c.classList.add("error-message"),c.innerHTML=t,n.before(c),setTimeout(function(){c.remove()},3e3)}t.forEach(function(t){let a=t.attachShadow({mode:"closed"}),o=e.createElement("button"),i=e.createElement("link"),r=t.getAttribute("data-domain"),l=t.getAttribute("data-token"),d=t.getAttribute("data-static-path"),s=null;i.rel="stylesheet",i.type="text/css",i.href=d+"/style.css",a.appendChild(i),o.textContent="Я не робот!",o.type="button",o.classList.add("button-open-window-captcha"),a.appendChild(o),o.addEventListener("click",function(){null===s&&(s=function t(a,o,i,r){let l=e.createElement("div");return l.classList.add("window-captcha"),l.innerHTML=`<div class="window-captcha__content"><div class="window-captcha__content__header">Я не робот! <button type="button" class="window-captcha__reload"></button><button type="button" class="window-captcha__close">X</button></div><div class="window-captcha__content__body"></div></div>`,a.appendChild(l),l.addEventListener("click",function(e){if(e.target!==l)return null;n(l)}),l.querySelector("button.window-captcha__close").addEventListener("click",function(){n(l)}),l.querySelector("button.window-captcha__reload").addEventListener("click",function(){c(l,o,i,a,r)}),l}(a,r,l,t)),s.style.display="block",c(s,r,l,a,t)})})}(document);
