import{c as P,c5 as B,c1 as V,cb as Y,cZ as f,v as d,cd as q,y as J,o as Z,b as p,n as G,J as Q,i as T,k as I,a as R,t as ee,dn as te,au as ne,a7 as ae,dp as D}from"./index-6Jqoo2rk.js";import{C as oe,w as re}from"./emailSetup-CFcJlzt-.js";import{T as ie}from"./tracking-CIK7NCFD.js";import{S as N}from"./simpleConfirmation-5BGWRx60.js";import{M as C}from"./mediaHeader-B5k-4_FU.js";import{t as O}from"./toast-B3QB1-N7.js";import{a as F,f as K,b as M}from"./anchorCallback-DsA1XczG.js";import{f as se}from"./focusWhenConnected-14rkj1jb.js";import{A as le}from"./AuthCard-Cghujz7c.js";import{u as ce,s}from"./bootstrapIm-BS8HLnlI.js";import"./iconTsx-Cr8T7ZIv.js";import"./transition-DuxZkCEv.js";import"./animations-O3syBkMX.js";const H=ee("<div>");function ke(x){const{managers:u,navigate:b,toIm:z}=ce();let t=x.spec.payload;const[W,U]=P(),[A,m]=P(),h=document.createElement("div"),g=B.isMobile?100:130;let l,_,r;const c=document.createElement("div");c.classList.add(s.errorLabel);const y=document.createElement("h4");y.classList.add(s.phone);const E=document.createElement("span");E.classList.add(s.phoneEdit),E.append(V("edit")),Y(E,()=>b({name:"signIn"}));const X=t.type.length,n=new oe({length:X,onChange:()=>{n.error=!1,f(c,"")},onFill:e=>$(e),class:s.codeInputField});function $(e){n.disabled=!0;const a={phone_number:t.phone_number,phone_code_hash:t.phone_code_hash,phone_code:e};u.apiManager.invokeApi("auth.signIn",a,{ignoreErrors:!0}).then(async o=>{switch(o._){case"auth.authorization":await u.apiManager.setUser(o.user),z();break;case"auth.authorizationSignUpRequired":b({name:"signUp",payload:{phone_number:t.phone_number,phone_code_hash:t.phone_code_hash}});break}}).catch(o=>{let i=!1;switch(o.type){case"SESSION_PASSWORD_NEEDED":i=!0,b({name:"password"}),setTimeout(()=>{n.value=""},300);break;case"PHONE_CODE_EXPIRED":n.error=!0,f(c,d("PHONE_CODE_EXPIRED"));break;case"PHONE_CODE_EMPTY":case"PHONE_CODE_INVALID":n.error=!0,f(c,d("PHONE_CODE_INVALID"));break;default:n.error=!0,f(c,o.type);break}n.disabled=!1,i||(n.value="",q(()=>n.input.focus()))})}function j(){if(l?.remove(),l=void 0,_?.remove(),_=void 0,h.replaceChildren(),t.type._==="auth.sentCodeTypeFragmentSms"){const e=document.createElement("div");return e.classList.add("media-sticker-wrapper"),h.append(e),D.loadAnimationAsAsset({container:e,loop:!0,autoplay:!0,width:g,height:g},"jolly_roger").then(a=>(_=a,D.waitForFirstFrame(a))).then(()=>{})}return l=new ie(n,g),h.append(l.container),l.load()}function S(){u.apiManager.invokeApi("auth.resetLoginEmail",{phone_number:t.phone_number,phone_code_hash:t.phone_code_hash}).then(e=>{e._==="auth.sentCode"?(t=Object.assign(e,{phone_number:t.phone_number}),t.type._==="auth.sentCodeTypeEmailCode"?k(t.type):v()):(console.error(e),O({langPackKey:"Error.AnError"}))}).catch(e=>{e.type.includes("TASK_ALREADY_EXISTS")?N.show({titleLangKey:"Login.ResetEmail.NeedPremium",descriptionLangKey:"Login.ResetEmail.NeedPremiumText",button:{langKey:"OK"}}):(console.error(e),O({langPackKey:"Error.AnError"}))})}function k(e){if(r&&(clearTimeout(r),r=void 0),e.reset_pending_date!=null){const a=e.reset_pending_date-ne(!0);if(a<=0||e.reset_pending_date<=0){m(d("Login.ResetEmail.PleaseWait")),S();return}m(d("Login.ResetEmail.Pending",[F(K(a,2)),M(S)])),r=ae.setTimeout(()=>k(e),3e4);return}e.reset_available_period!=null&&m(d("TroubleEmail",[M(()=>{N.show({titleLangKey:"Login.ResetEmail.Title",descriptionLangKey:"Login.ResetEmail.Text",descriptionArgs:[F(K(e.reset_available_period,2))],button:{langKey:"Login.ResetEmail.Title"}}).then(()=>S())})]))}function v(){const e=t.type.length;n.options.length=e,n.value="",y.innerText=t.phone_number??"",r&&(clearTimeout(r),r=void 0),m(void 0);let a,o;const i=t.type;switch(i._){case"auth.sentCodeTypeSms":a="Login.Code.SentSms";break;case"auth.sentCodeTypeApp":a="Login.Code.SentInApp";break;case"auth.sentCodeTypeCall":a="Login.Code.SentCall";break;case"auth.sentCodeTypeFragmentSms":{a="PhoneNumber.Code.Fragment.Info";const L=document.createElement("a");te(L),L.href=i.url,o=[L];break}case"auth.sentCodeTypeEmailCode":a="Login.Code.SentEmail",o=[re(i.email_pattern)],k(i);break;default:a="Login.Code.SentUnknown",o=[i._];break}U(d(a,o)),u.appStateManager.pushToState("authState",{_:"authStateAuthCode",sentCode:t}),j().catch(()=>{})}let w;return J(()=>{v(),w=se(n.input)}),Z(()=>{w?.(),r&&clearTimeout(r),l?.remove(),_?.remove(),n.cleanup()}),p(le,{get class(){return s.pageAuthCode},get header(){return p(C,{get children(){return[p(C.Sticker,{element:h,size:g}),p(C.Title,{get children(){const e=H();return T(e,y,null),T(e,E,null),I(()=>R(e,s.phoneWrapper)),e}}),p(C.Subtitle,{class:"secondary",get children(){return W()}})]}})},inputWrapper:!1,get children(){return[G(()=>n.container),c,p(Q,{get when(){return A()},get children(){const e=H();return T(e,A),I(()=>R(e,s.forgotLink)),e}})]}})}export{ke as default};
//# sourceMappingURL=AuthCodeCard-Ea06CwdI.js.map
