import{a as oe,f as re,d as ie,i as ae,h as le,n as ce,j as n,G as me}from"./index-C8aoN8t4.js";import{r as a}from"./vendor-ChGFRQAq.js";import{X as de,L as ue,c4 as pe}from"./ui-wfmc48pO.js";import"./pdf-DdgWE7Gk.js";const Ne=()=>{var W;const{user:i,updateProfile:A}=oe(),{paperTypes:M,finishingOptions:P,printingOptions:L}=re(),{orders:y,quotations:ge,payments:D}=ie(),{accountingEntries:R,cheques:O,partners:Y,expenses:C}=ae(),{customers:E,suppliers:k}=le(),{chatbots:_,activeCustomerChatbotId:J,activeAdminChatbotId:B}=ce(),[w,X]=a.useState(null),[v,u]=a.useState([]),[x,z]=a.useState(""),[f,j]=a.useState(!1),[F,Q]=a.useState(()=>{var e,t;return((t=(e=i==null?void 0:i.preferences)==null?void 0:e.chatbot)==null?void 0:t.isOpen)??!1}),H=e=>{var t;Q(e),i&&A&&A({preferences:{...i.preferences,chatbot:{...((t=i.preferences)==null?void 0:t.chatbot)||{},isOpen:e}}})},[N,Z]=a.useState({x:window.innerWidth-80,y:window.innerHeight-80}),[h,U]=a.useState(!1),V=a.useRef({x:0,y:0}),$=a.useRef({x:0,y:0}),I=a.useRef(!1),q=a.useRef(null),S=(i==null?void 0:i.role)==="customer",d=(i==null?void 0:i.role)==="admin"||(i==null?void 0:i.role)==="manager",ee=d?B:J,l=_.find(e=>e.id===ee),T=a.useMemo(()=>d?`Here is a summary of the current business data: ${JSON.stringify({customerCount:E.length,supplierCount:k.length,orderCount:y.length,totalRevenue:y.reduce((e,t)=>e+t.totalPrice,0),totalExpenses:C.reduce((e,t)=>e+t.amount,0),customers:E.map(({id:e,name:t,company:s,totalSpent:r,accountBalance:o})=>({id:e,name:t,company:s,totalSpent:r,accountBalance:o})),suppliers:(k||[]).map(({id:e,name:t,category:s,totalPurchases:r,currentBalance:o})=>({id:e,name:t,category:s,totalPurchases:r,currentBalance:o})),orders:(y||[]).map(({id:e,title:t,customerName:s,totalPrice:r,status:o,paymentStatus:c,createdAt:m})=>({id:e,title:t,customerName:s,totalPrice:r,status:o,paymentStatus:c,createdAt:m})),expenses:(C||[]).map(({id:e,category:t,description:s,amount:r,date:o})=>({id:e,category:t,description:s,amount:r,date:o})),paymentsReceived:D.length,totalPayments:D.reduce((e,t)=>e+t.amount,0),accountingEntries:R.map(({id:e,date:t,type:s,amount:r,description:o,category:c,reference:m})=>({id:e,date:t,type:s,amount:r,description:o,category:c,reference:m})),cheques:O.map(({id:e,type:t,partyName:s,chequeNumber:r,amount:o,dueDate:c,status:m})=>({id:e,type:t,partyName:s,chequeNumber:r,amount:o,dueDate:c,status:m})),partners:Y.map(({id:e,name:t,totalContribution:s,totalDistribution:r,netEquity:o})=>({id:e,name:t,totalContribution:s,totalDistribution:r,netEquity:o}))})}`:S?`PRODUCT DATA:
            PAPER TYPES:
            ${(M||[]).map(e=>`- ${e.name}: ${e.description}. Sizes available: ${e.sizes.map(t=>`${t.name} (${t.width}x${t.height}${t.unit}) with thicknesses: ${t.thicknesses.map(s=>`${s.thickness} (Sell price: RM${s.sellPrice})`).join(", ")}`).join("; ")}`).join(`
`)}
    
            Finishing Settings:
            ${(P||[]).map(e=>{var t;return`- ${e.name}: ${e.description}. Pricing is tiered based on quantity, starting from RM${((t=e.priceTiers[0])==null?void 0:t.sellPrice)||"N/A"}.`}).join(`
`)}
            
            Printing Settings:
            ${(L||[]).map(e=>`- ${e.name} (${e.type}): ${e.machineModel}. Pricing is tiered.`).join(`
`)}
            `:"",[d,S,E,k,y,C,D,R,O,Y,M,P,L]);a.useEffect(()=>{F?l&&(async()=>{try{const t="PLACEHOLDER_AI_KEY",s=new me({apiKey:t}),r=`${l.systemInstruction}

${T}`,o=s.chats.create({model:l.model||"gemini-1.5-flash",config:{systemInstruction:r}});X(o),j(!0);const c=d?`Hello! I'm ${l.name}, your AI assistant. How can I help you analyze the business data today?`:`Hello! Introduce yourself as ${l.name} from Pioneer Cetak and briefly ask how you can help with printing needs.`,m=await o.sendMessageStream({message:c});let b="";for await(const ne of m)b+=ne.text;u([{role:"model",text:b}])}catch(t){console.error("Error initializing Gemini chat:",t),u([{role:"model",text:"Sorry, I am having trouble connecting. Please check your configuration and try again later."}])}finally{j(!1)}})():(X(null),u([]))},[F,l,T,d]),a.useEffect(()=>{var e;(e=q.current)==null||e.scrollIntoView({behavior:"smooth"})},[v,f]);const G=async()=>{if(!x.trim()||f||!w)return;const e={role:"user",text:x};u(s=>[...s,e]);const t=x;z(""),j(!0);try{const s=await w.sendMessageStream({message:t});let r="";u(o=>[...o,{role:"model",text:""}]);for await(const o of s)r+=o.text,u(m=>{const b=[...m];return b[b.length-1]={role:"model",text:r},b})}catch(s){console.error("Error sending message:",s),u(r=>[...r,{role:"model",text:"I encountered an error. Please try asking again."}])}finally{j(!1)}},K=a.useCallback(e=>{const t="touches"in e?e.touches[0].clientX:e.clientX,s="touches"in e?e.touches[0].clientY:e.clientY;U(!0),I.current=!1,V.current={x:t,y:s},$.current={x:t-N.x,y:s-N.y}},[N]),p=a.useCallback(e=>{if(!h)return;const t="touches"in e?e.touches[0].clientX:e.clientX,s="touches"in e?e.touches[0].clientY:e.clientY;Math.sqrt(Math.pow(t-V.current.x,2)+Math.pow(s-V.current.y,2))>5&&(I.current=!0);const o=Math.max(10,Math.min(window.innerWidth-70,t-$.current.x)),c=Math.max(10,Math.min(window.innerHeight-80,s-$.current.y));Z({x:o,y:c})},[h]),g=a.useCallback(()=>{U(!1)},[]);a.useEffect(()=>(h?(window.addEventListener("mousemove",p),window.addEventListener("mouseup",g),window.addEventListener("touchmove",p),window.addEventListener("touchend",g)):(window.removeEventListener("mousemove",p),window.removeEventListener("mouseup",g),window.removeEventListener("touchmove",p),window.removeEventListener("touchend",g)),()=>{window.removeEventListener("mousemove",p),window.removeEventListener("mouseup",g),window.removeEventListener("touchmove",p),window.removeEventListener("touchend",g)}),[h,p,g]);const te=()=>{I.current||H(!0)};if(!i||!S&&!d||!l)return null;if(!F)return n.jsxDEV("button",{onMouseDown:K,onTouchStart:K,onClick:te,style:{left:`${N.x}px`,top:`${N.y}px`,cursor:h?"grabbing":"grab"},className:`fixed z-[100] w-14 h-14 sm:w-16 sm:h-16 ${d?"bg-indigo-600 hover:bg-indigo-700":"bg-blue-600 hover:bg-blue-700"} rounded-full shadow-2xl flex items-center justify-center text-white transition-all transform ${h?"scale-110 opacity-80":"hover:scale-110"} focus:outline-none focus:ring-2 focus:ring-offset-2 animate-fade-in touch-none`,"aria-label":"Open chat",children:[n.jsxDEV("img",{src:l.avatarUrl||"",alt:"Chat Avatar",className:"w-full h-full object-cover rounded-full p-1"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:267,columnNumber:17},void 0),n.jsxDEV("span",{className:"absolute -top-1 -right-1 flex h-4 w-4",children:[n.jsxDEV("span",{className:"animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:269,columnNumber:21},void 0),n.jsxDEV("span",{className:"relative inline-flex rounded-full h-4 w-4 bg-green-500 border-2 border-white"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:270,columnNumber:21},void 0)]},void 0,!0,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:268,columnNumber:17},void 0)]},void 0,!0,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:255,columnNumber:13},void 0);const se=d?"Analyze finances, customers...":"Ask about products or for a quote...";return n.jsxDEV("div",{className:"fixed inset-0 sm:inset-auto sm:bottom-6 sm:right-6 z-[100] w-full sm:max-w-sm h-full sm:h-[70vh] sm:max-h-[600px] flex flex-col bg-white dark:bg-gray-800 sm:rounded-3xl shadow-2xl overflow-hidden border-t sm:border border-gray-200 dark:border-gray-700 animate-slide-up",children:[n.jsxDEV("div",{className:"p-4 sm:p-5 border-b border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 flex justify-between items-center flex-shrink-0",children:[n.jsxDEV("div",{className:"flex items-center space-x-3",children:[n.jsxDEV("div",{className:"relative",children:[n.jsxDEV("img",{src:l.avatarUrl||"",alt:"Chat Avatar",className:"w-10 h-10 object-cover rounded-full border-2 border-blue-500"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:283,columnNumber:25},void 0),n.jsxDEV("div",{className:"absolute bottom-0 right-0 w-3 h-3 bg-green-500 border-2 border-white dark:border-gray-800 rounded-full"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:284,columnNumber:25},void 0)]},void 0,!0,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:282,columnNumber:21},void 0),n.jsxDEV("div",{children:[n.jsxDEV("h3",{className:"font-black text-sm sm:text-base text-gray-900 dark:text-white uppercase tracking-tight",children:l.name},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:287,columnNumber:25},void 0),n.jsxDEV("p",{className:"text-[10px] sm:text-xs text-green-600 dark:text-green-400 font-bold uppercase tracking-widest",children:"Online"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:288,columnNumber:25},void 0)]},void 0,!0,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:286,columnNumber:21},void 0)]},void 0,!0,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:281,columnNumber:17},void 0),n.jsxDEV("button",{onClick:()=>H(!1),className:"p-2 text-gray-400 hover:text-gray-600 dark:hover:text-white transition-colors bg-white dark:bg-gray-800 rounded-full border dark:border-gray-700 shadow-sm",children:n.jsxDEV(de,{className:"h-5 w-5"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:292,columnNumber:21},void 0)},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:291,columnNumber:17},void 0)]},void 0,!0,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:280,columnNumber:13},void 0),n.jsxDEV("div",{className:"flex-1 overflow-y-auto p-4 sm:p-6 space-y-4 sm:space-y-6 bg-white dark:bg-gray-950/20 custom-scrollbar",children:[v.map((e,t)=>{var s;return n.jsxDEV("div",{className:`flex items-start gap-2.5 sm:gap-3 ${e.role==="user"?"justify-end":""}`,children:[e.role==="model"&&n.jsxDEV("img",{src:l.avatarUrl||"",alt:"Chat Avatar",className:"w-8 h-8 object-cover rounded-full flex-shrink-0 mt-1 shadow-sm border border-gray-100"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:300,columnNumber:29},void 0),n.jsxDEV("div",{className:`max-w-[85%] sm:max-w-[80%] px-4 py-3 rounded-2xl shadow-sm text-sm sm:text-base ${e.role==="user"?"bg-indigo-600 text-white rounded-br-none":"bg-gray-100 dark:bg-gray-700 text-gray-900 dark:text-white rounded-bl-none border border-gray-200/50 dark:border-gray-600/50"}`,children:n.jsxDEV("p",{className:"whitespace-pre-wrap leading-relaxed",children:e.text},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:306,columnNumber:29},void 0)},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:302,columnNumber:25},void 0),e.role==="user"&&n.jsxDEV("div",{className:"h-8 w-8 bg-indigo-100 dark:bg-indigo-900/50 text-indigo-600 dark:text-indigo-400 rounded-full flex items-center justify-center flex-shrink-0 mt-1 font-black text-xs border border-indigo-200 dark:border-indigo-800",children:(s=i==null?void 0:i.name)==null?void 0:s.charAt(0).toUpperCase()},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:309,columnNumber:29},void 0)]},t,!0,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:298,columnNumber:21},void 0)}),f&&v.length>0&&((W=v[v.length-1])==null?void 0:W.role)!=="model"&&n.jsxDEV("div",{className:"flex items-start gap-3",children:[n.jsxDEV("img",{src:l.avatarUrl||"",alt:"Chat Avatar",className:"w-8 h-8 object-cover rounded-full flex-shrink-0 mt-1"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:317,columnNumber:25},void 0),n.jsxDEV("div",{className:"max-w-md px-5 py-3 rounded-2xl bg-gray-100 dark:bg-gray-700 text-gray-900 dark:text-white rounded-bl-none border border-gray-200/50 dark:border-gray-600/50",children:n.jsxDEV(ue,{className:"h-5 w-5 animate-spin text-indigo-500"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:319,columnNumber:29},void 0)},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:318,columnNumber:25},void 0)]},void 0,!0,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:316,columnNumber:21},void 0),n.jsxDEV("div",{ref:q},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:323,columnNumber:17},void 0)]},void 0,!0,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:296,columnNumber:13},void 0),n.jsxDEV("div",{className:"p-4 sm:p-5 border-t border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/80 flex-shrink-0",children:[n.jsxDEV("div",{className:"flex items-center space-x-2",children:[n.jsxDEV("input",{type:"text",placeholder:se,value:x,onChange:e=>z(e.target.value),onKeyPress:e=>e.key==="Enter"&&G(),disabled:f||!w,className:"flex-1 px-4 py-3 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-2xl focus:ring-2 focus:ring-indigo-500 outline-none transition-all text-sm dark:text-white shadow-inner"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:328,columnNumber:21},void 0),n.jsxDEV("button",{onClick:G,disabled:f||!x.trim()||!w,className:"bg-indigo-600 hover:bg-indigo-700 disabled:bg-gray-400 text-white p-3.5 rounded-2xl transition-all shadow-xl shadow-indigo-500/20 active:scale-90 flex-shrink-0",children:n.jsxDEV(pe,{className:"h-5 w-5"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:342,columnNumber:25},void 0)},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:337,columnNumber:21},void 0)]},void 0,!0,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:327,columnNumber:17},void 0),n.jsxDEV("p",{className:"text-[9px] text-center text-gray-400 dark:text-gray-500 mt-3 font-bold uppercase tracking-widest",children:"Powered by AI"},void 0,!1,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:345,columnNumber:17},void 0)]},void 0,!0,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:326,columnNumber:13},void 0)]},void 0,!0,{fileName:"D:/devlop/projects/printing system/src/components/FloatingChat.tsx",lineNumber:279,columnNumber:9},void 0)};export{Ne as default};
