import{l as R,g as z,r as n,m as S,j as e,I as T,S as q,n as O,p as $,q as B,s as F,B as b}from"./index-CfKY7XrC.js";import{C as N,a as _,b as A,d as k,c as V}from"./card-DM3GEpFz.js";import{u as Y}from"./use-org-preselect-C9l9X_04.js";function G(){const r=R.useSearch(),{session:a,isReady:U}=z(),c=!U,{orgList:g,selectedOrgId:u,setSelectedOrgId:L,orgsLoading:D}=Y(),[h,E]=n.useState(r.user_code||""),[m,j]=n.useState(null),[w,p]=n.useState(null),[i,I]=n.useState(null),l=r.return_to,v=l?"app":"terminal",f=!!r.user_code,[x,P]=n.useState(null);n.useEffect(()=>{if(!c&&!a){const t=new URL("/auth/login",window.location.origin);t.searchParams.set("callbackUrl",window.location.href),window.location.href=t.toString()}},[a,c]),n.useEffect(()=>{if(c||!a||!r.user_code)return;let t=!1;return fetch(`${S}/oauth/device/info?user_code=${encodeURIComponent(r.user_code)}`,{credentials:"include"}).then(s=>s.ok?s.json():null).then(s=>{t||!s||P({client_name:s.client_name??null,scopes:Array.isArray(s.scopes)?s.scopes:[]})}).catch(()=>{}),()=>{t=!0}},[a,c,r.user_code]);const y=async t=>{const s=h.trim().toUpperCase();if(!s){p(`Please enter the code shown in your ${v}.`);return}j(t?"approve":"deny"),p(null);try{const o=await fetch(`${S}/oauth/device/approve`,{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify({user_code:s,approved:t,...t&&u?{organization_id:u}:{}})}),d=await o.json().catch(()=>null);if(o.status===401){const C=new URL("/auth/login",window.location.origin);C.searchParams.set("callbackUrl",window.location.href),window.location.href=C.toString();return}if(d?.error==="org_selection_required"){j(null),p("Please select a workspace before approving.");return}if(!o.ok)throw new Error(typeof d?.error_description=="string"&&d.error_description||typeof d?.error=="string"&&d.error||"Failed to process device authorization");I(t?"approved":"denied")}catch(o){j(null),p(o instanceof Error?o.message:"Unexpected error")}};return n.useEffect(()=>{if(i!=="approved"||!l)return;const t=window.setTimeout(()=>{window.location.href=l},600);return()=>window.clearTimeout(t)},[i,l]),c||!a?e.jsx("main",{className:"min-h-[calc(100vh-3.5rem)] flex items-center justify-center p-4",children:e.jsx("p",{className:"text-sm text-muted-foreground",children:"Checking authentication..."})}):i?e.jsx("main",{className:"min-h-[calc(100vh-3.5rem)] flex items-center justify-center p-4",children:e.jsx(N,{className:"w-full max-w-lg",children:e.jsxs(_,{children:[e.jsx(A,{children:i==="approved"?f?"Access Authorized":"Device Authorized":"Authorization Denied"}),e.jsx(k,{children:i==="approved"?l?"Returning you to the app...":"You can close this tab and return to your terminal.":"The device was not authorized. You can close this tab."})]})})}):e.jsx("main",{className:"min-h-[calc(100vh-3.5rem)] flex items-center justify-center p-4",children:e.jsxs(N,{className:"w-full max-w-lg",children:[e.jsxs(_,{children:[e.jsx(A,{children:f?"Authorize access request":"Authorize Device"}),e.jsx(k,{children:f?"An application is requesting access to your Lobu account. Pick a workspace and approve to continue.":`Enter the code shown in your ${v} to connect your device.`})]}),e.jsxs(V,{className:"space-y-5",children:[f&&x&&e.jsxs("div",{className:"rounded-lg border p-4 space-y-1",children:[e.jsxs("p",{className:"text-sm",children:[e.jsx("span",{className:"font-medium",children:x.client_name||"An application"})," ","is requesting access to your Lobu account."]}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Access: ",x.scopes.length?x.scopes.join(", "):"profile"]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{htmlFor:"user-code",className:"text-sm font-medium",children:"Device code"}),e.jsx(T,{id:"user-code",value:h,onChange:t=>E(t.target.value.toUpperCase()),placeholder:"ABCD-1234",className:"font-mono text-center text-lg tracking-widest",maxLength:9,autoFocus:!r.user_code})]}),!D&&g.length>0&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{htmlFor:"org-select",className:"text-sm font-medium",children:"Workspace"}),e.jsxs(q,{value:u,onValueChange:L,children:[e.jsx(O,{id:"org-select",children:e.jsx($,{placeholder:"Select a workspace"})}),e.jsx(B,{children:g.map(t=>e.jsx(F,{value:t.id,children:t.name},t.id))})]})]}),w&&e.jsx("p",{className:"text-sm text-destructive",children:w}),e.jsxs("div",{className:"flex items-center justify-end gap-3",children:[e.jsx(b,{type:"button",variant:"outline",onClick:()=>y(!1),disabled:m!==null||!h.trim(),children:m==="deny"?"Denying...":"Deny"}),e.jsx(b,{type:"button",onClick:()=>y(!0),disabled:m!==null||!h.trim()||!u&&g.length>0,children:m==="approve"?"Approving...":"Approve"})]})]})]})})}export{G as component};
