/**
 * @package     Joomla.Plugin
 * @subpackage  System.webauthn
 *
 * @copyright   (C) 2020 Open Source Matters, Inc. <https://www.joomla.org>
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */window.Joomla=window.Joomla||{},((s,c)=>{const u=(n,r="")=>{let t="";return Object.keys(n).forEach(e=>{if(typeof n[e]!="object"){t.length>0&&(t+="&"),r===""?t+=`${encodeURIComponent(e)}=${encodeURIComponent(n[e])}`:t+=`${encodeURIComponent(r)}[${encodeURIComponent(e)}]=${encodeURIComponent(n[e])}`;return}t+=`${u(n[e],e)}`}),t},g=(n,r)=>{const t=n.querySelectorAll(r);return t.length?t[0]:null},d=(n,r)=>{let t=null;if(!n)return t;const e=n.parentElement;if(e.nodeName==="FORM")return t=g(e,r),t;const o=e.querySelectorAll("form");if(o.length){for(let a=0;a<o.length;a+=1)if(t=g(o[a],r),t!==null)return t}return null},l=n=>{s.renderMessages({error:[n]})},m=n=>{const r=e=>btoa(String.fromCharCode(...e)),t=e=>{let o=e.replace(/-/g,"+").replace(/_/g,"/");const a=o.length%4;if(a){if(a===1)throw new Error("InvalidLengthError: Input base64url string is the wrong length to determine padding");o+=new Array(5-a).join("=")}return o};if(!n.challenge){l(s.Text._("PLG_SYSTEM_WEBAUTHN_ERR_INVALID_USERNAME"));return}n.challenge=Uint8Array.from(window.atob(t(n.challenge)),e=>e.charCodeAt(0)),n.allowCredentials&&(n.allowCredentials=n.allowCredentials.map(e=>(e.id=Uint8Array.from(window.atob(t(e.id)),o=>o.charCodeAt(0)),e))),navigator.credentials.get({publicKey:n}).then(e=>{const o={id:e.id,type:e.type,rawId:r(new Uint8Array(e.rawId)),response:{authenticatorData:r(new Uint8Array(e.response.authenticatorData)),clientDataJSON:r(new Uint8Array(e.response.clientDataJSON)),signature:r(new Uint8Array(e.response.signature)),userHandle:e.response.userHandle?r(new Uint8Array(e.response.userHandle)):null}},a=s.getOptions("system.paths");window.location=`${a?`${a.base}/index.php`:window.location.pathname}?${s.getOptions("csrf.token")}=1&option=com_ajax&group=system&plugin=webauthn&format=raw&akaction=login&encoding=redirect&data=${btoa(JSON.stringify(o))}`}).catch(e=>{l(e)})};s.plgSystemWebauthnLogin=n=>{const r=c.getElementById(n),t=d(r,"input[name=username]"),e=d(r,"input[name=return]");if(t===null)return s.renderMessages({error:[s.Text._("PLG_SYSTEM_WEBAUTHN_ERR_CANNOT_FIND_USERNAME")]}),!1;const o=t.value,a=e?e.value:null;if(o==="")return s.renderMessages({error:[s.Text._("PLG_SYSTEM_WEBAUTHN_ERR_EMPTY_USERNAME")]}),!1;const p={option:"com_ajax",group:"system",plugin:"webauthn",format:"raw",akaction:"challenge",encoding:"raw",username:o,returnUrl:a};p[s.getOptions("csrf.token")]=1;const w=s.getOptions("system.paths");return s.request({url:`${w?`${w.base}/index.php`:window.location.pathname}?${s.getOptions("csrf.token")}=1`,method:"POST",data:u(p),onSuccess(i){let f={};try{f=JSON.parse(i)}catch{}m(f)},onError:i=>{l(`${i.status} ${i.statusText}`)}}),!1};const h=[].slice.call(c.querySelectorAll(".plg_system_webauthn_login_button"));h.length&&h.forEach(n=>{n.addEventListener("click",({currentTarget:r})=>{s.plgSystemWebauthnLogin(r.getAttribute("data-webauthn-form"))})})})(Joomla,document);
