import{a as N,j as e,b}from"./@tanstack-D1KSn3AD.js";import{z as l,k as x,l as f,j as h,F as j,B as m,f as D,ap as S,h as P,i as E,G as _}from"./index-M2LMbZSu.js";import{f as T}from"./@react-router-t1nY6c48.js";import{E as k}from"./EmptyState-PtKve-r-.js";import{o as C}from"./url-BxOqGmlk.js";import{E as F}from"./Error-DYh_RW8F.js";import{r as p}from"./@radix-Mqgm9uyP.js";import{u as I}from"./index.esm-5yOGu3_l.js";import{S as A}from"./check-circle-B_YRDK8p.js";import"./@reactflow-DOgoC88E.js";const V=l.object({device_id:l.string().optional(),user_code:l.string().optional()});function z(){const[s]=T(),{device_id:t,user_code:r}=V.parse({device_id:s.get("device_id")||void 0,user_code:s.get("user_code")||void 0});return{user_code:r,device_id:t}}function q({deviceId:s,queryParams:t}){return["devices",s,t]}async function B({deviceId:s,queryParams:t}){const r=x(f.devices.detail(s)+"?"+C(t)),i=await h(r,{method:"GET",credentials:"include",headers:{"Content-Type":"application/json"}});if(!i.ok)throw new j({message:"Error while fetching Device details",status:i.status,statusText:i.statusText});return i.json()}function Y(s,t){return N({queryKey:q(s),queryFn:async()=>B(s),...t})}function G({device:s}){var t,r,i,a,c,n;return e.jsx(m,{className:"w-full p-5",children:e.jsxs("dl",{className:"flex flex-col gap-5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("dt",{children:"IP Address"}),e.jsx("dd",{children:(t=s.body)==null?void 0:t.ip_address})]}),((r=s.metadata)==null?void 0:r.city)&&((i=s.metadata)==null?void 0:i.country)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("dt",{children:"Location"}),e.jsxs("dd",{children:[(a=s.metadata)==null?void 0:a.city,", ",(c=s.metadata)==null?void 0:c.country]})]}),e.jsxs("div",{className:"flex min-w-0 items-center justify-between",children:[e.jsx("dt",{children:"Hostname"}),e.jsx("dd",{className:"truncate",children:(n=s.body)==null?void 0:n.hostname})]})]})})}async function K({deviceId:s,payload:t}){const r=x(f.devices.verify(s)),i=await h(r,{method:"PUT",credentials:"include",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)});if(!i.ok){const a=await i.json().then(c=>c.detail).catch(()=>["","Failed to verify device."]);throw new j({status:i.status,statusText:i.statusText,message:a[1]||"Failed to verify device."})}return i.json()}function L(s){return b({mutationFn:async t=>K(t),...s})}function W({deviceId:s,user_code:t,setSuccess:r}){const i=p.useId(),{register:a,handleSubmit:c,watch:n,setValue:d}=I(),{toast:v}=D(),{mutate:y,isPending:g}=L({onSuccess:()=>{r(!0)},onError:o=>{o instanceof Error&&v({status:"error",emphasis:"subtle",icon:e.jsx(E,{className:"h-5 w-5 shrink-0 fill-error-700"}),description:o.message,rounded:!0})}});function w(o){y({deviceId:s,payload:{user_code:t,trusted_device:o.trustDevice}})}return e.jsxs("form",{onSubmit:c(w),className:"flex flex-col gap-5",children:[e.jsxs("div",{className:"flex items-start gap-2",children:[e.jsx(S,{...a("trustDevice"),onCheckedChange:o=>d("trustDevice",!!o),id:i}),e.jsxs("label",{htmlFor:i,children:[e.jsx("p",{children:"Trust this device"}),e.jsx("p",{className:"text-theme-text-secondary",children:"We won't ask you again soon on this device."})]})]}),e.jsx(P,{disabled:!n("trustDevice")||g,size:"md",className:"flex w-full justify-center",children:"Authorize this device"})]})}function H(){return e.jsxs(m,{className:"flex min-w-[540px] flex-col items-center justify-center space-y-7 px-7 py-9",children:[e.jsx(A,{className:"h-[120px] w-[120px] fill-theme-text-success"}),e.jsxs("div",{className:"text-center",children:[e.jsx("p",{className:"text-display-xs font-semibold",children:"You successfully added your device"}),e.jsx("p",{className:"text-theme-text-secondary",children:"You may close this screen and return to your CLI."})]})]})}function se(){const{device_id:s,user_code:t}=z(),[r,i]=p.useState(!1),{data:a,isPending:c,isError:n,error:d}=Y({deviceId:s,queryParams:{user_code:t}},{enabled:!!s&&!!t});return!s||!t?e.jsx(k,{children:e.jsx("p",{children:"Invalid device verification link."})}):n?e.jsx(u,{children:e.jsx(F,{isAlertCircle:!0,err:d})}):c?e.jsx(u,{children:e.jsx(_,{})}):r?e.jsx(H,{}):e.jsx(u,{children:e.jsxs("div",{className:"w-full space-y-7",children:[e.jsxs("div",{className:"text-center",children:[e.jsx("h1",{className:"mb-0.5 text-display-xs font-semibold",children:"Authorize a new device"}),e.jsx("p",{className:"text-theme-text-secondary",children:"You are logging in from a new device."})]}),e.jsx(G,{device:a}),e.jsx(W,{setSuccess:i,deviceId:s,user_code:t})]})})}function u({children:s}){return e.jsx(m,{className:"flex w-full min-w-[540px] flex-col items-center justify-center gap-5 p-7",children:s})}export{se as default};
