import{j as e}from"./@radix-D8tIKeo-.js";import{F as x,H as f,a6 as N,E as h,G as j,k as m,w as S,V as D,B as E,x as P,S as C,at as _}from"./index-B44sgOyQ.js";import{f as k}from"./@react-router-D1ODL_Ub.js";import{z as o,u as F,C as T,t as I}from"./@zod-2qqdh1cM.js";import{E as V}from"./EmptyState-CGTtSpJ6.js";import{b as A,c as z}from"./@tanstack-nMqraTcu.js";import{E as q}from"./Error-BACoGdK9.js";import{a as p}from"./@monaco-editor-CuNQBT0l.js";import"./@elkjs-CmhuJAjY.js";import"./@reactflow-DgowYF6H.js";const B=o.object({device_id:o.string().optional(),user_code:o.string().optional()});function Y(){const[s]=k(),{device_id:t,user_code:r}=B.parse({device_id:s.get("device_id")||void 0,user_code:s.get("user_code")||void 0});return{user_code:r,device_id:t}}const G=o.object({trustDevice:o.boolean()});function H({deviceId:s,queryParams:t}){return["devices",s,t]}async function K({deviceId:s,queryParams:t}){const r=x(f.devices.detail(s)+"?"+N(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 L(s,t){return A({queryKey:H(s),queryFn:async()=>K(s),...t})}function R({device:s}){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:s.body?.ip_address})]}),s.metadata?.city&&s.metadata?.country&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("dt",{children:"Location"}),e.jsxs("dd",{children:[s.metadata?.city,", ",s.metadata?.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:s.body?.hostname})]})]})})}async function W({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 c=await i.json().then(n=>n.detail).catch(()=>["","Failed to verify device."]);throw new j({status:i.status,statusText:i.statusText,message:c[1]||"Failed to verify device."})}return i.json()}function J(s){return z({mutationFn:async t=>W(t),...s})}function M({deviceId:s,user_code:t,setSuccess:r}){const i=p.useId(),{handleSubmit:c,formState:{isValid:n},control:d}=F({resolver:I(G),defaultValues:{trustDevice:!1}}),{toast:l}=S(),{mutate:v,isPending:y}=J({onSuccess:()=>{r(!0)},onError:a=>{a instanceof Error&&l({status:"error",emphasis:"subtle",icon:e.jsx(P,{className:"h-5 w-5 shrink-0 fill-error-700"}),description:a.message,rounded:!0})}});function g(a){v({deviceId:s,payload:{user_code:t,trusted_device:a.trustDevice}})}return e.jsxs("form",{onSubmit:c(g),className:"flex flex-col gap-5",children:[e.jsxs("div",{className:"flex items-start gap-2",children:[e.jsx(T,{control:d,name:"trustDevice",render:({field:{onChange:a,value:w}})=>e.jsx(D,{checked:w,onCheckedChange:b=>a(!!b),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(E,{disabled:y||!n,size:"md",className:"flex w-full justify-center",children:"Authorize this device"})]})}function O(){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(C,{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 ae(){const{device_id:s,user_code:t}=Y(),[r,i]=p.useState(!1),{data:c,isPending:n,isError:d,error:l}=L({deviceId:s,queryParams:{user_code:t}},{enabled:!!s&&!!t});return!s||!t?e.jsx(V,{children:e.jsx("p",{children:"Invalid device verification link."})}):d?e.jsx(u,{children:e.jsx(q,{isAlertCircle:!0,err:l})}):n?e.jsx(u,{children:e.jsx(_,{})}):r?e.jsx(O,{}):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(R,{device:c}),e.jsx(M,{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{ae as default};
