import{inject as W,ref as c,computed as j,onMounted as V,watch as q,resolveComponent as r,createBlock as B,unref as b,openBlock as S,withCtx as i,createVNode as t,createTextVNode as u,toDisplayString as G}from"/vue.js";import{c as l}from"./nc-main.DEgIQeOo.js";import"./service-worker.DGZzV4eE.js";const U=Object.assign({name:"NcLogin"},{__name:"nc-login",setup(H){const d=W("state"),o=d.rec,g=d.hdr,h=c(!1);o.logged_in=l.loggedIn();const m=c(l.loggedInInfo());let p=!1;const v=c(!1),y=c(!1),f=c(g.login.biometric_button),k=c([]);c(!1);const $=c(""),z=j(()=>h.value===!0||d.rec&&d.rec.login_id&&d.rec.password&&d.rec.organization_id);async function A(){if(p=l.isWebauthnSupported(),p)try{if(v.value=await l.supportBiometrics(),v.value){const e=navigator.platform.toLowerCase();e.includes("iphone")||e.includes("ipad")?f.value=g.login.biometric_button_ios:e.includes("mac")?f.value=g.login.biometric_button_mac:e.includes("win")?f.value=g.login.biometric_button_windows:f.value=g.login.biometric_button}}catch(e){console.warn("Failed to check biometric support:",e)}}function D(e){e.getModifierState&&e.getModifierState("CapsLock")}function I(){let e=document.getElementById("login_pw");e&&e.addEventListener("keydown",D),e=document.getElementById("login_id"),e?e.addEventListener("animationstart",()=>{h.value=!0}):setTimeout(()=>{I()},20)}V(()=>{I(),A(),setTimeout(()=>{L()},100)});function N(){const e=navigator.userAgent,n=navigator.platform.toLowerCase();let a="Device";n.includes("iphone")||e.includes("iPhone")?a="iPhone":n.includes("ipad")||e.includes("iPad")?a="iPad":n.includes("mac")?a="Mac":n.includes("win")?a="Windows PC":n.includes("linux")?a="Linux PC":e.includes("Android")&&(a="Android");let s="";e.includes("Chrome")&&!e.includes("Edg")?s="Chrome":e.includes("Firefox")?s="Firefox":e.includes("Safari")&&!e.includes("Chrome")?s="Safari":e.includes("Edg")&&(s="Edge");const _=s?`${a} (${s})`:a;$.value=_,(!o.device_name||o.device_name.trim()==="")&&(o.device_name=_)}q(()=>o.login_id,()=>{F(),N()});let C=!1;function L(){if(C)return;C=!0;const e=document.getElementById("login_id");e&&e.focus()}function T(){l.logout(()=>{o.logged_in=l.loggedIn(),o.is_designer=l.isDesigner(),m.value=l.loggedInInfo()}),o.login_id="",o.password=""}function F(){if(o.login_id&&p){const e={organization_id:o.organization_id,login_id:o.login_id,password:o.password};l.getPasskeyList(e,n=>{!n.error&&n.data?(y.value=n.data.length>0,k.value=n.data):(y.value=!1,k.value=[])})}}function M(){if(!z.value){console.debug(`🚀 *** login cancelled ~ login_id: ${d.rec.login_id}, password empty: ${d.rec.password!=""}, organization_id: ${d.rec.organization_id}`);return}const e={organization_id:o.organization_id,login_id:o.login_id,password:o.password};l.login(e,n=>{o.logged_in=l.loggedIn(),o.logged_in&&(o.password=""),o.is_designer=l.isDesigner(),console.debug(`login() callback, logged in: ${o.logged_in}`),!n.error&&n.status==="ok"&&(o.login_id=""),m.value=l.loggedInInfo()})}return(e,n)=>{const a=r("nc-header"),s=r("nc-row"),_=r("nc-translate"),E=r("nc-button"),P=r("nc-form"),K=r("nc-label-dropdown"),w=r("nc-form-field"),x=r("nc-label-input"),O=r("nc-col");return b(o).logged_in?(S(),B(P,{key:0},{default:i(()=>[t(s,{class:"nc-center-h nc-margin-top"},{default:i(()=>[t(a,{size:"medium",model:"",style:{color:"grey"}},{default:i(()=>[u(G(b(m)),1)]),_:1})]),_:1}),t(s,{class:"nc-center-h nc-margin-top"},{default:i(()=>[t(E,{onClick:T},{default:i(()=>[t(_,{model:"hdr.login.logout"},{default:i(()=>[...n[0]||(n[0]=[u("Logout",-1)])]),_:1})]),_:1})]),_:1})]),_:1})):(S(),B(P,{key:1,id:"nc-login",onKeyup:L},{default:i(()=>[t(w,null,{default:i(()=>[t(K,{horizontal:!1,hdr:"hdr.login.organization",model:"rec.organization_id",placeholder:"Organisaatio"},{default:i(()=>[...n[1]||(n[1]=[u("Organization",-1)])]),_:1})]),_:1}),t(w,null,{default:i(()=>[t(x,{class:"nc-autofill",id:"login_id",horizontal:!1,name:"login_id",hdr:"hdr.login.login_id",model:"rec.login_id",autocomplete:"username"},{default:i(()=>[...n[2]||(n[2]=[u("Login id",-1)])]),_:1})]),_:1}),t(w,null,{default:i(()=>[t(x,{class:"nc-autofill",id:"login_pw",horizontal:!1,name:"login_pw",hdr:"hdr.login.password",model:"rec.password",type:"password",autocomplete:"current-password"},{default:i(()=>[...n[3]||(n[3]=[u("Password",-1)])]),_:1})]),_:1}),t(s,{class:"nc-end-h"},{default:i(()=>[t(O,{class:"nc-end-h"},{default:i(()=>[t(E,{class:"primary nc-button-wide",id:"loginButton",disabled:!b(z),type:"submit",onClick:M},{default:i(()=>[t(_,{model:"hdr.login.login"},{default:i(()=>[...n[4]||(n[4]=[u("Login",-1)])]),_:1})]),_:1},8,["disabled"])]),_:1})]),_:1})]),_:1}))}}});export{U as default};
