import{c as k,m8 as E,r as p,m9 as $,j as d,ma as P,bF as f}from"./index-7d3dbb9c.js";import{L as x}from"./LoadingButton-323cc52e.js";const b=({instance_id:g,app_label:o,model_name:n,downloadFileName:y,hidePrintButton:m=!1})=>{const h=k(),{printTemplate:{TemplateList:j},system:{userAccessList:i}}=E(),[D,s]=p.useState(!1),[F,c]=p.useState(!1);p.useEffect(()=>{o&&n&&h($({app_label:o,model_name:n,pageParams:{no_of_pages:1,page_size:10,page:1}}))},[o,n]);const u=async(e,r)=>{try{const a=e==null?void 0:e.file_url;if(!a){f.fire({icon:"error",title:"Error",text:"PDF file not available"});return}if(r===1){s(!1);const t=window.open(a,"_blank");t?t.onload=()=>{setTimeout(()=>{t.print()},1e3)}:f.fire({icon:"error",title:"Error",text:"Unable to open print window. Please check popup blocker."})}else c(!1),fetch(a).then(t=>t.blob()).then(t=>{const w=window.URL.createObjectURL(t),l=document.createElement("a");l.href=w,l.download=y,document.body.appendChild(l),l.click(),document.body.removeChild(l),window.URL.revokeObjectURL(w)}).catch(t=>{console.error("Download failed:",t),f.fire({icon:"error",title:"Error",text:"Failed to download PDF"})})}catch{s(!1),c(!1),f.fire({icon:"error",title:"Error",text:"Failed to handle PDF"})}};return d.jsxs(d.Fragment,{children:[!m&&(i==null?void 0:i.indexOf(`${o}.print_${n}`))!==-1&&d.jsxs(x,{loading:D,variant:"contained",onClick:()=>{s(!0),h(P({instance_id:g,model_path:`${o}.${n}`})).then(async e=>{var r,a;console.log((r=e.payload)==null?void 0:r.response),u((a=e.payload)==null?void 0:a.response,1)}).catch(e=>{console.log("err",e),s(!1)})},children:[" ","Print"]}),(i==null?void 0:i.indexOf(`${o}.download_${n}`))!==-1&&d.jsx(x,{loading:F,variant:"contained",size:"large",onClick:()=>{c(!0),h(P({instance_id:g,model_path:`${o}.${n}`})).then(async e=>{var r;u((r=e.payload)==null?void 0:r.response,2)}).catch(e=>{c(!1)})},children:"Download PDF"})]})};export{b as P};
