1import{co as T,cp as O,b as D,a as F,d as R,G as H,r as h,t as z,j as e,C as B,g as L,h as M,o as $,k as q,l as J,am as G,c as p,ak as V,L as K,cq as W,W as X}from"./main-DX7XzkoU.js";import{A as Y,a as Q,b as Z}from"./alert-DNYoIZtf.js";import{c as ee,h as se,o as re,a as E,p as P,m as ae,b as te}from"./instagram-connection-error-C5t4-2DQ.js";import{i as ne}from"./lead-DkePIxeP.js";import{A as oe}from"./routes-C6M2Yg1X.js";import{C as ce}from"./circle-check-DRpR_UoF.js";import{C as U}from"./circle-x-ys4HOCgl.js";import{I as ie}from"./instagram-uOX4SJX4.js";function le(r,c){var n;if(!r||!T(c))return null;let a;try{a=JSON.parse(atob(r))}catch{return null}const t=(T(a.origin)?new URL(a.origin).origin:null)??(a.brand?((n=O[a.brand])==null?void 0:n.baseUrl)??null:null);return t&&t!==c?t:null}const N="/dashboard/posts",ue=oe,we=()=>{const{t:r}=D(),c=F(),[a]=R(),{fetchProfiles:k}=H(),[t,n]=h.useState("processing"),[_,u]=h.useState(""),[i,b]=h.useState(null),x=h.useMemo(()=>ne()?ue:N,[]);h.useEffect(()=>{const l=a.get("code"),o=a.get("state"),y=le(o,window.location.origin);if(y){const s=new URL(`${y}/instagram-oauth-callback`);s.search=window.location.search,window.location.replace(s.toString());return}const d=a.get("error"),m=a.get("error_reason"),g=a.get("error_description"),j=ee({code:l,error:d,errorReason:m,errorDescription:g});if(se({code:l,error:d,errorReason:m,errorDescription:g,state:o})){const s=re(o);z("instagram_connect_return",{...s.fields,outcome:j},void 0,`${s.key}_${j}`)}const f=`oauth_callback_${l}`,C=sessionStorage.getItem(f);if(C){try{const s=JSON.parse(C);b(s),s.status==="success"?(n("success"),setTimeout(()=>c(x,{replace:!0}),2e3)):s.status==="wrong-account"?n("wrong-account"):(n("error"),u(s.errorMessage||"Erro desconhecido"))}catch{sessionStorage.removeItem(f)}return}(async()=>{if(d){console.error("Instagram OAuth error:",{error:d,errorReason:m,errorDescription:g});const s=g||m||d;if(j==="blocked"){W("Instagram OAuth bloqueado pela atividade futura",{level:"info",extra:{error:d,errorReason:m,errorDescription:g,hasState:!!o,hasProfileId:!!P(o)}}),c(E(o),{replace:!0});return}n("error"),u(s);return}if(!l||!o){n("error"),u(r("publishing.oauth.missingParams","Parâmetros de autorização ausentes"));return}try{const{data:s,error:w}=await X.functions.invoke("instagram-oauth-callback",{body:{code:l,state:o}});if(w){console.error("OAuth callback error:",w),n("error"),u(w.message||r("publishing.oauth.callbackError","Erro no callback"));return}if(!(s!=null&&s.success)){if(s!=null&&s.wrongAccount){const I={instagramUsername:s.instagramUsername,profileId:s.profileId,expectedUsername:s.expectedUsername};b(I),n("wrong-account"),sessionStorage.setItem(f,JSON.stringify({...I,status:"wrong-account"}));return}n("error"),u((s==null?void 0:s.error)||r("publishing.oauth.callbackError","Erro no callback"));return}const S={instagramUsername:s.instagramUsername,profileId:s.profileId,expectedUsername:s.expectedUsername};b(S),sessionStorage.setItem(f,JSON.stringify({...S,status:"success"})),ae(o),await k(),n("success"),setTimeout(()=>{c(x,{replace:!0})},2e3)}catch(s){console.error("Unexpected error during OAuth callback:",s),n("error"),u(r("publishing.oauth.unexpectedError","Erro inesperado"))}})()},[]);const v=()=>{const l=a.get("code");l&&sessionStorage.removeItem(`oauth_callback_${l}`);const o=(i==null?void 0:i.profileId)??P(a.get("state"));c(te(o),{replace:!0})},A=()=>{c(N,{replace:!0})};return e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-background p-4",children:e.jsxs(B,{className:"w-full max-w-md",children:[e.jsxs(L,{className:"text-center",children:[e.jsxs(M,{className:"flex items-center justify-center gap-2",children:[t==="processing"&&e.jsxs(e.Fragment,{children:[e.jsx($,{className:"h-6 w-6 animate-spin text-primary"}),r("publishing.oauth.processing","Processando...")]}),t==="success"&&e.jsxs(e.Fragment,{children:[e.jsx(ce,{className:"h-6 w-6 text-green-500"}),r("publishing.oauth.success","Conectado com sucesso!")]}),t==="wrong-account"&&e.jsxs(e.Fragment,{children:[e.jsx(U,{className:"h-6 w-6 text-destructive"}
1),r("publishing.oauth.wrongAccount","Conta incorreta")]}),t==="error"&&e.jsxs(e.Fragment,{children:[e.jsx(U,{className:"h-6 w-6 text-destructive"}),r("publishing.oauth.error","Erro na conexão")]})]}),e.jsxs(q,{children:[t==="processing"&&r("publishing.oauth.processingDescription","Finalizando a conexão com o Instagram..."),t==="success"&&r("publishing.oauth.successDescription","Sua conta do Instagram foi conectada com sucesso!"),t==="error"&&_]})]}),e.jsxs(J,{className:"space-y-4",children:[t==="wrong-account"&&i&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs(Y,{variant:"destructive",children:[e.jsx(G,{className:"h-4 w-4"}),e.jsx(Q,{children:r("publishing.oauth.wrongAccountTitle","Você conectou a conta errada")}),e.jsxs(Z,{className:"mt-2 space-y-2",children:[e.jsxs("p",{className:"text-sm",children:[r("publishing.oauth.wrongAccountExpected","Você deveria conectar")," ",e.jsxs("strong",{children:["@",i.expectedUsername]})]}),e.jsxs("p",{className:"text-sm",children:[r("publishing.oauth.wrongAccountConnected","Mas fez login com")," ",e.jsxs("strong",{children:["@",i.instagramUsername]})]}),e.jsx("p",{className:"text-sm mt-3 text-muted-foreground",children:r("publishing.oauth.wrongAccountHint","A conexão foi cancelada por segurança. Tente novamente fazendo login com a conta correta.")})]})]}),e.jsxs("div",{className:"flex flex-col gap-2",children:[e.jsxs(p,{onClick:v,className:"w-full gap-2",children:[e.jsx(V,{className:"h-4 w-4"}),r("publishing.oauth.tryAgainCorrectAccount","Tentar novamente")]}),e.jsx(p,{variant:"outline",onClick:A,className:"w-full",children:r("publishing.oauth.goToDashboard","Ir para o dashboard")})]}),e.jsxs("div",{className:"rounded-lg overflow-hidden border-2 border-amber-400 dark:border-amber-500",children:[e.jsxs("p",{className:"text-sm font-semibold text-center text-amber-700 dark:text-amber-400 py-2 px-3 bg-amber-50 dark:bg-amber-950/50 flex items-center justify-center gap-2",children:[e.jsx("span",{children:"ð"}),r("publishing.oauth.watchHowToFix","Veja como resolver:"),e.jsx("span",{children:"ð"})]}),e.jsx("video",{src:"https://zyjxmpdizadudqwzggbt.supabase.co/storage/v1/object/public/instagem/instagem-connection.mp4",controls:!0,autoPlay:!0,muted:!0,loop:!0,playsInline:!0,className:"w-full"})]}),e.jsx("p",{className:"text-xs text-center text-muted-foreground",children:r("publishing.oauth.wrongAccountTip","Dica: Se você quer conectar @{{username}}, primeiro adicione esse perfil na plataforma.",{username:i.instagramUsername})})]}),t==="success"&&e.jsxs("div",{className:"flex flex-col items-center gap-4",children:[e.jsx("div",{className:"w-16 h-16 rounded-full bg-green-100 dark:bg-green-900/30 flex items-center justify-center",children:e.jsx(ie,{className:"w-8 h-8 text-green-600 dark:text-green-400"})}),i&&e.jsxs("p",{className:"text-center text-muted-foreground",children:["@",i.instagramUsername," ",r("publishing.oauth.nowConnected","agora está conectado")]}),e.jsx(p,{onClick:()=>c(x,{replace:!0}),children:r(x===N?"publishing.oauth.goToPosts":"publishing.oauth.continueDiagnostic")})]}),t==="error"&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex gap-2 justify-center",children:[e.jsx(p,{variant:"outline",onClick:A,children:r("publishing.oauth.goToDashboard")}),e.jsx(p,{onClick:v,children:r("publishing.oauth.tryAgain")})]}),a.get("error")&&e.jsx("p",{className:"text-center text-xs text-muted-foreground",children:e.jsx(K,{to:E(a.get("state")),className:"font-medium text-primary underline-offset-4 hover:underline",children:r("instagram_connection_help.entry_link")})})]})]})]})})};export{we as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.