import{a as ne,c as ae,j as r,G as oe}from"./index-D9pJRAs5.js";import{r as i}from"./vendor-D6aTFYy9.js";import{X as ie,L as le,bT as ce}from"./ui-CtYVlq7P.js";import"./pdf-Bz_mU5mG.js";const xe=()=>{var _;const{user:o,updateProfile:L}=ne(),{paperTypes:O,finishingOptions:R,printingOptions:D,chatbots:J,activeCustomerChatbotId:V,activeAdminChatbotId:B,customers:N,suppliers:C,orders:v,expenses:I,payments:$,accountingEntries:T,cheques:Y,partners:H}=ae(),[k,X]=i.useState(null),[f,m]=i.useState([]),[b,z]=i.useState(""),[w,j]=i.useState(!1),[E,Q]=i.useState(()=>{var e,t;return((t=(e=o==null?void 0:o.preferences)==null?void 0:e.chatbot)==null?void 0:t.isOpen)??!1}),U=e=>{var t;Q(e),o&&L&&L({preferences:{...o.preferences,chatbot:{...((t=o.preferences)==null?void 0:t.chatbot)||{},isOpen:e}}})},[y,Z]=i.useState({x:window.innerWidth-80,y:window.innerHeight-80}),[x,G]=i.useState(!1),S=i.useRef({x:0,y:0}),P=i.useRef({x:0,y:0}),M=i.useRef(!1),q=i.useRef(null),A=(o==null?void 0:o.role)==="customer",u=(o==null?void 0:o.role)==="admin"||(o==null?void 0:o.role)==="manager",ee=u?B:V,l=J.find(e=>e.id===ee),K=i.useMemo(()=>u?`Here is a summary of the current business data: ${JSON.stringify({customerCount:N.length,supplierCount:C.length,orderCount:v.length,totalRevenue:v.reduce((e,t)=>e+t.totalPrice,0),totalExpenses:I.reduce((e,t)=>e+t.amount,0),customers:N.map(({id:e,name:t,company:s,totalSpent:a,accountBalance:n})=>({id:e,name:t,company:s,totalSpent:a,accountBalance:n})),suppliers:C.map(({id:e,name:t,category:s,totalPurchases:a,currentBalance:n})=>({id:e,name:t,category:s,totalPurchases:a,currentBalance:n})),orders:v.map(({id:e,title:t,customerName:s,totalPrice:a,status:n,paymentStatus:c,createdAt:d})=>({id:e,title:t,customerName:s,totalPrice:a,status:n,paymentStatus:c,createdAt:d})),expenses:I.map(({id:e,category:t,description:s,amount:a,date:n})=>({id:e,category:t,description:s,amount:a,date:n})),paymentsReceived:$.length,totalPayments:$.reduce((e,t)=>e+t.amount,0),accountingEntries:T.map(({id:e,date:t,type:s,amount:a,description:n,category:c,reference:d})=>({id:e,date:t,type:s,amount:a,description:n,category:c,reference:d})),cheques:Y.map(({id:e,type:t,partyName:s,chequeNumber:a,amount:n,dueDate:c,status:d})=>({id:e,type:t,partyName:s,chequeNumber:a,amount:n,dueDate:c,status:d})),partners:H.map(({id:e,name:t,totalContribution:s,totalDistribution:a,netEquity:n})=>({id:e,name:t,totalContribution:s,totalDistribution:a,netEquity:n}))})}`:A?`PRODUCT DATA:
            PAPER TYPES:
            ${O.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 OPTIONS:
            ${R.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 OPTIONS:
            ${D.map(e=>`- ${e.name} (${e.type}): ${e.machineModel}. Pricing is tiered.`).join(`
`)}
            `:"",[u,A,N,C,v,I,$,T,Y,H,O,R,D]);i.useEffect(()=>{E?l&&(async()=>{try{const t="PLACEHOLDER_AI_KEY",s=new oe({apiKey:t}),a=`${l.systemInstruction}

${K}`,n=s.chats.create({model:l.model||"gemini-1.5-flash",config:{systemInstruction:a}});X(n),j(!0);const c=u?`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.`,d=await n.sendMessageStream({message:c});let p="";for await(const re of d)p+=re.text;m([{role:"model",text:p}])}catch(t){console.error("Error initializing Gemini chat:",t),m([{role:"model",text:"Sorry, I am having trouble connecting. Please check your configuration and try again later."}])}finally{j(!1)}})():(X(null),m([]))},[E,l,K,u]),i.useEffect(()=>{var e;(e=q.current)==null||e.scrollIntoView({behavior:"smooth"})},[f,w]);const F=async()=>{if(!b.trim()||w||!k)return;const e={role:"user",text:b};m(s=>[...s,e]);const t=b;z(""),j(!0);try{const s=await k.sendMessageStream({message:t});let a="";m(n=>[...n,{role:"model",text:""}]);for await(const n of s)a+=n.text,m(d=>{const p=[...d];return p[p.length-1]={role:"model",text:a},p})}catch(s){console.error("Error sending message:",s),m(a=>[...a,{role:"model",text:"I encountered an error. Please try asking again."}])}finally{j(!1)}},W=i.useCallback(e=>{const t="touches"in e?e.touches[0].clientX:e.clientX,s="touches"in e?e.touches[0].clientY:e.clientY;G(!0),M.current=!1,S.current={x:t,y:s},P.current={x:t-y.x,y:s-y.y}},[y]),h=i.useCallback(e=>{if(!x)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-S.current.x,2)+Math.pow(s-S.current.y,2))>5&&(M.current=!0);const n=Math.max(10,Math.min(window.innerWidth-70,t-P.current.x)),c=Math.max(10,Math.min(window.innerHeight-80,s-P.current.y));Z({x:n,y:c})},[x]),g=i.useCallback(()=>{G(!1)},[]);i.useEffect(()=>(x?(window.addEventListener("mousemove",h),window.addEventListener("mouseup",g),window.addEventListener("touchmove",h),window.addEventListener("touchend",g)):(window.removeEventListener("mousemove",h),window.removeEventListener("mouseup",g),window.removeEventListener("touchmove",h),window.removeEventListener("touchend",g)),()=>{window.removeEventListener("mousemove",h),window.removeEventListener("mouseup",g),window.removeEventListener("touchmove",h),window.removeEventListener("touchend",g)}),[x,h,g]);const te=()=>{M.current||U(!0)};if(!o||!A&&!u||!l)return null;if(!E)return r.jsxs("button",{onMouseDown:W,onTouchStart:W,onClick:te,style:{left:`${y.x}px`,top:`${y.y}px`,cursor:x?"grabbing":"grab"},className:`fixed z-[100] w-14 h-14 sm:w-16 sm:h-16 ${u?"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 ${x?"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:[r.jsx("img",{src:l.avatarUrl||"",alt:"Chat Avatar",className:"w-full h-full object-cover rounded-full p-1"}),r.jsxs("span",{className:"absolute -top-1 -right-1 flex h-4 w-4",children:[r.jsx("span",{className:"animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"}),r.jsx("span",{className:"relative inline-flex rounded-full h-4 w-4 bg-green-500 border-2 border-white"})]})]});const se=u?"Analyze finances, customers...":"Ask about products or for a quote...";return r.jsxs("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:[r.jsxs("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:[r.jsxs("div",{className:"flex items-center space-x-3",children:[r.jsxs("div",{className:"relative",children:[r.jsx("img",{src:l.avatarUrl||"",alt:"Chat Avatar",className:"w-10 h-10 object-cover rounded-full border-2 border-blue-500"}),r.jsx("div",{className:"absolute bottom-0 right-0 w-3 h-3 bg-green-500 border-2 border-white dark:border-gray-800 rounded-full"})]}),r.jsxs("div",{children:[r.jsx("h3",{className:"font-black text-sm sm:text-base text-gray-900 dark:text-white uppercase tracking-tight",children:l.name}),r.jsx("p",{className:"text-[10px] sm:text-xs text-green-600 dark:text-green-400 font-bold uppercase tracking-widest",children:"Online"})]})]}),r.jsx("button",{onClick:()=>U(!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:r.jsx(ie,{className:"h-5 w-5"})})]}),r.jsxs("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:[f.map((e,t)=>{var s;return r.jsxs("div",{className:`flex items-start gap-2.5 sm:gap-3 ${e.role==="user"?"justify-end":""}`,children:[e.role==="model"&&r.jsx("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"}),r.jsx("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:r.jsx("p",{className:"whitespace-pre-wrap leading-relaxed",children:e.text})}),e.role==="user"&&r.jsx("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=o==null?void 0:o.name)==null?void 0:s.charAt(0).toUpperCase()})]},t)}),w&&f.length>0&&((_=f[f.length-1])==null?void 0:_.role)!=="model"&&r.jsxs("div",{className:"flex items-start gap-3",children:[r.jsx("img",{src:l.avatarUrl||"",alt:"Chat Avatar",className:"w-8 h-8 object-cover rounded-full flex-shrink-0 mt-1"}),r.jsx("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:r.jsx(le,{className:"h-5 w-5 animate-spin text-indigo-500"})})]}),r.jsx("div",{ref:q})]}),r.jsxs("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:[r.jsxs("div",{className:"flex items-center space-x-2",children:[r.jsx("input",{type:"text",placeholder:se,value:b,onChange:e=>z(e.target.value),onKeyPress:e=>e.key==="Enter"&&F(),disabled:w||!k,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"}),r.jsx("button",{onClick:F,disabled:w||!b.trim()||!k,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:r.jsx(ce,{className:"h-5 w-5"})})]}),r.jsx("p",{className:"text-[9px] text-center text-gray-400 dark:text-gray-500 mt-3 font-bold uppercase tracking-widest",children:"Powered by AI"})]})]})};export{xe as default};
