import{j as e}from"./ui-vendor-DUfjpZ4p.js";import{h as F,e as $,r,L as g}from"./react-vendor-DlVMRrtE.js";import{u as z,s as i,am as j,n as E,B as d,b4 as I,e as T,H as V,D as Y,b7 as G,t as H}from"./index-BKwmLAPt.js";import{I as M}from"./ImporterReport-D9UG_J_9.js";import{g as O}from"./importerPayments-DOetcnu_.js";import"./charts-vendor-wgDUSDZK.js";import"./supabase-vendor-Bd7R0Yn5.js";const ee=()=>{const{id:s,type:w}=F(),a=w==="full"?"full":"basic",{user:l,isAdmin:m,isSuperAdmin:p}=z(),u=$(),[N,n]=r.useState(!0),[_,v]=r.useState(!1),[o,y]=r.useState(null),[b,D]=r.useState({}),[S,P]=r.useState([]),[k,R]=r.useState([]),[f,h]=r.useState(!1),c=r.useRef(null);r.useEffect(()=>{(async()=>{if(!l){u(`/portal/login?redirect=/portal/importer-network/report/${s}/${a}`);return}if(!s)return;n(!0);let t=m||p;if(!t){const x=await O(l.id,s);t=x==="full"||x==="basic"&&a==="basic"}if(v(t),!t){n(!1);return}const[{data:B},{data:C},{data:L},{data:q}]=await Promise.all([i.from("importer_profiles").select("*").eq("id",s).eq("published",!0).maybeSingle(),i.from("importer_verifications").select("*").eq("importer_profile_id",s).maybeSingle(),i.from("importer_officials").select("*").eq("importer_profile_id",s),i.from("importer_documents").select("id,document_type,file_name").eq("importer_profile_id",s)]);y(B),D(C||{}),P(L||[]),R(q||[]),n(!1)})()},[s,a,l,u,m,p]);const A=async()=>{if(!(!c.current||!o)){h(!0);try{const t=String(o.company_name||"importer").replace(/[^A-Za-z0-9]+/g,"_");await G(c.current,`ABTC_Importer_${t}_${a}.pdf`)}catch(t){console.error(t),H({title:"Download failed",description:(t==null?void 0:t.message)||"Could not generate PDF.",variant:"destructive"})}h(!1)}};return N?e.jsxs("div",{className:"flex items-center gap-2 p-6 text-muted-foreground",children:[e.jsx(j,{className:"h-4 w-4 animate-spin"})," Loading…"]}):_?o?e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3 print:hidden",children:[e.jsx(d,{asChild:!0,variant:"ghost",size:"sm",children:e.jsxs(g,{to:"/portal/importer-network/my-reports",children:[e.jsx(I,{className:"h-4 w-4 mr-1"})," My reports"]})}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(T,{className:"bg-accent/20 text-accent border-accent/30 gap-1",children:[e.jsx(V,{className:"h-3 w-3"})," ",a==="full"?"Full Due Diligence":"Basic"," Report"]}),e.jsxs(d,{size:"sm",onClick:A,disabled:f,className:"gap-2",children:[f?e.jsx(j,{className:"h-4 w-4 animate-spin"}):e.jsx(Y,{className:"h-4 w-4"}),"Download PDF"]})]})]}),e.jsx("div",{ref:c,children:e.jsx(M,{profile:o,verif:b,officials:S,documents:k,reportType:a})})]}):e.jsx("div",{className:"p-6",children:"Profile not found."}):e.jsxs("div",{className:"max-w-xl mx-auto py-12 text-center space-y-4",children:[e.jsx(E,{className:"h-12 w-12 mx-auto text-muted-foreground"}),e.jsx("h1",{className:"font-heading text-2xl font-bold",children:"Purchase Required"}),e.jsx("p",{className:"text-muted-foreground",children:a==="full"?"You need to purchase the Full Due Diligence report for this importer.":"You need to purchase the Basic report for this importer."}),e.jsx(d,{asChild:!0,children:e.jsx(g,{to:`/portal/importer-network/profile/${s}`,children:"Go to profile"})})]})};export{ee as default};
