import{aM as b,aJ as e,aN as S,r as x}from"./@ui-core-DKdciH1Z.js";import{H as f,J as h,aA as D,F as j,G as v,k as m,x as P,_,B as C,y as k,S as E,K as F,aH as T}from"./index-D7KLAbvI.js";import{f as A}from"./@react-router-DaIuhn3X.js";import{z as o,u as I,t as V,C as z}from"./@zod-DKyD3xVW.js";import{E as q}from"./Error-BzxhWqM2.js";import"./@reactflow-CJzATMj6.js";import"./@monaco-editor-CSMpaClf.js";const B=o.object({device_id:o.string().optional(),user_code:o.string().optional()});function H(){const[s]=A(),{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 J=o.object({trustDevice:o.boolean()});function K({deviceId:s,queryParams:t}){return["devices",s,t]}async function Y({deviceId:s,queryParams:t}){const r=f(h.devices.detail(s)+"?"+D(t)),i=await j(r,{method:"GET",credentials:"include",headers:{"Content-Type":"application/json"}});if(!i.ok)throw new v({message:"Error while fetching Device details",status:i.status,statusText:i.statusText});return i.json()}function G(s,t){return b({queryKey:K(s),queryFn:async()=>Y(s),...t})}function L({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 M({deviceId:s,payload:t}){const r=f(h.devices.verify(s)),i=await j(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 v({status:i.status,statusText:i.statusText,message:c[1]||"Failed to verify device."})}return i.json()}function R(s){return S({mutationFn:async t=>M(t),...s})}function W({deviceId:s,user_code:t,setSuccess:r}){const i=x.useId(),{handleSubmit:c,formState:{isValid:n},control:d}=I({resolver:V(J),defaultValues:{trustDevice:!1}}),{toast:l}=P(),{mutate:p,isPending:y}=R({onSuccess:()=>{r(!0)},onError:a=>{a instanceof Error&&l({status:"error",emphasis:"subtle",icon:e.jsx(k,{className:"h-5 w-5 shrink-0 fill-error-700"}),description:a.message,rounded:!0})}});function g(a){p({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(z,{control:d,name:"trustDevice",render:({field:{onChange:a,value:w}})=>e.jsx(_,{checked:w,onCheckedChange:N=>a(!!N),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(C,{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(E,{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 te(){const{device_id:s,user_code:t}=H(),[r,i]=x.useState(!1),{data:c,isPending:n,isError:d,error:l}=G({deviceId:s,queryParams:{user_code:t}},{enabled:!!s&&!!t});return!s||!t?e.jsx(F,{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(T,{})}):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(L,{device:c}),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{te as default};
