import{a as e,i as t,o as n,s as r,t as i}from"./graphql-Dsv3IYEA.js";import{n as a,t as o}from"./LoadingSpinner-B8ofpgbZ.js";import{t as s}from"./jsx-runtime-BKZ-v_4m.js";import{n as c}from"./not-found-i5RsCZif.js";import{t as l}from"./useTranslation-l30fIyqu.js";import{t as u}from"./useSearch-C-FkY2rF.js";import{n as d}from"./useNavigate-C0GZtrP6.js";import{t as f}from"./Button-BIiT28Ux.js";import{t as p}from"./error-solid-bRl8FwTZ.js";import{n as m,r as h,t as g}from"./password_changes-BSIe1fki.js";import{A as _,D as v,O as y,V as b}from"./main-CxGi0fIe.js";import{t as x}from"./ButtonLink-BcHfDxZ3.js";var S=s(),C=t(`
  mutation RecoverPassword($ticket: String!, $newPassword: String!) {
    setPasswordByRecovery(
      input: { ticket: $ticket, newPassword: $newPassword }
    ) {
      status
    }
  }
`),w=t(`
  mutation ResendRecoveryEmail($ticket: String!) {
    resendRecoveryEmail(input: { ticket: $ticket }) {
      status
      progressUrl
    }
  }
`),T=t(`
  fragment RecoverPassword_userRecoveryTicket on UserRecoveryTicket {
    username
    email
  }
`),E=t(`
  fragment RecoverPassword_siteConfig on SiteConfig {
    ...PasswordCreationDoubleInput_siteConfig
  }
`),D=t(`
  query PasswordRecovery($ticket: String!) {
    siteConfig {
      ...RecoverPassword_siteConfig
    }

    userRecoveryTicket(ticket: $ticket) {
      status
      ...RecoverPassword_userRecoveryTicket
    }
  }
`),O=e=>n({queryKey:[`passwordRecovery`,e],queryFn:({signal:t})=>i({query:D,signal:t,variables:{ticket:e}})}),k=()=>{let{t:e}=l();return(0,S.jsxs)(v,{children:[(0,S.jsx)(y,{Icon:p,title:e(`frontend.password_reset.consumed.title`),subtitle:e(`frontend.password_reset.consumed.subtitle`),invalid:!0}),(0,S.jsx)(x,{kind:`secondary`,to:`/`,reloadDocument:!0,children:e(`action.start_over`)})]})},A=t=>{let{t:n}=l(),r=e(T,t.userRecoveryTicket),s=a({mutationFn:async({ticket:e})=>{let t=await i({query:w,variables:{ticket:e}});if(t.resendRecoveryEmail.status===`SENT`){if(!t.resendRecoveryEmail.progressUrl)throw Error(`Unexpected response, missing progress URL`);window.location.href=t.resendRecoveryEmail.progressUrl,await new Promise(()=>void 0)}return t.resendRecoveryEmail}});return(0,S.jsxs)(v,{children:[(0,S.jsx)(y,{Icon:p,title:n(`frontend.password_reset.expired.title`),subtitle:n(`frontend.password_reset.expired.subtitle`,{email:r.email}),invalid:!0}),s.data?.status===`RATE_LIMITED`&&(0,S.jsx)(b,{type:`critical`,title:n(`frontend.errors.rate_limit_exceeded`)}),(0,S.jsxs)(f,{kind:`primary`,disabled:s.isPending,onClick:e=>{e.preventDefault(),s.mutate({ticket:t.ticket})},children:[!!s.isPending&&(0,S.jsx)(o,{inline:!0}),n(`frontend.password_reset.expired.resend_email`)]}),(0,S.jsx)(x,{kind:`secondary`,to:`/`,reloadDocument:!0,children:n(`action.start_over`)})]})},j=t=>{let{t:n}=l(),r=d(),s=e(E,t.siteConfig),c=e(T,t.userRecoveryTicket),u=a({mutationFn:async({ticket:e,form:t})=>{let n=t.get(`new_password`);if(n!==t.get(`new_password_again`))throw Error(`passwords mismatch; this should be checked by the form`);let a=await i({query:C,variables:{ticket:e,newPassword:n}});return a.setPasswordByRecovery.status===`ALLOWED`&&await r({to:`/`,reloadDocument:!0}),a.setPasswordByRecovery}}),f=async e=>{e.preventDefault();let n=new FormData(e.currentTarget);u.mutate({ticket:t.ticket,form:n})},p=u.error!==null,x=g(n,u.data?.status);return(0,S.jsx)(v,{children:(0,S.jsxs)(`div`,{className:`flex flex-col gap-10`,children:[(0,S.jsx)(y,{Icon:h,title:n(`frontend.password_reset.title`),subtitle:n(`frontend.password_reset.subtitle`)}),(0,S.jsxs)(_.Root,{onSubmit:f,method:`POST`,children:[p&&(0,S.jsx)(b,{type:`critical`,title:n(`frontend.password_change.failure.title`),children:n(`frontend.password_change.failure.description.unspecified`)}),x!==void 0&&(0,S.jsx)(b,{type:`critical`,title:n(`frontend.password_change.failure.title`),children:x}),(0,S.jsx)(`input`,{type:`hidden`,name:`username`,autoComplete:`username`,value:c.username}),(0,S.jsx)(m,{siteConfig:s,forceShowNewPasswordInvalid:u.data?.status===`INVALID_NEW_PASSWORD`||!1}),(0,S.jsxs)(_.Submit,{kind:`primary`,disabled:u.isPending,children:[!!u.isPending&&(0,S.jsx)(o,{inline:!0}),n(`action.save_and_continue`)]})]})]})})};function M(){let{ticket:e}=u({from:`/password/recovery/`}),{data:{siteConfig:t,userRecoveryTicket:n}}=r(O(e));if(!n)throw c();switch(n.status){case`EXPIRED`:return(0,S.jsx)(A,{ticket:e,userRecoveryTicket:n});case`CONSUMED`:return(0,S.jsx)(k,{});case`VALID`:return(0,S.jsx)(j,{ticket:e,siteConfig:t,userRecoveryTicket:n});default:{let e=n.status;throw Error(`Unhandled case: ${e}`)}}}export{M as component};
//# sourceMappingURL=password.recovery.index-gCYlsB1r.js.map