1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[11381],{11381:function(e,t,a){a.r(t),a.d(t,{default:function(){return eq}});var s=a(57437),l=a(2265),r=a(99376),n=a(30166),i=a(12381),o=a(40279),d=a(96246),c=a(53417),m=a(29202),u=a(89345),x=a(13041),p=a(43044),h=a(82431),g=a(17684),v=a(15863),b=a(76399),f=a(95468),j=a(73247),_=a(82023),w=a(99397),k=a(32489),N=a(37806),y=a(792),C=a(95805),S=a(84286),A=a(53113),Z=a(88997),E=a(96362),F=a(45675),M=a(18930),z=a(76980),T=a(82222),L=a(48662),P=a(22252),R=a(89227),D=a(33276),W=a(88623),I=a(81706),B=a(42449),U=a(69292),V=a(91723),O=a(44743),X=a(71488),$=a(32060),q=a(14438),H=a(71837),G=a(93448),K=a(46034),Q=a(74291),J=a(69174),Y=a(68245),ee=a(90641),et=a(37760),ea=a(66901),es=a(92369),el=a(82718);let er=e=>{let{className:t}=e;return(0,s.jsx)("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:(0,s.jsx)("path",{d:"M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z"})})},en=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"w-4 h-4";switch(null==e?void 0:e.toLowerCase()){case"instagram":return(0,s.jsx)(et.Z,{className:"".concat(t," text-pink-500")});case"messenger":case"facebook":return(0,s.jsx)(er,{className:"".concat(t," text-blue-500")});case"whatsapp":return(0,s.jsx)("svg",{className:"".concat(t," text-green-500"),viewBox:"0 0 24 24",fill:"currentColor",children:(0,s.jsx)("path",{d:"M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z"})});default:return(0,s.jsx)(c.Z,{className:"".concat(t," text-gray-500")})}},ei=e=>{switch(null==e?void 0:e.toLowerCase()){case"whatsapp":return"bg-green-100 text-green-700 border-green-200";case"instagram":return"bg-pink-100 text-pink-700 border-pink-200";case"messenger":case"facebook":return"bg-blue-100 text-blue-700 border-blue-200";default:return"bg-gray-100 text-gray-700 border-gray-200"}},eo=e=>{if(!e)return"";let t=new Date(e),a=Math.floor((new Date().getTime()-t.getTime())/36e5);return a<24?t.toLocaleTimeString("en-US",{hour:"numeric",minute:"2-digit"}):a<48?"Yesterday":t.toLocaleDateString("en-US",{month:"short",day:"numeric"})};function ed(e){let{open:t,onOpenChange:a,currentConversation:r,allConversations:n,onMergeComplete:c}=e,[m,u]=(0,l.useState)(new Set([r.id])),[x,p]=(0,l.useState)(""),[h,g]=(0,l.useState)(!1),[b,f]=(0,l.useState)(!1),[_,w]=(0,l.useState)(null);(0,l.useEffect)(()=>{t&&(u(new Set([r.id])),p(""),g(!1),f(!1),w(null))},[t,r.id]);let k=n.filter(e=>{if(!x.trim())return!0;let t=x.toLowerCase();return(e.contact_name||"").toLowerCase().includes(t)||(e.last_message||"").toLowerCase().includes(t)||(e.platform||"").toLowerCase().includes(t)}),N=e=>{let t=new Set(m);e!==r.id&&(t.has(e)?t.delete(e):t.add(e),u(t))},C=async()=>{if(m.size<2){q.A.error("Please select at least one other conversation to merge");return}g(!0);try{let e=Array.from(m).filter(e=>e!==r.id),t=await H.api.post("/conversations/merge",{primary_conversation_id:r.id,conversation_ids_to_merge:e,preserve_messages:!0});if(t.data.success||t.data.primary_id){let s={primary_id:t.data.primary_id||r.id,merged_count:t.data.merged_count||e.length,messages_moved:t.data.messages_moved||0,channels_unified:t.data.channels_unified||[]};w(s),f(!0),q.A.success("Merged ".concat(s.merged_count," conversation(s) successfully")),setTimeout(()=>{a(!1),null==c||c(s)},1500)}}catch(a){var e,t;console.error("Failed to merge conversations:",a),q.A.error((null===(t=a.response)||void 0===t?void 0:null===(e=t.data)||void 0===e?void 0:e.detail)||a.message||"Failed to merge conversations")}finally{g(!1)}};return b&&_?(0,s.jsx)(Q.Vq,{open:t,onOpenChange:a,children:(0,s.jsx)(Q.cZ,{className:"max-w-[95vw] sm:max-w-md max-h-[90vh]",children:(0,s.jsxs)("div",{className:"flex flex-col items-center justify-center py-8",children:[(0,s.jsx)(ea.Z,{className:"w-16 h-16 text-green-500 mb-4"}),(0,s.jsx)("h3",{className:"text-lg font-semibold",children:"Conversations Merged"}),(0,s.jsxs)("p",{className:"text-muted-foreground text-center mt-2",children:[_.merged_count," conversation(s) merged successfully.",_.messages_moved>0&&(0,s.jsxs)("span",{className:"block mt-1",children:[_.messages_moved," messages consolidated."]})]}),_.channels_unified.length>0&&(0,s.jsxs)("div",{className:"flex items-center gap-2 mt-3",children:[(0,s.jsx)("span",{className:"text-sm text-muted-foreground",children:"Channels unified:"}),_.channels_unified.map(e=>(0,s.jsx)(J.C,{variant:"outline",className:ei(e),children:e},e))]})]})})}):(0,s.jsx)(Q.Vq,{open:t,onOpenChange:a,children:(0,s.jsxs)(Q.cZ,{className:"sm:max-w-2xl max-h-[85vh] flex flex-col",children:[(0,s.jsxs)(Q.fK,{children:[(0,s.jsxs)(Q.$N,{className:"flex items-center gap-2",children:[(0,s.jsx)(y.Z,{className:"w-5 h-5 text-primary"}),"Manual Merge Conversations"]}),(0,s.jsx)(Q.Be,{children:"Select conversations to merge with the current chat. All messages will be combined into one thread."})]}),(0,s.jsxs)("div",{className:"p-3 bg-primary/10 rounded-lg border-2 border-primary mb-2",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2 text-sm text-primary font-medium mb-2",children:[(0,s.jsx)(ea.Z,{className:"w-4 h-4"}),"Primary Conversation (Current)"]}),(0,s.jsxs)("div",{className:"flex items-center gap-3",children:[(0,s.jsxs)(d.qE,{className:"w-10 h-10",children:[(0,s.jsx)(d.F$,{src:r.avatar_url}
1),(0,s.jsx)(d.Q5,{children:(0,s.jsx)(es.Z,{className:"w-5 h-5"})})]}),(0,s.jsxs)("div",{className:"flex-1",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("span",{className:"font-medium",children:r.contact_name||"Unknown"}),en(r.platform)]}),(0,s.jsx)("p",{className:"text-sm text-muted-foreground truncate",children:r.last_message||"No messages"})]})]})]}),(0,s.jsxs)("div",{className:"relative",children:[(0,s.jsx)(j.Z,{className:"absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground"}),(0,s.jsx)(o.I,{placeholder:"Search conversations to merge...",value:x,onChange:e=>p(e.target.value),className:"pl-9"})]}),(0,s.jsx)(ee.x,{className:"flex-1 min-h-[300px] max-h-[400px] border rounded-lg",children:(0,s.jsx)("div",{className:"divide-y",children:0===k.length?(0,s.jsxs)("div",{className:"p-8 text-center",children:[(0,s.jsx)(el.Z,{className:"w-12 h-12 mx-auto mb-2 text-muted-foreground opacity-50"}),(0,s.jsx)("p",{className:"text-sm text-muted-foreground",children:x?"No conversations match your search":"No other conversations available"})]}):k.filter(e=>e.id!==r.id).map(e=>{let t=m.has(e.id);return(0,s.jsx)("div",{className:"p-3 cursor-pointer transition-colors ".concat(t?"bg-primary/5":"hover:bg-accent/50"),onClick:()=>N(e.id),children:(0,s.jsxs)("div",{className:"flex items-center gap-3",children:[(0,s.jsx)(Y.X,{checked:t,onCheckedChange:()=>N(e.id),onClick:e=>e.stopPropagation()}),(0,s.jsxs)(d.qE,{className:"w-10 h-10",children:[(0,s.jsx)(d.F$,{src:e.avatar_url}),(0,s.jsx)(d.Q5,{children:(0,s.jsx)(es.Z,{className:"w-5 h-5"})})]}),(0,s.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between mb-1",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("span",{className:"font-medium ".concat(t?"text-primary":""),children:e.contact_name||"Unknown"}),en(e.platform||e.channel||"")]}),(0,s.jsx)("span",{className:"text-xs text-muted-foreground",children:eo(e.last_message_at)})]}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("p",{className:"text-sm text-muted-foreground truncate flex-1",children:e.last_message||"No messages"}),(0,s.jsx)(J.C,{variant:"outline",className:"text-xs ".concat(ei(e.platform||e.channel||"")),children:e.platform||e.channel||"chat"})]})]})]})},e.id)})})}),(0,s.jsxs)("div",{className:"flex items-center justify-between text-sm py-2",children:[(0,s.jsxs)("span",{className:"text-muted-foreground",children:[m.size-1," conversation(s) selected to merge"]}),m.size>1&&(0,s.jsx)(i.z,{variant:"ghost",size:"sm",onClick:()=>u(new Set([r.id])),children:"Clear selection"})]}),(0,s.jsxs)(Q.cN,{className:"flex gap-2 sm:gap-0",children:[(0,s.jsx)(i.z,{variant:"outline",onClick:()=>a(!1),children:"Cancel"}),(0,s.jsx)(i.z,{onClick:C,disabled:h||m.size<2,className:"gap-2",children:h?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(v.Z,{className:"w-4 h-4 animate-spin"}),"Merging..."]}):(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(y.Z,{className:"w-4 h-4"}),"Merge ",m.size-1," Conversation(s)"]})})]})]})})}var ec=a(75060),em=a(15291),eu=a(17168),ex=a(13781),ep=a(60044),eh=a(84881);let eg=e=>{let{className:t}=e;return(0,s.jsx)("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:(0,s.jsx)("path",{d:"M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z"})})},ev=e=>{switch(null==e?void 0:e.toLowerCase()){case"instagram":return(0,s.jsx)(et.Z,{className:"w-4 h-4 text-pink-500"});case"whatsapp":return(0,s.jsx)(c.Z,{className:"w-4 h-4 text-green-500"});case"messenger":case"facebook":return(0,s.jsx)(eg,{className:"w-4 h-4 text-blue-500"});default:return(0,s.jsx)(c.Z,{className:"w-4 h-4 text-gray-500"})}};function eb(e){let{open:t,onOpenChange:a,conversation:r,onLeadCreated:n}=e,{user:d}=(0,eh.a)(),[c,m]=(0,l.useState)(!1),[p,h]=(0,l.useState)(!0),[g,b]=(0,l.useState)(!1),[f,j]=(0,l.useState)(!1),[_,w]=(0,l.useState)(!1),[k,y]=(0,l.useState)([]),[S,A]=(0,l.useState)([]),[Z,E]=(0,l.useState)([]),[F,M]=(0,l.useState)(!1),[z,T]=(0,l.useState)(null),[L,P]=(0,l.useState)(null),[R,D]=(0,l.useState)("lead"),[W,I]=(0,l.useState)(""),[B,U]=(0,l.useState)(""),[V,O]=(0,l.useState)(""),[X,$]=(0,l.useState)(""),[G,K]=(0,l.useState)(""),[ee,el]=(0,l.useState)([]),[er,en]=(0,l.useState)([]),ei=()=>{I(r.contact_name||""),P(null);let e="",t="",a="",s=r.meta_data||{};if(s.phone&&(e=s.phone),s.email&&(t=s.email),s.company&&(a=s.company),!e&&"whatsapp"===r.platform&&(s.wa_id?e=s.wa_id:r.external_platform_id&&/^\d+$/.test(r.external_platform_id)&&(e=r.external_platform_id)),r.raw_data){let s=r.raw_data;!e&&s.phone&&(e=s.phone),!t&&s.email&&(t=s.email),!a&&s.company&&(a=s.company),s.meta_data&&(!e&&s.meta_data.phone&&(e=s.meta_data.phone),!t&&s.meta_data.email&&(t=s.meta_data.email),!e&&s.meta_data.wa_id&&(e=s.meta_data.wa_id))}e&&/^\d{10,}$/.test(e)&&!e.startsWith("+")&&(e="+"+e),U(e),O(t),$(a)},[eo,ed]=(0,l.useState)(!1);(0,l.useEffect)(()=>
1{if(t&&!eo){let e=!!r.lead_id;console.log("Modal opened - isEditing:",e,"lead_id:",r.lead_id),M(!1),T(null),D("lead"),el([]),en([]);let t=localStorage.getItem("workspace_id")||(null==d?void 0:d.workspace_id);console.log("Modal workspace_id:",t,"from localStorage:",localStorage.getItem("workspace_id"),"from user:",null==d?void 0:d.workspace_id),e?(Promise.all([H.api.get("/leads/".concat(r.lead_id)).catch(e=>(console.error("Lead fetch error:",e),{data:null})),H.api.get("/lead-pipelines",{params:{workspace_id:t,include_stages:!0,active_only:!0}}).catch(e=>(console.error("Pipelines fetch error:",e),{data:[]})),H.api.get("/v1/crm/pipelines",{params:{include_stages:!1,include_counts:!1}}).catch(e=>(console.error("CRM fetch error:",e),{data:[]}))]).then(e=>{var t,a,s,l,n;let[i,o,d]=e;console.log("Edit mode - API responses:",{lead:i.data,pipelines:o.data,crm:d.data});let c=(null===(t=o.data)||void 0===t?void 0:t.pipelines)||o.data||[];console.log("Setting leadPipelines:",c.length,"pipelines"),E(c),A((null===(a=d.data)||void 0===a?void 0:a.pipelines)||d.data||[]);let m=(null===(s=i.data)||void 0===s?void 0:s.lead)||i.data;if(console.log("Lead data:",m),m){I(m.name||""),U(m.phone||""),O(m.email||""),$(m.company||""),P(r.lead_id);let e=c.find(e=>e.is_default)||c[0];if(e){en([e.id]);let t=e.stages||[];if(y(t),m.pipeline_stage_id){let e=null,a=null;for(let t of c){let s=(t.stages||[]).find(e=>e.id===m.pipeline_stage_id);if(s){e=s,a=t;break}}if(a&&e)console.log("Found stage in pipeline:",a.name),en([a.id]),y(a.stages||[]),K(m.pipeline_stage_id);else{let e=t.find(e=>e.is_entry_point);K((null==e?void 0:e.id)||(null===(l=t[0])||void 0===l?void 0:l.id)||"")}}else{let e=t.find(e=>e.is_entry_point);K((null==e?void 0:e.id)||(null===(n=t[0])||void 0===n?void 0:n.id)||"")}}}w(!1),h(!1),j(!1),b(!1)}).catch(e=>{console.error("Failed to load edit data:",e),w(!1),h(!1),j(!1),b(!1)}),w(!0),h(!0),j(!0),b(!0)):(ei(),ef(!1),eb(),eg()),ed(!0)}t||ed(!1)},[t]);let eg=async()=>{h(!0);try{var e;let t=localStorage.getItem("workspace_id")||(null==d?void 0:d.workspace_id),a=await H.api.get("/lead-pipelines",{params:{workspace_id:t,include_stages:!0,active_only:!0}}),s=(null===(e=a.data)||void 0===e?void 0:e.pipelines)||a.data||[];E(s);let l=s.find(e=>e.is_default)||s[0];if(l){en([l.id]);let e=l.stages||[];y(e);let t=e.find(e=>e.is_entry_point);t?K(t.id):e.length>0&&K(e[0].id)}}catch(e){console.error("Failed to fetch pipeline stages:",e),q.A.error("Failed to load pipeline stages")}finally{h(!1)}},eb=async()=>{b(!0);try{var e;let t=await H.api.get("/v1/crm/pipelines",{params:{include_stages:!1,include_counts:!1}}),a=(null===(e=t.data)||void 0===e?void 0:e.pipelines)||t.data||[];A(a);let s=a.find(e=>e.is_default);s?el([s.id]):a.length>0&&el([a[0].id])}catch(e){console.error("Failed to fetch CRM pipelines:",e)}finally{b(!1)}},ef=async function(){let e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];j(!0);try{var t;let a=localStorage.getItem("workspace_id")||(null==d?void 0:d.workspace_id),s=await H.api.get("/lead-pipelines",{params:{workspace_id:a,include_stages:!0,active_only:!0}}),l=(null===(t=s.data)||void 0===t?void 0:t.pipelines)||s.data||[];if(E(l),!e){let e=l.find(e=>e.is_default);if(e){console.log("Auto-selecting default pipeline:",e.name,e.id),en([e.id]);let t=e.stages||[];if(t.length>0){y(t);let e=t.find(e=>e.is_entry_point);e?K(e.id):K(t[0].id)}}}return l}catch(e){return console.error("Failed to fetch lead pipelines:",e),null}finally{j(!1)}},ej=e=>{el(t=>t.includes(e)?t.filter(t=>t!==e):[...t,e])},e_=async e=>{var t,s,l,i,o,c,u,x,p,h;if(e&&(e.preventDefault(),e.stopPropagation()),!W.trim()){q.A.error("Name is required");return}if(!B.trim()&&!V.trim()){q.A.error("Please provide at least a phone number or email");return}if("lead"===R&&!G){q.A.error("Please select a pipeline stage");return}if("crm"===R&&0===ee.length){q.A.error("Please select at least one CRM pipeline");return}m(!0);try{let e=L,c="lead"===R?G:void 0,m=null===(t=k.find(e=>e.id===G))||void 0===t?void 0:t.name,u=null===(s=Z.find(e=>er.includes(e.id)))||void 0===s?void 0:s.name;if(console.log("Submitting lead with:",{selectedStageId:G,stageName:m,selectedLeadPipelineIds:er,pipelineName:u,stageToSubmit:c,availableStages:k.map(e=>({id:e.id,name:e.name}))}),L)await H.api.put("/leads/".concat(L),{name:W.trim(),phone:B.trim()||null,email:V.trim()||null,company:X.trim()||null,pipeline_stage_id:c}),q.A.success("Contact updated successfully!");else{let t=er.length>0?er[0]:void 0;console.log("[AddChatToLeadModal] Creating lead with pipeline_id:",t);let a=await H.api.post("/leads/",{name:W.trim(),phone:B.trim()||null,email:V.trim()||null,company:X.trim()||null,source:"socialselling_".concat(r.platform||"chat"),pipeline_stage_id:"lead"===R?G:void 0,pipeline_id:t,meta_data:{source_conversation_id:r.id,source_platform:r.platform}});if(e=(null===(i=a.data)||void 0===i?void 0:null===(l=i.lead)||void 0===l?void 0:l.id)||(null===(o=a.data)||void 0===o?void 0:o.id))try{await H.api.patch("/conversations/".concat(r.id),{lead_id:e})}catch(e){console.warn("Failed to link conversation to lead:",e)}}if(e){if("crm"===R&&ee.length>0&&!L)try{await H.api.post("/v1/crm/pipelines/contacts/add",{lead_id:e,pipeline_ids:ee,notes:"Added from ".concat(r.platform||"chat"," conversation")})}catch(e){console.error("Failed to add to CRM pipeline:",e),q.A.error("Lead created but failed to add to CRM pipeline")}if(er.length>0)try{let t=localStorage.getItem("workspace_id")||(null==d?void 0:d.workspace_id);if(console.log("Adding to lead pipelines:",{leadId:e,selectedLeadPipelineIds:er,workspaceId:t,pipelineNames:Z.filter(e=>er.includes(e.id)).map(e=>e.name)}
1),t){let a=await H.api.post("/lead-pipelines/entries/add",{lead_id:e,pipeline_ids:er,notes:"Added from ".concat(r.platform||"chat"," conversation")},{params:{workspace_id:t}});console.log("Lead pipeline entry response:",a.data)}}catch(e){console.error("Failed to add to lead pipeline:",e),q.A.error("Contact saved but failed to add to lead pipeline")}T(e),M(!0),L||q.A.success("crm"===R?"Added to CRM successfully!":"Lead created successfully!"),setTimeout(()=>{a(!1),null==n||n(e)},1200)}else q.A.success("Contact added successfully!"),setTimeout(()=>{a(!1)},1e3)}catch(e){console.error("Failed to save lead:",e),(null===(c=e.response)||void 0===c?void 0:c.status)===409||(null===(x=e.response)||void 0===x?void 0:null===(u=x.data)||void 0===u?void 0:u.duplicates)?q.A.error("A lead with this phone/email already exists"):q.A.error((null===(h=e.response)||void 0===h?void 0:null===(p=h.data)||void 0===p?void 0:p.detail)||e.message||"Failed to save contact")}finally{m(!1)}};if(F&&z){var ew;return(0,s.jsx)(Q.Vq,{open:t,onOpenChange:a,children:(0,s.jsx)(Q.cZ,{className:"max-w-[95vw] sm:max-w-md max-h-[90vh]",children:(0,s.jsxs)("div",{className:"flex flex-col items-center justify-center py-8",children:[(0,s.jsx)(ea.Z,{className:"w-16 h-16 text-green-500 mb-4"}),(0,s.jsx)("h3",{className:"text-lg font-semibold",children:"crm"===R?"Added to CRM":"Lead Created"}),(0,s.jsxs)("p",{className:"text-muted-foreground text-center mt-2",children:[W," has been added to your ","crm"===R?"CRM pipeline":"pipeline","."]}),"lead"===R&&(0,s.jsx)(J.C,{variant:"outline",className:"mt-3",children:(null===(ew=k.find(e=>e.id===G))||void 0===ew?void 0:ew.name)||"Pipeline"}),"crm"===R&&ee.length>0&&(0,s.jsx)("div",{className:"flex flex-wrap gap-2 mt-3 justify-center",children:ee.map(e=>{let t=S.find(t=>t.id===e);return t?(0,s.jsx)(J.C,{variant:"outline",style:{borderColor:t.color},children:t.name},e):null})})]})})})}return(0,s.jsx)(Q.Vq,{open:t,onOpenChange:a,children:(0,s.jsxs)(Q.cZ,{className:"max-w-[95vw] sm:max-w-lg max-h-[90vh]",children:[(0,s.jsxs)(Q.fK,{children:[(0,s.jsxs)(Q.$N,{className:"flex items-center gap-2",children:[(0,s.jsx)(N.Z,{className:"w-5 h-5 text-primary"}),L?"Edit Contact":"Add Contact"]}),(0,s.jsx)(Q.Be,{children:L?"Update the contact details for this conversation.":"Convert this conversation into a contact and add them to your pipeline."})]}),(0,s.jsxs)("form",{onSubmit:e=>{e.preventDefault(),e.stopPropagation(),e_(e)},children:[(0,s.jsx)("div",{className:"py-4 space-y-4",children:_?(0,s.jsxs)("div",{className:"flex items-center justify-center py-8",children:[(0,s.jsx)(v.Z,{className:"w-6 h-6 animate-spin text-primary"}),(0,s.jsx)("span",{className:"ml-2 text-sm text-muted-foreground",children:"Loading contact details..."})]}):(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("div",{className:"p-3 bg-slate-50 dark:bg-slate-800 rounded-lg space-y-1",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[ev(r.platform||""),(0,s.jsxs)("span",{className:"text-sm text-muted-foreground",children:["From ",r.platform||"chat"," conversation"]})]}),r.instagram_username&&(0,s.jsxs)("div",{className:"flex items-center gap-2 text-sm",children:[(0,s.jsx)(et.Z,{className:"w-3.5 h-3.5 text-pink-500"}),(0,s.jsxs)("span",{className:"text-muted-foreground",children:["@",r.instagram_username]})]}),L?(0,s.jsxs)("div",{className:"text-xs text-blue-600 dark:text-blue-400 mt-1.5 flex items-center gap-1",children:[(0,s.jsx)(ea.Z,{className:"w-3 h-3"}),"Existing contact - details loaded from saved record"]}):W||B||V?(0,s.jsxs)("div",{className:"text-xs text-green-600 dark:text-green-400 mt-1.5 flex items-center gap-1",children:[(0,s.jsx)(ea.Z,{className:"w-3 h-3"}),"Contact info prefilled from conversation"]}):null]}),(0,s.jsx)(eu.Tabs,{value:R,onValueChange:e=>D(e),className:"w-full",children:(0,s.jsxs)(eu.TabsList,{className:"grid w-full grid-cols-2",children:[(0,s.jsxs)(eu.TabsTrigger,{value:"lead",className:"flex items-center gap-2",children:[(0,s.jsx)(C.Z,{className:"w-4 h-4"}),"Lead Pipeline"]}),(0,s.jsxs)(eu.TabsTrigger,{value:"crm",className:"flex items-center gap-2",children:[(0,s.jsx)(ex.Z,{className:"w-4 h-4"}),"CRM Pipeline"]})]})}),(0,s.jsxs)("div",{className:"space-y-4",children:[(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsxs)(ec._,{htmlFor:"name",className:"flex items-center gap-2",children:[(0,s.jsx)(es.Z,{className:"w-4 h-4"}),"Name *"]}),(0,s.jsx)(o.I,{id:"name",placeholder:"Contact name",value:W,onChange:e=>I(e.target.value)})]}),(0,s.jsxs)("div",{className:"grid grid-cols-2 gap-4",children:[(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsxs)(ec._,{htmlFor:"phone",className:"flex items-center gap-2",children:[(0,s.jsx)(x.Z,{className:"w-4 h-4"}),"Phone"]}
1),(0,s.jsx)(o.I,{id:"phone",placeholder:"+1234567890",value:B,onChange:e=>U(e.target.value)})]}),(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsxs)(ec._,{htmlFor:"email",className:"flex items-center gap-2",children:[(0,s.jsx)(u.Z,{className:"w-4 h-4"}),"Email"]}),(0,s.jsx)(o.I,{id:"email",type:"email",placeholder:"[email protected]",value:V,onChange:e=>O(e.target.value)})]})]}),(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsxs)(ec._,{htmlFor:"company",className:"flex items-center gap-2",children:[(0,s.jsx)(ep.Z,{className:"w-4 h-4"}),"Company"]}),(0,s.jsx)(o.I,{id:"company",placeholder:"Company name (optional)",value:X,onChange:e=>$(e.target.value)})]}),"lead"===R&&(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsx)(ec._,{children:"Pipeline Stage *"}),p?(0,s.jsxs)("div",{className:"flex items-center gap-2 text-sm text-muted-foreground py-2",children:[(0,s.jsx)(v.Z,{className:"w-4 h-4 animate-spin"}),"Loading stages..."]}):(0,s.jsxs)(em.Ph,{value:G,onValueChange:K,children:[(0,s.jsx)(em.i4,{children:(0,s.jsx)(em.ki,{placeholder:"Select a pipeline stage"})}),(0,s.jsx)(em.Bw,{children:k.map(e=>(0,s.jsx)(em.Ql,{value:e.id,children:(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("div",{className:"w-3 h-3 rounded-full",style:{backgroundColor:e.color||"#6b7280"}}),e.name,e.is_entry_point&&(0,s.jsx)(J.C,{variant:"secondary",className:"text-xs ml-2",children:"Default"})]})},e.id))})]})]}),"crm"===R&&(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsx)(ec._,{children:"Select CRM Pipeline(s) *"}),g?(0,s.jsxs)("div",{className:"flex items-center gap-2 text-sm text-muted-foreground py-2",children:[(0,s.jsx)(v.Z,{className:"w-4 h-4 animate-spin"}),"Loading pipelines..."]}):0===S.length?(0,s.jsx)("div",{className:"text-sm text-muted-foreground py-2 p-3 bg-slate-50 dark:bg-slate-800 rounded-lg",children:"No CRM pipelines found. Create a pipeline in Settings > CRM first."}):(0,s.jsx)("div",{className:"space-y-2 max-h-48 overflow-y-auto p-2 border rounded-lg",children:S.map(e=>(0,s.jsxs)("div",{className:"flex items-center gap-3 p-2 rounded-lg hover:bg-slate-50 dark:hover:bg-slate-800 cursor-pointer",onClick:()=>ej(e.id),children:[(0,s.jsx)(Y.X,{checked:ee.includes(e.id),onCheckedChange:()=>ej(e.id)}),(0,s.jsx)("div",{className:"w-3 h-3 rounded-full flex-shrink-0",style:{backgroundColor:e.color||"#3B82F6"}}),(0,s.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,s.jsx)("div",{className:"font-medium text-sm truncate",children:e.name}),e.description&&(0,s.jsx)("div",{className:"text-xs text-muted-foreground truncate",children:e.description})]}),e.is_default&&(0,s.jsx)(J.C,{variant:"secondary",className:"text-xs flex-shrink-0",children:"Default"})]},e.id))})]}),Z.length>0&&(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsxs)(ec._,{className:"flex items-center gap-2",children:[(0,s.jsx)(ex.Z,{className:"w-4 h-4"}),"Lead Pipeline"]}),f?(0,s.jsxs)("div",{className:"flex items-center gap-2 text-sm text-muted-foreground py-2",children:[(0,s.jsx)(v.Z,{className:"w-4 h-4 animate-spin"}),"Loading pipelines..."]}):(0,s.jsxs)(em.Ph,{value:er[0]||"",onValueChange:e=>{var t;if(console.log("Pipeline selection changed:",e,"Name:",null===(t=Z.find(t=>t.id===e))||void 0===t?void 0:t.name),en(e?[e]:[]),e){let t=Z.find(t=>t.id===e);if(t){let a=t.stages||[];console.log("Using stages from pipeline:",e,a.map(e=>e.name)),y(a);let s=a.find(e=>e.is_entry_point);s?(console.log("Setting selectedStageId to entry point:",s.name,s.id),K(s.id)):a.length>0&&(console.log("Setting selectedStageId to first stage:",a[0].name,a[0].id),K(a[0].id))}}},children:[(0,s.jsx)(em.i4,{children:(0,s.jsx)(em.ki,{placeholder:"Select a lead pipeline (optional)"})}),(0,s.jsx)(em.Bw,{children:Z.map(e=>(0,s.jsx)(em.Ql,{value:e.id,children:(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("div",{className:"w-3 h-3 rounded-full",style:{backgroundColor:e.color||"#10B981"}}),e.name,e.is_default&&(0,s.jsx)(J.C,{variant:"secondary",className:"text-xs ml-2",children:"Default"})]})},e.id))})]})]})]}),(0,s.jsxs)("p",{className:"text-xs text-muted-foreground",children:["* Either phone or email is required to ",L?"update":"create"," a contact."]})]})}),(0,s.jsxs)(Q.cN,{children:[(0,s.jsx)(i.z,{type:"button",variant:"outline",onClick:()=>a(!1),children:"Cancel"}),(0,s.jsx)(i.z,{type:"submit",disabled:c||_||"lead"===R&&p||"crm"===R&&g,className:"gap-2",children:c?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(v.Z,{className:"w-4 h-4 animate-spin"}),L?"Updating...":"Creating..."]}):(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(N.Z,{className:"w-4 h-4"}),L?"Update Contact":"crm"===R?"Add to CRM":"Add to Pipeline"]})})]})]})]})})}function ef(e){let{onResultSelect:t,onSearchResults:a,placeholder:r="Search conversations semantically...",className:n}=e,[d,m]=(0,l.useState)(""),[u,x]=(0,l.useState)([]),[p,h]=(0,l.useState)(!1),[g,b]=(0,l.useState)(!1),[f,w]=(0,l.useState)(null),N=(0,l.useCallback)(async()=>
1{if(d.trim()&&!(d.length<3)){h(!0),w(null),b(!0);try{let e=await H.api.post("/conversations/indexer/search",{query:d,n_results:10,include_ai_responses:!0});e.data.success&&(x(e.data.results||[]),null==a||a(e.data.results||[]))}catch(e){console.error("Semantic search failed:",e),w("Search failed. Please try again."),x([])}finally{h(!1)}}},[d,a]);return(0,s.jsxs)("div",{className:(0,G.cn)("relative",n),children:[(0,s.jsx)("div",{className:"flex items-center gap-2",children:(0,s.jsxs)("div",{className:"relative flex-1",children:[(0,s.jsx)("div",{className:"absolute left-3 top-1/2 -translate-y-1/2 flex items-center gap-1.5",children:(0,s.jsx)(_.Z,{className:"w-4 h-4 text-purple-500"})}),(0,s.jsx)(o.I,{value:d,onChange:e=>m(e.target.value),onKeyDown:e=>{"Enter"===e.key&&N()},placeholder:r,className:"pl-10 pr-20 h-10 bg-gradient-to-r from-purple-50 to-pink-50 dark:from-purple-900/20 dark:to-pink-900/20 border-purple-200 dark:border-purple-700 focus-visible:ring-purple-500"}),(0,s.jsxs)("div",{className:"absolute right-2 top-1/2 -translate-y-1/2 flex items-center gap-1",children:[d&&(0,s.jsx)(i.z,{variant:"ghost",size:"icon",onClick:()=>{m(""),x([]),b(!1),w(null)},className:"h-6 w-6",children:(0,s.jsx)(k.Z,{className:"w-3 h-3"})}),(0,s.jsx)(i.z,{size:"sm",onClick:N,disabled:!d.trim()||d.length<3||p,className:"h-7 px-2 bg-purple-600 hover:bg-purple-700",children:p?(0,s.jsx)(v.Z,{className:"w-3.5 h-3.5 animate-spin"}):(0,s.jsx)(j.Z,{className:"w-3.5 h-3.5"})})]})]})}),g&&d.length>=3&&(0,s.jsx)("div",{className:"absolute top-full left-0 right-0 mt-2 bg-white dark:bg-slate-900 rounded-lg border shadow-lg z-50 max-h-[400px] overflow-y-auto",children:f?(0,s.jsx)("div",{className:"p-4 text-center text-red-500 text-sm",children:f}):p?(0,s.jsxs)("div",{className:"p-4 flex items-center justify-center gap-2",children:[(0,s.jsx)(v.Z,{className:"w-4 h-4 animate-spin text-purple-500"}),(0,s.jsx)("span",{className:"text-sm text-muted-foreground",children:"Searching semantically..."})]}):0===u.length?(0,s.jsxs)("div",{className:"p-4 text-center text-muted-foreground text-sm",children:['No conversations found matching "',d,'"']}):(0,s.jsxs)("div",{className:"divide-y",children:[(0,s.jsx)("div",{className:"px-3 py-2 bg-slate-50 dark:bg-slate-800",children:(0,s.jsxs)("span",{className:"text-xs text-muted-foreground",children:["Found ",u.length," semantically similar conversations"]})}),u.map((e,a)=>(0,s.jsx)("button",{onClick:()=>{null==t||t(e),b(!1)},className:"w-full text-left p-3 hover:bg-slate-50 dark:hover:bg-slate-800 transition-colors",children:(0,s.jsxs)("div",{className:"flex items-start gap-3",children:[(0,s.jsx)("div",{className:"w-8 h-8 rounded-full bg-gradient-to-r from-purple-100 to-pink-100 dark:from-purple-900/40 dark:to-pink-900/40 flex items-center justify-center flex-shrink-0",children:(0,s.jsx)(c.Z,{className:"w-4 h-4 text-purple-600"})}),(0,s.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,s.jsx)("p",{className:"text-sm text-foreground line-clamp-2",children:e.content}),(0,s.jsxs)("div",{className:"flex items-center gap-2 mt-1.5 flex-wrap",children:[(0,s.jsxs)(J.C,{variant:"secondary",className:"text-[10px] h-5",children:[Math.round(100*e.relevance_score),"% match"]}),e.platform&&(0,s.jsx)(J.C,{variant:"outline",className:"text-[10px] h-5 capitalize",children:e.platform}),e.sentiment&&(0,s.jsx)(J.C,{variant:"outline",className:(0,G.cn)("text-[10px] h-5 capitalize","positive"===e.sentiment&&"border-green-300 text-green-700","negative"===e.sentiment&&"border-red-300 text-red-700"),children:e.sentiment}),e.intent&&(0,s.jsx)(J.C,{variant:"outline",className:"text-[10px] h-5 capitalize",children:e.intent})]})]})]})},a))]})})]})}var ej=a(90740),e_=a(26899),ew=a(14586),ek=a(22135),eN=a(40875),ey=a(76858),eC=a(65302);let eS={all:{label:"All",icon:ej.Z,color:"text-gray-600",bgColor:"bg-gray-50 dark:bg-gray-900/30",borderColor:"border-gray-300"},hot:{label:"Hot",icon:b.Z,color:"text-orange-600",bgColor:"bg-orange-50 dark:bg-orange-900/30",borderColor:"border-orange-300"},warm:{label:"Warm",icon:e_.Z,color:"text-amber-600",bgColor:"bg-
1amber-50 dark:bg-amber-900/30",borderColor:"border-amber-300"},cold:{label:"Cold",icon:ew.Z,color:"text-blue-600",bgColor:"bg-blue-50 dark:bg-blue-900/30",borderColor:"border-blue-300"}};function eA(e){let t=new Date(e),a=new Date().getTime()-t.getTime(),s=Math.floor(a/6e4),l=Math.floor(a/36e5),r=Math.floor(a/864e5);return s<1?"Just now":s<60?"".concat(s,"m ago"):l<24?"".concat(l,"h ago"):1===r?"Yesterday":r<7?"".concat(r,"d ago"):t.toLocaleDateString("en-US",{month:"short",day:"numeric"})}function eZ(e){let{open:t,onOpenChange:a}=e,[r,n]=(0,l.useState)([]),[o,d]=(0,l.useState)([]),[m,p]=(0,l.useState)({hot:0,warm:0,cold:0,all:0}),[h,g]=(0,l.useState)("all"),[b,f]=(0,l.useState)(new Set),[j,_]=(0,l.useState)(null),[w,k]=(0,l.useState)(null),[N,y]=(0,l.useState)("select"),[S,A]=(0,l.useState)(!1),[Z,E]=(0,l.useState)(!1),[F,M]=(0,l.useState)(null),z=(0,l.useCallback)(async e=>{A(!0);try{var t,a;let s=await H.api.get("/inbox/qualified-conversations",{params:{lead_category:"all"===e?null:e,limit:100}});d((null===(t=s.data)||void 0===t?void 0:t.conversations)||[]),p((null===(a=s.data)||void 0===a?void 0:a.category_counts)||{hot:0,warm:0,cold:0,all:0})}catch(e){console.error("Failed to fetch conversations:",e),q.A.error("Failed to load conversations")}finally{A(!1)}},[]),T=(0,l.useCallback)(async()=>{try{var e;let t=await H.api.get("/lead-pipelines",{params:{include_stages:!0,include_counts:!0}});n((null===(e=t.data)||void 0===e?void 0:e.pipelines)||[])}catch(e){console.error("Failed to fetch pipelines:",e)}},[]);(0,l.useEffect)(()=>{t?(z(h),T()):(k(null),f(new Set),_(null),M(null),y("select"),g("all"))},[t]),(0,l.useEffect)(()=>{t&&(z(h),f(new Set))},[h,t,z]);let L=e=>{f(t=>{let a=new Set(t);return a.has(e)?a.delete(e):a.add(e),a})},R=async()=>{var e,t,a,s;if(!w){q.A.error("Please select a pipeline");return}if(0===b.size){q.A.error("No conversations selected");return}E(!0),M(null);try{let a=o.filter(e=>b.has(e.id)&&e.lead_id).map(e=>e.lead_id);if(0===a.length){q.A.error("No leads found in selected conversations"),E(!1);return}let s=await H.api.post("/lead-pipelines/entries/bulk-add",{pipeline_id:w,lead_ids:a,notes:"Synced from inbox conversations (".concat(h," leads)")}),l={added:(null===(e=s.data)||void 0===e?void 0:e.added)||0,skipped:(null===(t=s.data)||void 0===t?void 0:t.skipped)||0};M(l),l.added>0?q.A.success("Added ".concat(l.added," leads to pipeline!")):l.skipped>0&&q.A.info("All ".concat(l.skipped," leads are already in the pipeline"))}catch(e){console.error("Sync failed:",e),q.A.error((null==e?void 0:null===(s=e.response)||void 0===s?void 0:null===(a=s.data)||void 0===a?void 0:a.detail)||"Failed to sync leads")}finally{E(!1)}},D=e=>{if(!e)return null;let t=eS[e],a=t.icon;return(0,s.jsxs)(J.C,{variant:"outline",className:(0,G.cn)("text-xs",t.color,t.bgColor,t.borderColor),children:[(0,s.jsx)(a,{className:"w-3 h-3 mr-1"}),t.label]})};return(0,s.jsx)(Q.Vq,{open:t,onOpenChange:a,children:(0,s.jsxs)(Q.cZ,{className:"sm:max-w-[700px] h-[85vh] flex flex-col overflow-hidden",children:[(0,s.jsxs)(Q.fK,{children:[(0,s.jsxs)(Q.$N,{className:"flex items-center gap-2",children:[(0,s.jsx)(C.Z,{className:"w-5 h-5 text-emerald-500"}),"Sync Conversations to Pipeline"]}),(0,s.jsx)(Q.Be,{children:"Select qualified conversations to add to a leads pipeline."})]}),S&&0===o.length?(0,s.jsxs)("div",{className:"flex items-center justify-center py-8",children:[(0,s.jsx)(v.Z,{className:"w-6 h-6 animate-spin text-emerald-500"}),(0,s.jsx)("span",{className:"ml-2 text-sm text-muted-foreground",children:"Loading conversations..."})]}):(0,s.jsxs)("div",{className:"flex flex-col flex-1 min-h-0 space-y-4",children:[(0,s.jsx)("div",{className:"flex items-center gap-2 flex-wrap",children:["all","hot","warm","cold"].map(e=>{let t=eS[e],a=t.icon,l=m[e],r=h===e;return(0,s.jsxs)(i.z,{variant:r?"default":"outline",size:"sm",onClick:()=>g(e),className:(0,G.cn)("gap-1.5",r&&"hot"===e&&"bg-orange-500 hover:bg-orange-600",r&&"warm"===e&&"bg-amber-500 hover:bg-amber-600",r&&"cold"===e&&"bg-blue-500 hover:bg-blue-600",!r&&t.color),children:[(0,s.jsx)(a,{className:"w-3.5 h-3.5"}),t.label,(0,s.jsx)(J.C,{variant:"secondary",className:"ml-1 px-1.5 py-0 text-xs",children:l})]},e)})}),"select"===N?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)(Y.X,{checked:b.size===o.length&&o.length>0,onCheckedChange:()=>{b.size===o.length?f(new Set):f(new Set(o.map(e=>e.id)))}}),(0,s.jsx)("span",{className:"text-sm text-muted-foreground",children:b.size>0?"".concat(b.size," selected"):"Select all"})]}),S&&(0,s.jsx)(v.Z,{className:"w-4 h-4 animate-spin text-muted-foreground"})]}),(0,s.jsx)(ee.x,{className:"h-[400px] border rounded-lg",children:0===o.length?(0,s.jsxs)("div",{className:"flex flex-col items-center justify-center py-12 text-center",children:[(0,s.jsx)(P.Z,{className:"w-10 h-10 text-muted-foreground mb-2"}),(0,s.jsx)("p",{className:"text-sm text-muted-foreground",children:"No qualified conversations foun
1d."}),(0,s.jsx)("p",{className:"text-xs text-muted-foreground mt-1",children:"Conversations need name + email or name + phone."})]}):(0,s.jsx)("div",{className:"divide-y",children:o.map(e=>{let t=b.has(e.id),a=j===e.id;return(0,s.jsxs)("div",{className:"p-3",children:[(0,s.jsxs)("div",{className:"flex items-start gap-3",children:[(0,s.jsx)(Y.X,{checked:t,onCheckedChange:()=>L(e.id),className:"mt-1"}),(0,s.jsxs)("div",{className:"flex-1 cursor-pointer",onClick:()=>_(a?null:e.id),children:[(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("span",{className:"font-medium text-sm",children:e.contact.name||"Unknown"}),D(e.contact.category),e.contact.score>0&&(0,s.jsxs)("span",{className:"text-xs text-muted-foreground",children:["Score: ",e.contact.score]})]}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("span",{className:"text-xs text-muted-foreground",children:e.last_message_at?eA(e.last_message_at):""}),a?(0,s.jsx)(ek.Z,{className:"w-4 h-4 text-muted-foreground"}):(0,s.jsx)(eN.Z,{className:"w-4 h-4 text-muted-foreground"})]})]}),(0,s.jsxs)("div",{className:"flex items-center gap-3 mt-1 text-xs text-muted-foreground",children:[e.contact.email&&(0,s.jsxs)("span",{className:"flex items-center gap-1",children:[(0,s.jsx)(u.Z,{className:"w-3 h-3"}),e.contact.email]}),e.contact.phone&&(0,s.jsxs)("span",{className:"flex items-center gap-1",children:[(0,s.jsx)(x.Z,{className:"w-3 h-3"}),e.contact.phone]}),(0,s.jsxs)("span",{className:"flex items-center gap-1",children:[(0,s.jsx)(c.Z,{className:"w-3 h-3"}),e.message_count," messages"]}),(0,s.jsx)(J.C,{variant:"outline",className:"text-[10px] capitalize",children:e.channel})]}),!a&&e.messages.length>0&&(0,s.jsx)("p",{className:"text-xs text-muted-foreground mt-2 line-clamp-1",children:e.messages[e.messages.length-1].content})]})]}),a&&e.messages.length>0&&(0,s.jsx)("div",{className:"mt-3 ml-7 p-3 bg-muted/30 rounded-lg space-y-2 max-h-[200px] overflow-y-auto",children:e.messages.map(e=>(0,s.jsxs)("div",{className:(0,G.cn)("p-2 rounded-lg text-xs","contact"===e.sender_type?"bg-white dark:bg-gray-800 border":"bg-emerald-50 dark:bg-emerald-900/20 ml-4"),children:[(0,s.jsxs)("div",{className:"flex items-center justify-between mb-1",children:[(0,s.jsxs)("span",{className:"font-medium flex items-center gap-1",children:[(0,s.jsx)(es.Z,{className:"w-3 h-3"}),e.sender_name||e.sender_type,e.ai_generated&&(0,s.jsx)(J.C,{variant:"outline",className:"text-[9px] ml-1",children:"AI"})]}),(0,s.jsx)("span",{className:"text-muted-foreground text-[10px]",children:e.created_at?eA(e.created_at):""})]}),(0,s.jsx)("p",{className:"whitespace-pre-wrap break-words",children:e.content})]},e.id))})]},e.id)})})}),(0,s.jsxs)("div",{className:"flex justify-between items-center pt-2 border-t",children:[(0,s.jsxs)("span",{className:"text-sm text-muted-foreground",children:[b.size," of ",o.length," selected"]}),(0,s.jsxs)("div",{className:"flex gap-2",children:[(0,s.jsx)(i.z,{variant:"outline",onClick:()=>a(!1),children:"Cancel"}),(0,s.jsxs)(i.z,{onClick:()=>y("pipeline"),disabled:0===b.size,className:"bg-emerald-500 hover:bg-emerald-600",children:["Continue",(0,s.jsx)(ey.Z,{className:"w-4 h-4 ml-2"})]})]})]})]}):(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("div",{className:"flex items-center gap-2 p-3 bg-emerald-50 dark:bg-emerald-900/20 rounded-lg border border-emerald-200 dark:border-emerald-800",children:[(0,s.jsx)(eC.Z,{className:"w-4 h-4 text-emerald-600"}),(0,s.jsxs)("span",{className:"text-sm",children:[(0,s.jsx)("strong",{children:b.size})," conversations selected"]})]}),(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsx)("label",{className:"text-sm font-medium",children:"Select Pipeline"}),(0,s.jsx)(ee.x,{className:"h-[250px] border rounded-lg p-2",children:0===r.length?(0,s.jsx)("div",{className:"flex items-center justify-center py-4 text-sm text-muted-foreground",children:"No pipelines found. Create one first."}):(0,s.jsx)("div",{className:"space-y-2",children:r.map(e=>(0,s.jsx)("button",{onClick:()=>k(e.id),className:(0,G.cn)("w-full p-3 rounded-lg border text-left transition-all",w===e.id?"border-emerald-500 bg-emerald-50 dark:bg-emerald-950/30":"border-gray-200 dark:border-gray-700 hover:border-gray-300 dark:hover:border-gray-600"),children:(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{className:"font-medium text-sm",children:e.name}),e.description&&(0,s.jsx)("div",{className:"text-xs text-muted-foreground mt-0.5",children:e.description})]}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[void 0!==e.entry_count&&(0,s.jsxs)("span",{className:"text-xs text-muted-foreground flex items-center gap-1",children:[(0,s.jsx)(C.Z,{className:"w-3 h-3"}),e.entry_count]}),w===e.id&&(0,s.jsx)(eC.Z,{className:"w-4 h-4 text-emerald-500"})]})]})},e.id))})})]}),F&&(0,s.jsx)("div",{className:"p-3 bg-emerald-50 dark:bg-emerald-950/30 rounded-lg border border-emerald-200 dark:border-emerald-800",children:(0,s.jsxs)("div",{className:"flex items-center gap-2 text-emerald-700 dark:text-emerald-300",children:[(0,s.jsx)(eC.Z,{className:"w-4 h-4"}),(0,s.jsxs)("span",{className:"text-sm font-medium",children:[F.added," leads added, ",F.skipped," already in pipeline"]})]})}),(0,s.jsxs)("div",{className:"flex justify-end gap-2 pt-2 border-t",children:[!F&&(0,s.jsx)(i.z,{variant:"outline",onClick:()=>y("select"),children:"Back"}),(0,s.jsx)(i.z,{variant:"outline",onClick:()=>a(!1),children:F?"Close":"Cancel"}),!F&&(0,s.jsx)(i.z,{onClick:R,disabled:Z||!w||0===r.length,className:"bg-emerald-500 hover:bg-emerald-600",children:Z?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(v.Z,{className:"w-4 h-4 mr-2 animate-spin"}),"Syncing..."]}):(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(eC.Z,{className:"w-4 h-4 mr-2"}),"Sync to Pipeline"]})})]})]})]})]})})}var eE=a(25001),eF=a(73608);let eM=e=>{let{className:t}=e;
1return(0,s.jsx)("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:(0,s.jsx)("path",{d:"M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z"})})},ez=e=>{let{className:t}=e;return(0,s.jsx)("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:(0,s.jsx)("path",{d:"M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zM12 0C8.741 0 8.333.014 7.053.072 2.695.272.273 2.69.073 7.052.014 8.333 0 8.741 0 12c0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98C8.333 23.986 8.741 24 12 24c3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98C15.668.014 15.259 0 12 0zm0 5.838a6.162 6.162 0 100 12.324 6.162 6.162 0 000-12.324zM12 16a4 4 0 110-8 4 4 0 010 8zm6.406-11.845a1.44 1.44 0 100 2.881 1.44 1.44 0 000-2.881z"})})},eT=["bg-rose-500","bg-pink-500","bg-fuchsia-500","bg-purple-500","bg-violet-500","bg-indigo-500","bg-blue-500","bg-sky-500","bg-cyan-500","bg-teal-500","bg-emerald-500","bg-green-500","bg-amber-500","bg-orange-500","bg-red-500"];function eL(e){let t=0;for(let a=0;a<e.length;a++)t=e.charCodeAt(a)+((t<<5)-t);return eT[Math.abs(t)%eT.length]}function eP(e){var t,a;if(!e||"Unknown"===e)return"?";if(e.startsWith("@"))return(null===(t=e[1])||void 0===t?void 0:t.toUpperCase())||"@";let s=e.trim().split(/\s+/);return s.length>=2?(s[0][0]+s[s.length-1][0]).toUpperCase():(null===(a=e[0])||void 0===a?void 0:a.toUpperCase())||"?"}function eR(e,t){return e&&"Unknown"!==e&&"Unknown Contact"!==e?"Facebook User"===e&&(null==t?void 0:t.includes("instagram"))?"Instagram User":(e.startsWith("IG User #")||e.startsWith("User #"),e):"Unknown"}function eD(e){return"instagram_comment"===e||"facebook_comment"===e}function eW(e){switch(e){case"whatsapp":return{bg:"bg-[#e5ddd5] dark:bg-slate-950",headerBg:"bg-[#075e54] dark:bg-[#1f2c33]",headerText:"text-white",headerSub:"text-emerald-200",bubbleSent:"bg-[#dcf8c6] dark:bg-emerald-900 text-slate-900 dark:text-emerald-50",bubbleReceived:"bg-white dark:bg-slate-800 text-slate-900 dark:text-white",accent:"text-emerald-600",inputBg:"bg-[#f0f0f0] dark:bg-slate-900",label:"WhatsApp",dotColor:"bg-green-500"};case"instagram":return{bg:"bg-gray-50 dark:bg-slate-950",headerBg:"bg-gradient-to-r from-purple-600 via-pink-500 to-orange-400",headerText:"text-white",headerSub:"text-pink-100",bubbleSent:"bg-gradient-to-r from-blue-500 to-purple-500 text-white",bubbleReceived:"bg-white dark:bg-slate-800 text-slate-900 dark:text-white border border-slate-200 dark:border-slate-700",accent:"text-pink-500",inputBg:"bg-white dark:bg-slate-900",label:"Instagram DM",dotColor:"bg-pink-500"};case"instagram_comment":return{bg:"bg-gray-50 dark:bg-slate-950",headerBg:"bg-gradient-to-r from-purple-600 via-pink-500 to-orange-400",headerText:"text-white",headerSub:"text-pink-100",bubbleSent:"bg-gradient-to-r from-blue-500 to-purple-500 text-white",bubbleReceived:"bg-white dark:bg-slate-800 text-slate-900 dark:text-white",accent:"text-pink-500",inputBg:"bg-white dark:bg-slate-900",label:"Instagram Comments",dotColor:"bg-pink-500"};case"messenger":return{bg:"bg-white dark:bg-slate-950",headerBg:"bg-white dark:bg-slate-900 border-b border-slate-200 dark:border-slate-800",headerText:"text-slate-900 dark:text-white",headerSub:"text-slate-400",bubbleSent:"bg-blue-500 text-white",bubbleReceived:"bg-slate-100 dark:bg-slate-800 text-slate-900 dark:text-white",accent:"text-blue-500",inputBg:"bg-slate-50 dark:bg-slate-900",label:"Messenger",dotColor:"bg-blue-500"};case"facebook_comment":return{bg:"bg-[#f0f2f5] dark:bg-slate-950",headerBg:"bg-[#1877f2]",headerText:"text-white",headerSub:"text-blue-200",bubbleSent:"bg-[#1877f2] text-white",bubbleReceived:"bg-white dark:bg-slate-800 text-slate-900 dark:text-white",accent:"text-blue-500",inputBg:"bg-white dark:bg-slate-900",label:"Facebook Comments",dotColor:"bg-blue-600"};case"email":return{bg:"bg-amber-50/40 dark:bg-slate-950",headerBg:"bg-white dark:bg-slate-900 border-b border-amber-200 dark:border-slate-800",headerText:"text-slate-900 dark:text-white",headerSub:"text-amber-700 dark:text-amber-300",bubbleSent:"bg-amber-500 text-white",bubbleReceived:"bg-white dark:bg-slate-800 text-slate-900 dark:text-white border border-amber-100 dar
1k:border-slate-700",accent:"text-amber-600",inputBg:"bg-amber-50/60 dark:bg-slate-900",label:"Email",dotColor:"bg-amber-500"};case"call":return{bg:"bg-sky-50/50 dark:bg-slate-950",headerBg:"bg-sky-700 dark:bg-sky-950",headerText:"text-white",headerSub:"text-sky-100",bubbleSent:"bg-sky-600 text-white",bubbleReceived:"bg-white dark:bg-slate-800 text-slate-900 dark:text-white border border-sky-100 dark:border-slate-700",accent:"text-sky-600",inputBg:"bg-sky-50 dark:bg-slate-900",label:"Customer call",dotColor:"bg-sky-500"};case"lead_form":return{bg:"bg-violet-50/40 dark:bg-slate-950",headerBg:"bg-violet-700 dark:bg-violet-950",headerText:"text-white",headerSub:"text-violet-100",bubbleSent:"bg-violet-600 text-white",bubbleReceived:"bg-white dark:bg-slate-800 text-slate-900 dark:text-white border border-violet-100 dark:border-slate-700",accent:"text-violet-600",inputBg:"bg-violet-50 dark:bg-slate-900",label:"Lead form",dotColor:"bg-violet-500"};default:return{bg:"bg-[#f0f2f5] dark:bg-slate-950",headerBg:"bg-white dark:bg-slate-900 border-b border-slate-200 dark:border-slate-800",headerText:"text-slate-900 dark:text-white",headerSub:"text-slate-400",bubbleSent:"bg-emerald-500 text-white",bubbleReceived:"bg-white dark:bg-slate-800 text-slate-900 dark:text-white border border-slate-200 dark:border-slate-700",accent:"text-emerald-500",inputBg:"bg-[#f0f2f5] dark:bg-slate-900",label:"Chat",dotColor:"bg-slate-500"}}}function eI(e){var t,a,n,Q,J,Y,ee,et,ea,es;let{defaultPlatform:el="all",showLeadTools:er=!0,title:en="Inbox",className:ei}=e,[eo,ec]=(0,l.useState)([]),[em,eu]=(0,l.useState)(null),[ex,ep]=(0,l.useState)([]),[eh,eg]=(0,l.useState)(""),[ev,ej]=(0,l.useState)(!0),[e_,ew]=(0,l.useState)(!1),[ek,eN]=(0,l.useState)(!1),[ey,eC]=(0,l.useState)(!1),[eS,eA]=(0,l.useState)(""),[eT,eI]=(0,l.useState)(el),[eB,eU]=(0,l.useState)("all"),[eV,eO]=(0,l.useState)("all"),[eX,e$]=(0,l.useState)([]),[eq,eH]=(0,l.useState)(null),[eG,eK]=(0,l.useState)(null),[eQ,eJ]=(0,l.useState)(null),[eY,e0]=(0,l.useState)(!1),[e1,e2]=(0,l.useState)(!1),[e5,e4]=(0,l.useState)([]),[e3,e8]=(0,l.useState)(!1),[e6,e9]=(0,l.useState)([]),[e7,te]=(0,l.useState)(!1),[tt,ta]=(0,l.useState)(""),[ts,tl]=(0,l.useState)(!1),[tr,tn]=(0,l.useState)(!1),[ti,to]=(0,l.useState)(null),[td,tc]=(0,l.useState)(null),[tm,tu]=(0,l.useState)(""),[tx,tp]=(0,l.useState)(""),[th,tg]=(0,l.useState)(null),[tv,tb]=(0,l.useState)(!1),[tf,tj]=(0,l.useState)(30),[t_,tw]=(0,l.useState)(!1);(0,l.useRef)(null);let{refresh:tk}=function(){let[e,t]=(0,l.useState)(0),[a,s]=(0,l.useState)({}),[r,n]=(0,l.useState)(!0),[i,o]=(0,l.useState)(null),d=(0,l.useCallback)(async()=>{try{o(null);let e=(await H.api.get("/inbox/unread-count")).data;t(e.total_unread||0),s(e.by_platform||{})}catch(t){var e;(null==t?void 0:null===(e=t.response)||void 0===e?void 0:e.status)!==401&&(console.error("Failed to fetch unread count:",t),o(t.message||"Failed to fetch unread count"))}finally{n(!1)}},[]);return(0,l.useEffect)(()=>{d()},[d]),(0,l.useEffect)(()=>{let e=setInterval(d,3e4);return()=>clearInterval(e)},[d]),(0,l.useEffect)(()=>{let e=e=>{let a=e.detail;if("contact"===a.sender_type){t(e=>e+1);let e=a.platform||a.channel||"unknown";s(t=>({...t,[e]:(t[e]||0)+1}))}},a=e=>{var a;let s=(null===(a=e.detail.message_ids)||void 0===a?void 0:a.length)||0;s>0&&(t(e=>Math.max(0,e-s)),d())};return window.addEventListener("ws:new_message",e),window.addEventListener("ws:messages_read",a),()=>{window.removeEventListener("ws:new_message",e),window.removeEventListener("ws:messages_read",a)}},[d]),{totalUnread:e,byPlatform:a,loading:r,error:i,refresh:d}}(),{oauthStatus:tN,connectedCount:ty,accounts:tC,loading:tS}=(0,eF.J)(),[tA,tZ]=(0,l.useState)(!1);(0,l.useEffect)(()=>{let e=localStorage.getItem("workspace_id");e&&H.api.get("/channel-credentials/whatsapp/status?workspace_id=".concat(e)).then(e=>{var t,a;tZ((null===(t=e.data)||void 0===t?void 0:t.configured)&&(null===(a=e.data)||void 0===a?void 0:a.verified))}).catch(()=>{})},[]);let tE=(0,l.useRef)(null),tF=(0,l.useRef)(null),tM=(0,l.useRef)(null),tz=(0,l.useRef)(null),tT=(0,l.useRef)(null),tL=(0,l.useRef)(!1),{connected:tP,startTyping:tR,stopTyping:tD}=(0,K.s)(),tW=(0,r.useSearchParams)().get("conversation"),tI=(0,l.useRef)(!1),tB=(0,l.useRef)(el),tU=(0,l.useRef)(!1),tV=(0,l.useCallback)(async function(){let e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];try{e?tw(!0):ej(!0),eH(null);let t=new URLSearchParams({limit:String(30)});"all"!==eT&&t.set("platform",eT),e&&t.set("limit","200");let a=await H.api.get("/conversations?".concat(t.toString()));Array.isArray(a.data)?e?(ec(a.data),tj(e=>Math.min(e+30,a.data.length))):(ec(a.data),tj(30)):ec([])}catch(e){eH(e.message||"Failed to load conversations"),ec([])}finally{ej(!1),tw(!1)}},[eT]),tO=(0,l.useCallback)(async e=>{try{var t;ew(!0);let a=(await H.api.get("/conversations/".concat(e,"/messages"))).data,s=[...Array.isArray(a)?a:null!==(t=null==a?void 0:a.messages)&&void 0!==t?t:[]].sort((e,t)=>
1{let a=e.created_at?new Date(e.created_at).getTime():0,s=t.created_at?new Date(t.created_at).getTime():0;return a-s});ep(s)}catch(e){console.error("Error fetching messages:",e),q.A.error("Failed to load messages"),ep([])}finally{ew(!1)}},[]),tX=(0,l.useCallback)(async()=>{try{tl(!0),q.A.info("Syncing comments from all posts...");let e=await H.api.post("/comments/sync",{days_back:30});if(e.data.success)q.A.success("Synced ".concat(e.data.comments_synced," comments from ").concat(e.data.posts_processed," posts")),tV();else{let t=e.data.errors||[];t.length>0?q.A.warning("Sync completed with errors: ".concat(t[0])):q.A.success("Comment sync completed"),tV()}}catch(a){var e,t;console.error("Error syncing comments:",a),q.A.error((null===(t=a.response)||void 0===t?void 0:null===(e=t.data)||void 0===e?void 0:e.detail)||"Failed to sync comments")}finally{tl(!1)}},[tV]),[t$,tq]=(0,l.useState)(!1),tH=(0,l.useCallback)(async()=>{var e,t,a,s;let l=(null==em?void 0:em.phone)||(null==em?void 0:em.contact_phone)||(null==em?void 0:null===(e=em.meta_data)||void 0===e?void 0:e.phone)||(null==em?void 0:null===(t=em.meta_data)||void 0===t?void 0:t.wa_id);if(!l){q.A.error("No phone number on this conversation");return}let r=localStorage.getItem("workspace_id");if(!r){q.A.error("No workspace selected");return}if(window.confirm("Block ".concat(l," on WhatsApp? They won't be able to message this number.")))try{tq(!0),await H.api.post("/whatsapp/block",{workspace_id:r,numbers:[l]}),q.A.success("Blocked ".concat(l))}catch(e){q.A.error((null===(s=e.response)||void 0===s?void 0:null===(a=s.data)||void 0===a?void 0:a.detail)||"Failed to block")}finally{tq(!1)}},[em]),[tG,tK]=(0,l.useState)(!1),tQ=(0,l.useCallback)(async()=>{var e,t,a,s;let l=(null==em?void 0:em.phone)||(null==em?void 0:em.contact_phone)||(null==em?void 0:null===(e=em.meta_data)||void 0===e?void 0:e.phone)||(null==em?void 0:null===(t=em.meta_data)||void 0===t?void 0:t.wa_id);if(!l){q.A.error("No phone number on this conversation");return}let r=localStorage.getItem("workspace_id");if(!r){q.A.error("No workspace selected");return}if(window.confirm("Unblock ".concat(l," on WhatsApp? They'll be able to message this number again.")))try{tK(!0),await H.api.post("/whatsapp/unblock",{workspace_id:r,numbers:[l]}),q.A.success("Unblocked ".concat(l))}catch(e){q.A.error((null===(s=e.response)||void 0===s?void 0:null===(a=s.data)||void 0===a?void 0:a.detail)||"Failed to unblock")}finally{tK(!1)}},[em]);(0,l.useEffect)(()=>{let e=tB.current!==eT;(!tI.current||e)&&(tI.current=!0,tB.current=eT,tj(30),tV());let t=setInterval(()=>{if(e1)return;let e=new URLSearchParams({limit:String(30)});"all"!==eT&&e.set("platform",eT),H.api.get("/conversations?".concat(e.toString())).then(e=>{Array.isArray(e.data)&&ec(t=>{var a,s;return t.length!==e.data.length||(null===(a=t[0])||void 0===a?void 0:a.last_message_at)!==(null===(s=e.data[0])||void 0===s?void 0:s.last_message_at)?e.data:t.reduce((e,t)=>e+(t.unread_count||0),0)!==e.data.reduce((e,t)=>e+(t.unread_count||0),0)?e.data:t})}).catch(()=>{})},5e3);return()=>clearInterval(t)},[eT,e1]),(0,l.useEffect)(()=>{if(tW&&eo.length>0&&!tU.current){let e=eo.find(e=>e.id===tW);e?(eu(e),tU.current=!0):H.api.get("/conversations/".concat(tW)).then(e=>{e.data&&(eu(e.data),tU.current=!0)}).catch(e=>{console.error("Failed to fetch conversation from URL:",e)})}},[tW,eo]),(0,l.useEffect)(()=>{eY&&H.api.get("/conversations?limit=200").then(e=>{Array.isArray(e.data)&&e4(e.data)}).catch(e=>{console.error("Failed to fetch all conversations for merge:",e)})},[eY]),(0,l.useEffect)(()=>{(async()=>{try{var e;let t=await H.api.get("/social/connected-assets"),a=((null===(e=t.data)||void 0===e?void 0:e.accounts)||[]).map(e=>({id:e.id||e.accountId,name:e.name||e.accountName||e.username,platform:e.platform}));e$(a)}catch(e){console.error("Failed to fetch connected pages:",e)}})()},[]);let tJ=(0,l.useCallback)(async e=>{try{await H.api.post("/conversations/".concat(e,"/read")),ec(t=>t.map(t=>t.id===e?{...t,is_unread:!1,unread_count:0}:t)),tk()}catch(e){console.error("Failed to mark messages as read:",e)}},[tk]),tY=(0,l.useRef)(null);(0,l.useEffect)(()=>
1{if(em){if(tY.current!==em.id){var e;tY.current=em.id,tO(em.id),eg(""),eC(!1),tc(null),tu(""),(em.is_unread||(null!==(e=em.unread_count)&&void 0!==e?e:0)>0)&&tJ(em.id)}let t=setInterval(()=>{e1||H.api.get("/conversations/".concat(em.id,"/messages")).then(e=>{if(Array.isArray(e.data)){let t=[...e.data].sort((e,t)=>(e.created_at?new Date(e.created_at).getTime():0)-(t.created_at?new Date(t.created_at).getTime():0));ep(e=>e.length!==t.length?t:e)}}).catch(()=>{})},2e3);return()=>clearInterval(t)}tY.current=null},[null==em?void 0:em.id,e1]);let t0=(0,l.useRef)(0),t1=(0,l.useRef)(null);(0,l.useEffect)(()=>{let e=t1.current!==(null==em?void 0:em.id),t=ex.length>t0.current&&t0.current>0;if(t0.current=ex.length,t1.current=(null==em?void 0:em.id)||null,e||t){var a;null===(a=tE.current)||void 0===a||a.scrollIntoView({behavior:e?"auto":"smooth"})}},[ex,null==em?void 0:em.id]),(0,l.useEffect)(()=>{if(em){var e;null===(e=tF.current)||void 0===e||e.focus()}},[em]),(0,l.useEffect)(()=>{let e=e=>{let t=e.detail;if(em&&t.conversation_id===em.id){if("agent"===t.user_type||"agent"===t.sender_type)return;t.is_typing?(te(!0),ta(t.user_name||eR(em.contact_name,em.platform)||"Contact")):(te(!1),ta(""))}};return window.addEventListener("ws:typing_indicator",e),()=>{window.removeEventListener("ws:typing_indicator",e)}},[em]),(0,l.useEffect)(()=>{te(!1),ta("")},[null==em?void 0:em.id]),(0,l.useEffect)(()=>{let e=e=>{var t,a,s;let l=e.detail,r=l.content||(null===(t=l.message)||void 0===t?void 0:t.content)||"",n=l.timestamp||new Date().toISOString(),i=l.sender_type||(null===(a=l.message)||void 0===a?void 0:a.sender_type)||"contact";if(em&&l.conversation_id===em.id){let e={id:l.message_id||(null===(s=l.message)||void 0===s?void 0:s.id)||Date.now().toString(),conversation_id:l.conversation_id,content:r,sender_id:l.sender_id||"contact",sender_name:l.sender_name||eR(em.contact_name,em.platform)||"Contact",sender_type:i,created_at:n,message_type:l.message_type||"text",is_delivered:!0,is_read:!1};ep(t=>t.some(t=>t.id===e.id)?t:[...t,e]),te(!1);let t=l.conversation_id,a=e=>setTimeout(()=>{H.api.get("/conversations/".concat(t,"/messages")).then(e=>{if(Array.isArray(e.data)){let t=[...e.data].sort((e,t)=>new Date(e.created_at||0).getTime()-new Date(t.created_at||0).getTime());ep(e=>e.length!==t.length?t:e)}}).catch(()=>{})},e);a(3e3),a(6e3),a(1e4)}ec(e=>e.map(e=>{if(e.id===l.conversation_id){let t="contact"===i,a=(null==em?void 0:em.id)===l.conversation_id;return{...e,last_message:r,last_message_at:n,unread_count:t&&!a?(e.unread_count||0)+1:e.unread_count,is_unread:!!t&&!a||e.is_unread}}return e})),setTimeout(()=>tV(),500)};return window.addEventListener("ws:new_message",e),()=>{window.removeEventListener("ws:new_message",e)}},[em,tV]),(0,l.useEffect)(()=>()=>{tT.current&&clearTimeout(tT.current)},[]);let t2=(e,t)=>{var a;let s=null===(a=e.target.files)||void 0===a?void 0:a[0];if(s){if(s.size>10485760){q.A.error("File size must be less than 10MB");return}if("image"===t&&!s.type.startsWith("image/")){q.A.error("Please select an image file");return}if(eK(s),s.type.startsWith("image/")){let e=new FileReader;e.onloadend=()=>eJ(e.result),e.readAsDataURL(s)}else eJ(null);q.A.success("File attached: ".concat(s.name))}},t5=()=>{eK(null),eJ(null),tM.current&&(tM.current.value=""),tz.current&&(tz.current.value="")},t4=async()=>{var e,t,a,s;if((eh.trim()||eG)&&em){eN(!0);try{let a=eh,s="text";if(eG){let e=new FormData;e.append("file",eG),e.append("conversation_id",em.id);try{let l=await H.api.post("/conversations/upload",e,{headers:{"Content-Type":"multipart/form-data"}});(null===(t=l.data)||void 0===t?void 0:t.file_url)&&(a=l.data.file_url,s=eG.type.startsWith("image/")?"image":"file")}catch(e){if(console.error("File upload failed:",e),!eh.trim()){q.A.error("Failed to upload file"),eN(!1);return}q.A.error("File upload failed, sending text only")}}let l={content:a||eh,message_type:s};"facebook_comment"===em.platform&&ey&&(l.reply_via="messenger"),td&&(l.comment_id=td,tc(null),tu(""));let r=await H.api.post("/conversations/".concat(em.id,"/messages"),l),n={id:(null===(e=r.data)||void 0===e?void 0:e.message_id)||Date.now().toString(),conversation_id:em.id,content:a||eh,sender_id:"me",sender_name:"You",sender_type:"agent",created_at:new Date().toISOString(),message_type:s,is_delivered:!0,is_read:!1};ep(e=>[...e,n]),eg(""),t5(),ec(e=>e.map(e=>e.id===em.id?{...e,last_message:a||eh,last_message_at:new Date().toISOString()}:e)),eu(e=>e?{...e,last_message:a||eh,last_message_at:new Date().toISOString()}:null),"facebook_comment"===em.platform&&ey?(q.A.success("Private message sent via Messenger!"),eC(!1),tc(null),tu("")):q.A.
1success("Message sent!"),setTimeout(()=>tO(em.id),1500)}catch(e){q.A.error((null===(s=e.response)||void 0===s?void 0:null===(a=s.data)||void 0===a?void 0:a.detail)||e.message||"Failed to send message")}finally{eN(!1)}}},t3=async function(e){var t,a,s;let l=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(em){tg(e);try{let a=await H.api.delete("/conversations/".concat(em.id,"/messages/").concat(e,"?delete_for_everyone=").concat(l));(null===(t=a.data)||void 0===t?void 0:t.success)&&(ep(t=>t.map(t=>t.id===e?{...t,is_deleted:!0,content:"This message was deleted",meta_data:{...t.meta_data,deleted:!0}}:t)),l&&a.data.deleted_from_platform?q.A.success("Message deleted for everyone"):l&&a.data.platform_error?q.A.warning("Deleted locally. ".concat(a.data.platform_error)):q.A.success("Message deleted"))}catch(e){q.A.error((null===(s=e.response)||void 0===s?void 0:null===(a=s.data)||void 0===a?void 0:a.detail)||"Failed to delete message")}finally{tg(null)}}},t8=e=>{to(e.id),tp(e.content)},t6=()=>{to(null),tp("")},t9=async()=>{var e,t,a;if(em&&ti&&tx.trim())try{let t=await H.api.put("/conversations/".concat(em.id,"/messages/").concat(ti),{content:tx.trim()});(null===(e=t.data)||void 0===e?void 0:e.success)&&(ep(e=>e.map(e=>e.id===ti?{...e,content:tx.trim(),is_edited:!0,meta_data:{...e.meta_data,is_edited:!0}}:e)),q.A.success("Message edited"),t6())}catch(e){q.A.error((null===(a=e.response)||void 0===a?void 0:null===(t=a.data)||void 0===t?void 0:t.detail)||"Failed to edit message")}},t7=e=>{if(!e)return"";let t=new Date(e),a=Math.floor((new Date().getTime()-t.getTime())/36e5);return a<24?t.toLocaleTimeString("en-US",{hour:"numeric",minute:"2-digit",hour12:!0}):a<48?"Yesterday":t.toLocaleDateString("en-US",{month:"short",day:"numeric"})},ae=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"w-4 h-4";switch(e){case"instagram":case"instagram_comment":return(0,s.jsx)(ez,{className:(0,G.cn)(t,"text-pink-500")});case"messenger":case"facebook_comment":return(0,s.jsx)(eM,{className:(0,G.cn)(t,"text-blue-500")});case"whatsapp":case"whatsapp_reply":return(0,s.jsx)(c.Z,{className:(0,G.cn)(t,"text-green-500")});case"widget":case"web_chat":return(0,s.jsx)(m.Z,{className:(0,G.cn)(t,"text-violet-500")});case"email":case"email_reply":return(0,s.jsx)(u.Z,{className:(0,G.cn)(t,"text-amber-500")});case"call":return(0,s.jsx)(x.Z,{className:(0,G.cn)(t,"text-sky-500")});case"lead_form":return(0,s.jsx)(p.Z,{className:(0,G.cn)(t,"text-violet-500")});default:return(0,s.jsx)(c.Z,{className:(0,G.cn)(t,"text-gray-500")})}},at=e=>{switch(e){case"instagram":return"Instagram";case"instagram_comment":return"Instagram Comment";case"messenger":return"Messenger";case"facebook_comment":return"Facebook Comment";case"whatsapp":return"WhatsApp Business";case"widget":return"Website Chat";case"whatsapp_reply":return"WhatsApp Reply";case"email":return"Email";case"email_reply":return"Email Reply";case"call":return"Customer Call";case"lead_form":return"Lead Form";default:return e}},aa=eo.filter(e=>{if("campaign"===eB&&!e.is_campaign_response||"direct"===eB&&e.is_campaign_response||"all"!==eV&&e.page_name!==eV)return!1;if(!eS.trim())return!0;let t=eS.toLowerCase();return(e.contact_name||"").toLowerCase().includes(t)||(e.last_message||"").toLowerCase().includes(t)}).sort((e,t)=>{let a=e.last_message_at?new Date(e.last_message_at).getTime():0;return(t.last_message_at?new Date(t.last_message_at).getTime():0)-a}),as=(0,l.useMemo)(()=>{let e=new Set;return eX.forEach(t=>{t.name&&e.add(t.name)}),eo.forEach(t=>{t.page_name&&e.add(t.page_name)}),["all",...Array.from(e).sort()]},[eX,eo]),al=[{id:"all",label:"All",icon:null},{id:"instagram",label:"IG",icon:ez},{id:"messenger",label:"FB",icon:eM},{id:"whatsapp",label:"WA",icon:c.Z},{id:"widget",label:"Website",icon:m.Z},{id:"email",label:"Email",icon:u.Z},{id:"call",label:"Calls",icon:x.Z},{id:"lead_form",label:"Forms",icon:p.Z},{id:"comments",label:"Comments",icon:null},{id:"mentions",label:"Mentions",icon:null}];return(0,s.jsxs)(X.pn,{children:[(0,s.jsxs)("div",{className:"h-full flex overflow-hidden min-h-0",children:[(0,s.jsxs)("div",{"data-tour":"inbox-conversations",className:(0,G.cn)("w-full sm:w-[360
1px] lg:w-[400px] border-r border-slate-200 dark:border-slate-800 flex flex-col min-h-0 flex-shrink-0 bg-white dark:bg-slate-900",em&&"hidden sm:flex"),children:[(0,s.jsxs)("div",{className:"border-b border-slate-200 dark:border-slate-800",children:[(0,s.jsxs)("div",{className:"px-4 pt-3 pb-2 flex items-center justify-between",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2.5",children:[(0,s.jsx)("div",{className:"w-8 h-8 rounded-lg bg-emerald-500 flex items-center justify-center",children:(0,s.jsx)(c.Z,{className:"w-4 h-4 text-white"})}),(0,s.jsx)("h1",{className:"text-[15px] font-semibold text-slate-900 dark:text-white",children:en})]}),(0,s.jsxs)("div",{className:"flex items-center",children:[(0,s.jsx)(i.z,{variant:"ghost",size:"icon",onClick:()=>tV(),disabled:ev,className:"h-8 w-8 text-slate-400 hover:text-slate-600",children:(0,s.jsx)(h.Z,{className:(0,G.cn)("w-4 h-4",ev&&"animate-spin")})}),(0,s.jsxs)($.h_,{children:[(0,s.jsx)($.$F,{asChild:!0,children:(0,s.jsx)(i.z,{variant:"ghost",size:"icon",className:"h-8 w-8 text-slate-400 hover:text-slate-600",children:(0,s.jsx)(g.Z,{className:"w-4 h-4"})})}),(0,s.jsxs)($.AW,{align:"end",children:[(0,s.jsxs)($.Xi,{onClick:tX,disabled:ts,children:[ts?(0,s.jsx)(v.Z,{className:"w-4 h-4 mr-2 animate-spin"}):(0,s.jsx)(c.Z,{className:"w-4 h-4 mr-2"}),"Sync Comments"]}),er&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)($.VD,{}),(0,s.jsxs)($.Xi,{onClick:()=>tn(!0),children:[(0,s.jsx)(b.Z,{className:"w-4 h-4 mr-2 text-orange-500"})," Sync to Pipeline"]})]}),((null==em?void 0:em.phone)||(null==em?void 0:em.contact_phone)||(null==em?void 0:null===(t=em.meta_data)||void 0===t?void 0:t.phone))&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)($.VD,{}),(0,s.jsxs)($.Xi,{onClick:tH,disabled:t$,className:"text-red-600 focus:text-red-600",children:[t$?(0,s.jsx)(v.Z,{className:"w-4 h-4 mr-2 animate-spin"}):(0,s.jsx)(f.Z,{className:"w-4 h-4 mr-2"}),"Block user"]})]})]})]})]})]}),(0,s.jsx)("div",{className:"px-3 pb-2",children:(0,s.jsxs)("div",{className:"relative",children:[(0,s.jsx)(j.Z,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate-400"}),e3?(0,s.jsx)(ef,{placeholder:"AI search...",onSearchResults:e=>e9(e),onResultSelect:e=>{let t=eo.find(t=>{var a,s,l;return t.id===(null===(a=e.metadata)||void 0===a?void 0:a.conversation_id)||(null===(l=t.last_message)||void 0===l?void 0:l.includes(null===(s=e.content)||void 0===s?void 0:s.substring(0,50)))});t&&eu(t)}}):(0,s.jsx)(o.I,{placeholder:"Search conversations...",value:eS,onChange:e=>eA(e.target.value),className:"pl-9 h-9 text-sm bg-slate-100 dark:bg-slate-800 border-0 rounded-lg focus-visible:ring-1 focus-visible:ring-emerald-500"}),(0,s.jsx)("button",{onClick:()=>e8(!e3),className:(0,G.cn)("absolute right-2.5 top-1/2 -translate-y-1/2 p-1 rounded transition-colors",e3?"text-purple-500 bg-purple-100 dark:bg-purple-900/30":"text-slate-400 hover:text-slate-600"),children:(0,s.jsx)(_.Z,{className:"w-3.5 h-3.5"})})]})}),(0,s.jsxs)("button",{onClick:()=>tb(!0),className:"mx-3 mb-1.5 flex items-center gap-2 px-2.5 py-1.5 rounded-lg bg-slate-50 dark:bg-slate-800/50 hover:bg-slate-100 dark:hover:bg-slate-800 border border-slate-200 dark:border-slate-700/50 transition-colors group",children:[(0,s.jsx)("div",{className:"flex items-center gap-1",children:[{id:"facebook",color:"bg-blue-500",label:"FB"},{id:"instagram",color:"bg-pink-500",label:"IG"},{id:"whatsapp",color:"bg-green-500",label:"WA"}].map(e=>{var t,a;let l="whatsapp"===e.id?tA||(null===(t=tN.whatsapp)||void 0===t?void 0:t.isConnected)||tC.some(e=>"whatsapp"===e.platform):(null===(a=tN[e.id])||void 0===a?void 0:a.isConnected)||tC.some(t=>t.platform===e.id);return(0,s.jsx)("div",{className:(0,G.cn)("w-2 h-2 rounded-full transition-colors",l?e.color:"bg-slate-300 dark:bg-slate-600"),title:"".concat(e.label,": ").concat(l?"Connected":"Not connected")},e.id)})}),(0,s.jsx)("span",{className:"text-[11px] text-slate-500 dark:text-slate-400 group-hover:text-slate-700 dark:group-hover:text-slate-300",children:ty>0?"".concat(ty," channel").concat(ty>1?"s":""," active"):"No channels connected"}),(0,s.jsx)(w.Z,{className:"w-3 h-3 text-slate-400 group-hover:text-slate-600 dar
1k:group-hover:text-slate-300 ml-auto"})]}),(0,s.jsxs)("div",{className:"px-3 pb-2 flex items-center gap-1 overflow-x-auto no-scrollbar",children:[al.map(e=>(0,s.jsxs)("button",{onClick:()=>eI(e.id),className:(0,G.cn)("flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium transition-all whitespace-nowrap border",eT===e.id?"bg-emerald-500 text-white border-emerald-500 shadow-sm":"bg-white dark:bg-slate-800 text-slate-500 border-slate-200 dark:border-slate-700 hover:border-slate-300 hover:text-slate-700"),children:[e.icon&&(0,s.jsx)(e.icon,{className:"w-3.5 h-3.5"}),e.label]},e.id)),as.length>2&&(0,s.jsx)("select",{value:eV,onChange:e=>eO(e.target.value),className:"px-2 py-1.5 text-xs rounded-full border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-800 text-slate-500 focus:outline-none",children:as.map(e=>(0,s.jsx)("option",{value:e,children:"all"===e?"All Pages":e},e))})]})]}),(0,s.jsx)("div",{className:"flex-1 overflow-y-auto",children:ev&&0===eo.length?(0,s.jsx)("div",{className:"flex items-center justify-center h-32",children:(0,s.jsx)(v.Z,{className:"w-5 h-5 animate-spin text-slate-400"})}):eq?(0,s.jsxs)("div",{className:"p-6 text-center",children:[(0,s.jsx)("p",{className:"text-sm text-slate-500 mb-2",children:eq}),(0,s.jsx)(i.z,{variant:"outline",size:"sm",onClick:()=>tV(),className:"text-xs",children:"Try Again"})]}):0===aa.length?(0,s.jsxs)("div",{className:"p-6 text-center space-y-3",children:[(0,s.jsx)("div",{className:"w-12 h-12 rounded-full bg-slate-100 dark:bg-slate-800 flex items-center justify-center mx-auto",children:(0,s.jsx)(c.Z,{className:"w-5 h-5 text-slate-400"})}),(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-sm font-medium text-slate-600 dark:text-slate-400",children:0===ty?"Connect a channel to start":"No conversations yet"}),(0,s.jsx)("p",{className:"text-xs text-slate-400 dark:text-slate-500 mt-1",children:0===ty?"Link your Facebook, Instagram or WhatsApp to receive messages here":"Messages will appear here when customers reach out"})]}),0===ty&&(0,s.jsxs)(i.z,{size:"sm",className:"text-xs",onClick:()=>tb(!0),children:[(0,s.jsx)(w.Z,{className:"w-3.5 h-3.5 mr-1.5"}),"Connect Channel"]})]}):(0,s.jsxs)("div",{children:[aa.slice(0,tf).map(e=>{var t,a,l,r,n,i,o,c,m;return(0,s.jsxs)("button",{onClick:()=>eu(e),className:(0,G.cn)("w-full px-4 py-3 flex items-center gap-3 text-left transition-colors",(null==em?void 0:em.id)===e.id?(0,G.cn)("border-b border-slate-100 dark:border-slate-800/50","whatsapp"===e.platform?"bg-emerald-50 dark:bg-emerald-900/20 border-l-2 border-l-green-500":"instagram"===e.platform||"instagram_comment"===e.platform?"bg-pink-50 dark:bg-pink-900/20 border-l-2 border-l-pink-500":"messenger"===e.platform||"facebook_comment"===e.platform?"bg-blue-50 dark:bg-blue-900/20 border-l-2 border-l-blue-500":"call"===e.platform?"bg-sky-50 dark:bg-sky-900/20 border-l-2 border-l-sky-500":"lead_form"===e.platform?"bg-violet-50 dark:bg-violet-900/20 border-l-2 border-l-violet-500":"bg-slate-100 dark:bg-slate-800/50 border-l-2 border-l-emerald-500"):(0,G.cn)("hover:bg-slate-50 dark:hover:bg-slate-800/30 border-b border-slate-100 dark:border-slate-800/50 border-l-2","whatsapp"===e.platform?"border-l-green-300 dark:border-l-green-800":"instagram"===e.platform||"instagram_comment"===e.platform?"border-l-pink-300 dark:border-l-pink-800":"messenger"===e.platform||"facebook_comment"===e.platform?"border-l-blue-300 dark:border-l-blue-800":"telegram"===e.platform?"border-l-sky-300 dark:border-l-sky-800":"email"===e.platform?"border-l-amber-300 dark:border-l-amber-800":"call"===e.platform?"border-l-sky-300 dark:border-l-sky-800":"lead_form"===e.platform?"border-l-violet-300 dark:border-l-violet-800":"border-l-slate-200 dark:border-l-slate-700")),children:[(0,s.jsxs)("div",{className:"relative flex-shrink-0",children:[(0,s.jsxs)(d.qE,{className:"w-11 h-11",children:[e.avatar_url&&(0,s.jsx)(d.F$,{src:e.avatar_url}),(0,s.jsx)(d.Q5,{className:(0,G.cn)("text-white text-sm font-bold",eL(e.contact_name||"U")),children:eP(e.contact_name||"")})]}),(0,s.jsx)("div",{className:(0,G.cn)("absolute -bottom-0.5 -right-0.5 w-[18px] h-[18px] rounded-full border-2 border-white dark:border-slate-950 flex items-center justify-center","whatsapp"===e.platform&&"bg-green-500","widget"===e.platform&&"bg-violet-500",("instagram"===e.platform||"instagram_comment"===e.platform)&&"bg-pink-500",("messenger"===e.platform||"facebook_comment"===e.platform)&&"bg-blue-500","email"===e.platform&&"bg-amber-500","call"===e.platform&&"bg-sky-500","lead_form"===e.platform&&"bg-violet-500"),
1children:ae(e.platform,"w-2.5 h-2.5 text-white")})]}),(0,s.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between gap-2",children:[(0,s.jsxs)("div",{className:"flex items-center gap-1.5 min-w-0",children:[(0,s.jsx)("span",{className:(0,G.cn)("text-[15px] truncate leading-tight",e.is_unread?"font-semibold text-slate-900 dark:text-white":"font-medium text-slate-700 dark:text-slate-300"),children:eR(e.contact_name||(null===(t=e.meta_data)||void 0===t?void 0:t.contact_name),e.platform)}),(0,s.jsx)("span",{className:(0,G.cn)("text-[9px] font-semibold px-1.5 py-0.5 rounded-full flex-shrink-0 uppercase tracking-wider","whatsapp"===e.platform?"bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400":"instagram"===e.platform||"instagram_comment"===e.platform?"bg-pink-100 text-pink-700 dark:bg-pink-900/30 dark:text-pink-400":"messenger"===e.platform||"facebook_comment"===e.platform?"bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400":"widget"===e.platform?"bg-violet-100 text-violet-700 dark:bg-violet-900/30 dark:text-violet-400":"telegram"===e.platform?"bg-sky-100 text-sky-700 dark:bg-sky-900/30 dark:text-sky-400":"email"===e.platform?"bg-amber-100 text-amber-700 dark:bg-amber-900/30 dark:text-amber-400":"call"===e.platform?"bg-sky-100 text-sky-700 dark:bg-sky-900/30 dark:text-sky-400":"lead_form"===e.platform?"bg-violet-100 text-violet-700 dark:bg-violet-900/30 dark:text-violet-400":"bg-slate-100 text-slate-600 dark:bg-slate-800 dark:text-slate-400"),children:"whatsapp"===e.platform?"WhatsApp":"instagram"===e.platform?"IG DM":"instagram_comment"===e.platform?"IG Comment":"messenger"===e.platform?"Messenger":"facebook_comment"===e.platform?"FB Comment":"widget"===e.platform?"Website":"telegram"===e.platform?"Telegram":"email"===e.platform?"Email":"call"===e.platform?"Call":"lead_form"===e.platform?"Form":"instagram_story_mention"===e.platform?"Story":"instagram_mention"===e.platform?"Mention":null===(a=e.platform)||void 0===a?void 0:a.substring(0,2).toUpperCase()})]}),(0,s.jsx)("span",{className:"text-xs text-slate-400 tabular-nums flex-shrink-0",children:t7(e.last_message_at)})]}),(e.phone||e.contact_phone||(null===(l=e.meta_data)||void 0===l?void 0:l.phone))&&(0,s.jsxs)("p",{className:"text-[11px] text-slate-400 dark:text-slate-500 leading-tight mt-0.5 truncate",children:["+",String(e.phone||e.contact_phone||(null===(r=e.meta_data)||void 0===r?void 0:r.phone)).replace(/^\+/,"")]}),(0,s.jsxs)("div",{className:"flex items-center justify-between gap-2 mt-1",children:[(0,s.jsx)("p",{className:(0,G.cn)("text-[13px] truncate leading-normal",e.is_unread?"text-slate-600 dark:text-slate-400":"text-slate-400 dark:text-slate-500"),children:eD(e.platform)?e.last_message?"\uD83D\uDCAC ".concat(e.last_message):"\uD83D\uDCAC Comment thread":"instagram_story_mention"===e.platform?"\uD83D\uDCF1 Mentioned in story":"instagram_mention"===e.platform?"@ Mentioned you":e.last_message||"No messages"}),(null!==(m=e.unread_count)&&void 0!==m?m:0)>0&&(0,s.jsx)("span",{className:"min-w-[20px] h-[20px] px-1.5 text-[11px] font-semibold text-white bg-emerald-500 rounded-full flex items-center justify-center flex-shrink-0",children:e.unread_count})]}),e.channels&&e.channels.length>1&&(0,s.jsx)("div",{className:"flex items-center gap-1 mt-1.5",children:e.channels.map(e=>(0,s.jsx)("div",{className:(0,G.cn)("flex items-center gap-0.5 px-1.5 py-0.5 rounded-full text-[10px] font-medium","whatsapp"===e.channel?"bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400":"instagram"===e.channel||"instagram_comment"===e.channel?"bg-pink-100 text-pink-700 dark:bg-pink-900/30 dark:text-pink-400":"messenger"===e.channel||"facebook_comment"===e.channel?"bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400":"bg-slate-100 text-slate-600"),children:ae(e.channel,"w-2.5 h-2.5")},e.id))}),("instagram_comment"===e.platform||"facebook_comment"===e.platform)&&((null===(n=e.meta_data)||void 0===n?void 0:n.post_thumbnail)||(null===(i=e.meta_data)||void 0===i?void 0:i.post_caption))&&(0,s.jsxs)("div",{className:"flex items-center gap-2 mt-1.5 p-1.5 bg-slate-50 dark:bg-slate-800 rounded-md",children:[(null===(o=e.meta_data)||void 0===o?void 0:o.post_thumbnail)&&(0,s.jsx)("img",{src:e.meta_data.post_thumbnail,alt:"Post",className:"w-8 h-8 rounded object-cover flex-shrink-0",onError:e=>{e.target.style.display="none"}}),(0,s.jsx)("span",{className:"text-[11px] text-slate-400 truncate leading-tight",children:(null===(c=e.meta_data)||void 0===c?void 0:c.post_c
1aption)||"Commented on post"})]})]})]},e.id)}),aa.length>tf&&(0,s.jsx)("button",{onClick:()=>{eo.length<=tf?tV(!0):tj(e=>e+30)},disabled:t_,className:"w-full py-3 text-center text-xs text-slate-400 hover:text-slate-600 hover:bg-slate-50 dark:hover:bg-slate-800/50 transition-colors",children:t_?(0,s.jsx)(v.Z,{className:"w-4 h-4 animate-spin mx-auto"}):"Show more (".concat(aa.length-tf," remaining)")})]})})]}),(0,s.jsx)("div",{className:(0,G.cn)("flex-1 flex flex-col min-w-0 min-h-0",!em&&"hidden sm:flex"),children:em?(0,s.jsxs)(s.Fragment,{children:[(()=>{var e,t,a,l;let r=eW(em.platform);return(0,s.jsxs)("div",{className:(0,G.cn)("px-4 py-3 flex items-center justify-between",r.headerBg),children:[(0,s.jsxs)("div",{className:"flex items-center gap-3 min-w-0",children:[(0,s.jsx)(i.z,{variant:"ghost",size:"icon",className:(0,G.cn)("sm:hidden h-8 w-8 flex-shrink-0",r.headerText,"hover:bg-white/20"),onClick:()=>eu(null),children:(0,s.jsx)(k.Z,{className:"w-5 h-5"})}),(0,s.jsx)("div",{className:"relative flex-shrink-0",children:(0,s.jsxs)(d.qE,{className:"w-10 h-10 ring-2 ring-white/30",children:[em.avatar_url&&(0,s.jsx)(d.F$,{src:em.avatar_url}),(0,s.jsx)(d.Q5,{className:(0,G.cn)("text-white text-sm font-bold",eL(em.contact_name||"U")),children:eP(em.contact_name||"")})]})}),(0,s.jsxs)("div",{className:"min-w-0",children:[(0,s.jsx)("h2",{className:(0,G.cn)("text-[15px] font-semibold truncate",r.headerText),children:eR(em.contact_name,em.platform)}),(0,s.jsxs)("div",{className:(0,G.cn)("flex items-center gap-1.5 text-xs",r.headerSub),children:[(()=>{var e,t;let a=em.phone||em.contact_phone||(null===(e=em.meta_data)||void 0===e?void 0:e.phone)||(null===(t=em.meta_data)||void 0===t?void 0:t.wa_id);if(!a)return null;let l="+".concat(String(a).replace(/^\+/,""));return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("button",{type:"button",title:"Copy number",onClick:e=>{var t;e.stopPropagation(),null===(t=navigator.clipboard)||void 0===t||t.writeText(l),q.A.success("Number copied")},className:"font-medium tabular-nums hover:underline cursor-pointer",children:l}),(0,s.jsx)("span",{className:"opacity-50",children:"\xb7"})]})})(),em.channels&&em.channels.length>1?(0,s.jsx)(s.Fragment,{children:em.channels.map(e=>(0,s.jsxs)("span",{className:"flex items-center gap-0.5 px-1.5 py-0.5 rounded-full bg-white/20 text-[10px] font-medium",children:[ae(e.channel,"w-2.5 h-2.5"),at(e.channel).split(" ")[0]]},e.id))}):(0,s.jsxs)(s.Fragment,{children:[ae(em.platform,"w-3 h-3"),(0,s.jsx)("span",{children:r.label})]}),em.page_name&&"Instagram"!==em.page_name&&"Messenger"!==em.page_name&&"Facebook"!==em.page_name&&"WhatsApp"!==em.page_name&&"WhatsApp Business"!==em.page_name&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("span",{children:"\xb7"}),(0,s.jsx)("span",{className:"truncate",children:em.page_name})]}),("instagram_comment"===em.platform||"facebook_comment"===em.platform)&&(em.post_url||(null===(e=em.meta_data)||void 0===e?void 0:e.post_url))&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("span",{children:"â¢"}),(0,s.jsx)("a",{href:em.post_url||(null===(t=em.meta_data)||void 0===t?void 0:t.post_url),target:"_blank",rel:"noopener noreferrer",className:"text-blue-500 hover:text-blue-600 hover:underline",children:"View Post"})]})]})]})]}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[er&&("instagram_comment"===em.platform||"facebook_comment"===em.platform)&&(0,s.jsxs)(i.z,{variant:"secondary",size:"sm",className:"gap-1.5 bg-white/20 hover:bg-white/30 text-white border-0",onClick:()=>e2(!0),children:[(0,s.jsx)(N.Z,{className:"w-4 h-4"}),"Add to Lead"]}),(0,s.jsxs)($.h_,{children:[(0,s.jsx)($.$F,{asChild:!0,children:(0,s.jsx)(i.z,{variant:"ghost",size:"icon",className:(0,G.cn)("h-9 w-9",r.headerText,"hover:bg-white/20"),children:(0,s.jsx)(g.Z,{className:"w-4 h-4"})})}),(0,s.jsxs)($.AW,{align:"end",className:"w-56",children:[er&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)($.Xi,{onClick:()=>e2(!0),children:[(0,s.jsx)(N.Z,{className:"w-4 h-4 mr-2"}),"Add to Lead"]}),(0,s.jsx)($.VD,{})]}),(0,s.jsxs)($.Xi,{onClick:()=>e0(!0),children:[(0,s.jsx)(y.Z,{className:"w-4 h-4 mr-2"}),"Merge Conversations"]}),(0,s.jsxs)($.Xi,{onClick:()=>{H.api.get("/conversations/".concat(em.id,"/merge-candidates")).then(e=>{var t;let a=(null===(t=e.data.candidates)||void 0===t?void 0:t.length)||0;a>0?(q.A.info("Found ".concat(a," conversation(s) that can be merged")),e0(!0)):q.A.info("No duplicate conversations foun
1d for this contact")}).catch(()=>{q.A.error("Failed to check for duplicates")})},children:[(0,s.jsx)(C.Z,{className:"w-4 h-4 mr-2"}),"Find Duplicate Chats"]}),(em.phone||em.contact_phone||(null===(a=em.meta_data)||void 0===a?void 0:a.phone)||(null===(l=em.meta_data)||void 0===l?void 0:l.wa_id))&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)($.VD,{}),(0,s.jsxs)($.Xi,{onClick:tH,disabled:t$,className:"text-red-600 focus:text-red-600 focus:bg-red-50 dark:focus:bg-red-950",children:[t$?(0,s.jsx)(v.Z,{className:"w-4 h-4 mr-2 animate-spin"}):(0,s.jsx)(f.Z,{className:"w-4 h-4 mr-2"}),"Block on WhatsApp"]}),(0,s.jsxs)($.Xi,{onClick:tQ,disabled:tG,children:[tG?(0,s.jsx)(v.Z,{className:"w-4 h-4 mr-2 animate-spin"}):(0,s.jsx)(S.Z,{className:"w-4 h-4 mr-2"}),"Unblock on WhatsApp"]})]})]})]})]})]})})(),("instagram_comment"===em.platform||"facebook_comment"===em.platform)&&((null===(a=em.meta_data)||void 0===a?void 0:a.post_thumbnail)||(null===(n=em.meta_data)||void 0===n?void 0:n.post_url))&&(0,s.jsxs)("a",{href:em.post_url||(null===(Q=em.meta_data)||void 0===Q?void 0:Q.post_url),target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-3 px-4 py-2 bg-slate-50 dark:bg-slate-800 border-b border-slate-200 dark:border-slate-700 hover:bg-slate-100 dark:hover:bg-slate-700 transition-colors",children:[(null===(J=em.meta_data)||void 0===J?void 0:J.post_thumbnail)?(0,s.jsx)("img",{src:em.meta_data.post_thumbnail,alt:"Post",className:"w-12 h-12 rounded-lg object-cover flex-shrink-0",onError:e=>{e.target.style.display="none"}}):(0,s.jsx)("div",{className:"w-12 h-12 rounded-lg bg-slate-200 dark:bg-slate-700 flex items-center justify-center flex-shrink-0",children:(0,s.jsx)(A.Z,{className:"w-5 h-5 text-slate-400"})}),(0,s.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,s.jsx)("p",{className:"text-xs font-medium text-slate-500 dark:text-slate-400",children:"instagram_comment"===em.platform?"Instagram Post":"Facebook Post"}),(0,s.jsx)("p",{className:"text-sm text-slate-700 dark:text-slate-300 truncate",children:(null===(Y=em.meta_data)||void 0===Y?void 0:Y.post_caption)||"View original post"})]}),(0,s.jsx)("span",{className:"text-xs text-blue-500 flex-shrink-0",children:"View"})]}),(0,s.jsx)("div",{className:(0,G.cn)("flex-1 overflow-y-auto",eW(em.platform).bg),children:e_?(0,s.jsx)("div",{className:"flex items-center justify-center h-full",children:(0,s.jsx)(v.Z,{className:"w-6 h-6 animate-spin text-emerald-500"})}):0===ex.length?(0,s.jsxs)("div",{className:"flex flex-col items-center justify-center h-full text-center",children:[(0,s.jsx)(c.Z,{className:"w-8 h-8 text-slate-300 dark:text-slate-600 mb-2"}),(0,s.jsx)("p",{className:"text-sm text-slate-400",children:"No messages yet"})]}):eD(em.platform)?(0,s.jsxs)("div",{className:"p-4",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2 mb-4 px-3 py-2 bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 shadow-sm",children:[(0,s.jsx)(Z.Z,{className:"w-4 h-4 text-pink-500"}),(0,s.jsxs)("span",{className:"text-sm font-medium text-slate-700 dark:text-slate-300",children:[ex.length," comment",1!==ex.length?"s":""," on"," ",new Set(ex.map(e=>{var t;return null===(t=e.meta_data)||void 0===t?void 0:t.post_id}).filter(Boolean)).size," post",1!==new Set(ex.map(e=>{var t;return null===(t=e.meta_data)||void 0===t?void 0:t.post_id}).filter(Boolean)).size?"s":""]}),(0,s.jsxs)("span",{className:"text-xs text-slate-400 ml-auto",children:[(null===(ee=ex[0])||void 0===ee?void 0:ee.created_at)?t7(ex[0].created_at):""," â ",(null===(et=ex[ex.length-1])||void 0===et?void 0:et.created_at)?t7(ex[ex.length-1].created_at):""]})]}),(()=>{let e=new Map,t=[];ex.forEach(a=>{var s,l,r,n,i;let o=(null===(s=a.meta_data)||void 0===s?void 0:s.post_id)||(null===(l=a.meta_data)||void 0===l?void 0:l.media_id);o?(e.has(o)||e.set(o,{postId:o,postUrl:null===(r=a.meta_data)||void 0===r?void 0:r.post_url,postThumb:null===(n=a.meta_data)||void 0===n?void 0:n.post_thumbnail,postCaption:null===(i=a.meta_data)||void 0===i?void 0:i.post_caption,comments:[]}),e.get(o).comments.push(a)):t.push(a)});let a=Array.from(e.values());return a.sort((e,t)=>
1{var a,s;let l=(null===(a=e.comments[e.comments.length-1])||void 0===a?void 0:a.created_at)||"";return((null===(s=t.comments[t.comments.length-1])||void 0===s?void 0:s.created_at)||"").localeCompare(l)}),(0,s.jsxs)("div",{className:"space-y-3",children:[a.map(e=>(0,s.jsxs)("div",{className:"bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 overflow-hidden shadow-sm",children:[(0,s.jsxs)("a",{href:e.postUrl||"#",target:"_blank",rel:"noopener noreferrer",className:"flex items-start gap-3 p-3 hover:bg-slate-50 dark:hover:bg-slate-700/50 transition-colors border-b border-slate-100 dark:border-slate-700",children:[e.postThumb?(0,s.jsx)("img",{src:e.postThumb,alt:"",className:"w-16 h-16 rounded-lg object-cover flex-shrink-0",onError:e=>{e.target.style.display="none"}}):(0,s.jsx)("div",{className:"w-16 h-16 rounded-lg bg-gradient-to-br from-slate-100 to-slate-200 dark:from-slate-700 dark:to-slate-600 flex items-center justify-center flex-shrink-0",children:(0,s.jsx)(A.Z,{className:"w-6 h-6 text-slate-400"})}),(0,s.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,s.jsx)("p",{className:"text-[13px] font-medium text-slate-800 dark:text-slate-200 line-clamp-2 leading-snug",children:e.postCaption||"Post"}),(0,s.jsxs)("div",{className:"flex items-center gap-2 mt-1.5",children:[ae(em.platform,"w-3 h-3"),(0,s.jsxs)("span",{className:"text-[11px] text-slate-400",children:[e.comments.length," comment",1!==e.comments.length?"s":""]}),(0,s.jsx)(E.Z,{className:"w-3 h-3 text-blue-400 ml-auto"})]})]})]}),(0,s.jsx)("div",{className:"divide-y divide-slate-50 dark:divide-slate-700/50",children:e.comments.map(e=>{var t,a,l,r;return(0,s.jsx)("div",{className:(0,G.cn)("px-3 py-2.5 hover:bg-slate-50/50 dark:hover:bg-slate-800/50 transition-colors",td===(null===(t=e.meta_data)||void 0===t?void 0:t.comment_id)&&"bg-blue-50/50 dark:bg-blue-950/20 ring-1 ring-blue-200 dark:ring-blue-800","agent"===e.sender_type&&"bg-emerald-50/30 dark:bg-emerald-950/10"),children:(0,s.jsxs)("div",{className:(0,G.cn)("flex items-start gap-2.5","agent"===e.sender_type&&"flex-row-reverse"),children:[(0,s.jsx)("div",{className:(0,G.cn)("w-7 h-7 rounded-full flex items-center justify-center flex-shrink-0 text-[10px] font-bold text-white","agent"===e.sender_type?"bg-emerald-500":eL(e.sender_name||em.contact_name||"U")),children:"agent"===e.sender_type?"A":eP(e.sender_name||em.contact_name||"")}),(0,s.jsxs)("div",{className:(0,G.cn)("flex-1 min-w-0","agent"===e.sender_type&&"text-right"),children:[(0,s.jsxs)("div",{className:(0,G.cn)("flex items-center gap-2","agent"===e.sender_type&&"justify-end"),children:[(0,s.jsx)("span",{className:"text-[11px] font-semibold text-slate-700 dark:text-slate-300",children:"agent"===e.sender_type?"You":e.sender_name||em.contact_name}),(0,s.jsx)("span",{className:"text-[10px] text-slate-400",children:t7(e.created_at)})]}),(0,s.jsx)("p",{className:(0,G.cn)("text-[13px] leading-relaxed mt-0.5","agent"===e.sender_type?"text-emerald-800 dark:text-emerald-200":"text-slate-800 dark:text-slate-200"),children:e.content}),(null===(a=e.meta_data)||void 0===a?void 0:a.comment_id)&&(0,s.jsxs)("div",{className:"flex items-center gap-3 mt-1.5",children:["agent"!==e.sender_type&&(0,s.jsx)("button",{onClick:()=>{var t,a,s;tc((null===(t=e.meta_data)||void 0===t?void 0:t.comment_id)||null),tu((null===(a=e.content)||void 0===a?void 0:a.substring(0,40))||""),null===(s=tF.current)||void 0===s||s.focus()},className:"text-[10px] text-blue-500 hover:text-blue-700 font-medium",children:"Reply"}),(null===(l=e.meta_data)||void 0===l?void 0:l.liked)?(0,s.jsx)("span",{className:"text-[10px] text-pink-500 font-medium",children:"⤠Liked"}):(0,s.jsx)("button",{onClick:()=>{var t;H.api.post("/comments/".concat(null===(t=e.meta_data)||void 0===t?void 0:t.comment_id,"/like")).then(()=>{q.A.success("Liked"),tO(em.id)}).catch(()=>q.A.error("Failed to like"))},className:"text-[10px] text-slate-400 hover:text-pink-500 font-medium",children:"⡠Like"}),(null===(r=e.meta_data)||void 0===r?void 0:r.hidden)?(0,s.jsx)("span",{className:"text-[10px] text-amber-500 font-medium",children:"Hidden"}):(0,s.jsx)("button",{onClick:()=>{var t;H.api.post("/comments/".concat(null===(t=e.meta_data)||void 0===t?void 0:t.comment_id,"/hide")).then(()=>{q.A.success("Comment hidden"),tO(em.id)}).catch(()=>q.A.error("Failed to hide"))},className:"text-[10px] text-slate-400 hover:text-amber-500 font-medium",children:"Hide"}),(0,s.jsx)("button",{onClick:()=>
1{if(confirm("Delete this comment permanently?")){var t;H.api.delete("/comments/".concat(null===(t=e.meta_data)||void 0===t?void 0:t.comment_id)).then(()=>{q.A.success("Comment deleted"),tO(em.id)}).catch(()=>q.A.error("Failed to delete"))}},className:"text-[10px] text-slate-400 hover:text-red-500 font-medium",children:"Delete"})]})]})]})},e.id)})})]},e.postId)),(()=>{let e=new Map;t.forEach(t=>{var a;let s=(null===(a=t.meta_data)||void 0===a?void 0:a.channel)||"messages";e.has(s)||e.set(s,[]),e.get(s).push(t)});let a={whatsapp:"WhatsApp",instagram:"Instagram DM",messenger:"Messenger",facebook:"Facebook",telegram:"Telegram",email:"Email",messages:"Messages"},l={whatsapp:"bg-green-500",instagram:"bg-pink-500",messenger:"bg-blue-500",facebook:"bg-blue-500",telegram:"bg-sky-500",email:"bg-amber-500",messages:"bg-slate-400"},r={whatsapp:"border-l-green-500",instagram:"border-l-pink-500",messenger:"border-l-blue-500",facebook:"border-l-blue-500",telegram:"border-l-sky-500",email:"border-l-amber-500",messages:"border-l-slate-300"};return Array.from(e.entries()).map(e=>{let[t,n]=e;return(0,s.jsxs)("div",{className:(0,G.cn)("bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 overflow-hidden shadow-sm border-l-4",r[t]||"border-l-slate-300"),children:[(0,s.jsxs)("div",{className:"px-3 py-2 border-b border-slate-100 dark:border-slate-700 flex items-center gap-2",children:[(0,s.jsx)("span",{className:(0,G.cn)("w-2.5 h-2.5 rounded-full",l[t]||"bg-slate-400")}),(0,s.jsx)("span",{className:"text-[11px] font-semibold text-slate-600 dark:text-slate-300 uppercase tracking-wider",children:a[t]||t}),(0,s.jsx)("span",{className:"text-[10px] text-slate-400 ml-auto",children:n.length})]}),(0,s.jsx)("div",{className:"divide-y divide-slate-50 dark:divide-slate-700/50",children:n.map(e=>(0,s.jsxs)("div",{className:(0,G.cn)("flex items-start gap-2.5 px-3 py-2","agent"===e.sender_type&&"flex-row-reverse bg-emerald-50/30 dark:bg-emerald-950/10"),children:[(0,s.jsx)("div",{className:(0,G.cn)("w-6 h-6 rounded-full flex items-center justify-center flex-shrink-0 mt-0.5 text-[10px] font-bold text-white","agent"===e.sender_type?"bg-emerald-500":eL(e.sender_name||(null==em?void 0:em.contact_name)||"U")),children:"agent"===e.sender_type?"A":eP(e.sender_name||(null==em?void 0:em.contact_name)||"")}),(0,s.jsxs)("div",{className:(0,G.cn)("flex-1 min-w-0","agent"===e.sender_type&&"text-right"),children:[(0,s.jsx)("p",{className:"text-[13px] text-slate-800 dark:text-slate-200",children:e.content}),(0,s.jsx)("span",{className:"text-[10px] text-slate-400",children:t7(e.created_at)})]})]},e.id))})]},t)})})(),(0,s.jsx)("div",{ref:tE})]})})()]}):(0,s.jsxs)("div",{className:"px-4 py-4",children:[ex.map((e,t)=>{var a,r,n,c,m,u,x,p,h,b,j,_,w,k,N,y,C,S,Z,O,X,q,H,K,Q,J,Y;let ee="agent"===e.sender_type||"bot"===e.sender_type||"ai"===e.sender_type,et=t>0&&((null===(a=ex[t-1])||void 0===a?void 0:a.sender_type)==="agent"||(null===(r=ex[t-1])||void 0===r?void 0:r.sender_type)==="bot"||(null===(n=ex[t-1])||void 0===n?void 0:n.sender_type)==="ai"),ea=0===t||ee!==et,es=e.is_deleted||(null===(c=e.meta_data)||void 0===c?void 0:c.deleted),el=e.is_edited||(null===(m=e.meta_data)||void 0===m?void 0:m.is_edited),er=ti===e.id,en=th===e.id,ei=(null===(u=e.meta_data)||void 0===u?void 0:u.channel)||(null==em?void 0:em.platform)||"",eo=t>0&&((null===(p=ex[t-1])||void 0===p?void 0:null===(x=p.meta_data)||void 0===x?void 0:x.channel)||(null==em?void 0:em.platform))||"";return(0,s.jsxs)("div",{children:[(0===t||ei!==eo)&&(0,s.jsxs)("div",{className:"flex items-center gap-2 my-3",children:[(0,s.jsx)("div",{className:"flex-1 h-px border-t border-dashed border-slate-200 dark:border-slate-700"}),(0,s.jsxs)("div",{className:"flex items-center gap-1.5 px-3 py-1 rounded-full bg-white dark:bg-slate-800 border border-slate-200 dark:border-slate-700 shadow-sm",children:[(0,s.jsx)("span",{className:(0,G.cn)("w-2 h-2 rounded-full",{whatsapp:"bg-green-500",instagram:"bg-pink-500",instagram_comment:"bg-pink-500",messenger:"bg-blue-500",facebook_comment:"bg-blue-500",telegram:"bg-sky-500",email:"bg-amber-500"}[ei]||"bg-slate-400")}),(0,s.jsx)("span",{className:"text-[10px] font-semibold text-slate-500",children:{whatsapp:"WhatsApp",instagram:"Instagram DM",instagram_comment:"Instagram Comment",messenger:"Messenger",facebook_comment:"Facebook Comment",telegram:"Telegram",email:"Email"}[ei]||ei})]}),(0,s.jsx)("div",{className:"flex-1 h-px border-t border-dashed border-slate-200 dark:border-slate-700"})]}),(0,s.jsx)("div",{className:(0,G.cn)("flex w-full",ee?"justify-end":"justify-start",ea?"mt-4 first:mt-0":"mt-1"),children:(0,s.jsxs)("div",{className:(0,G.cn)("flex gap-1.5 sm:gap-2 max-w-[85%] sm:max-w-[75%] group/msg items-start",ee?"flex-row-reverse":"flex-row"),children:[!ee&&ea&&(0,s.jsxs)(d.qE,{className:"w-8 h-8 flex-shrink-0 mt-1",children:[(0,s.jsx)(d.F$,{src:null==em?void 0:em.avatar_url}
1),(0,s.jsx)(d.Q5,{className:(0,G.cn)("text-white text-[11px] font-bold",eL((null==em?void 0:em.contact_name)||"U")),children:eP((null==em?void 0:em.contact_name)||"")})]}),!ee&&!ea&&(0,s.jsx)("div",{className:"w-8 flex-shrink-0"}),ee&&!es&&!er&&(0,s.jsxs)($.h_,{children:[(0,s.jsx)($.$F,{asChild:!0,children:(0,s.jsx)(i.z,{variant:"ghost",size:"icon",className:"h-6 w-6 rounded-full opacity-20 hover:opacity-100 group-hover/msg:opacity-100 transition-opacity text-slate-400 hover:text-slate-600 hover:bg-slate-200 dark:hover:bg-slate-700 self-center",children:(0,s.jsx)(g.Z,{className:"w-3.5 h-3.5"})})}),(0,s.jsxs)($.AW,{align:"end",className:"w-48",children:[(0,s.jsxs)($.Xi,{onClick:()=>t8(e),children:[(0,s.jsx)(F.Z,{className:"w-4 h-4 mr-2"}),"Edit message"]}),(0,s.jsx)($.VD,{}),(0,s.jsxs)($.Xi,{onClick:()=>t3(e.id,!1),disabled:en,children:[en?(0,s.jsx)(v.Z,{className:"w-4 h-4 mr-2 animate-spin"}):(0,s.jsx)(M.Z,{className:"w-4 h-4 mr-2"}),"Delete for me"]}),((null==em?void 0:em.platform)==="instagram_comment"||(null==em?void 0:em.platform)==="facebook_comment")&&(0,s.jsxs)($.Xi,{onClick:()=>t3(e.id,!0),className:"text-red-600 focus:text-red-600",disabled:en,children:[(0,s.jsx)(f.Z,{className:"w-4 h-4 mr-2"}),"Delete for everyone"]})]})]}),(0,s.jsxs)("div",{className:(0,G.cn)("flex flex-col",ee?"items-end":"items-start"),children:[!ee&&ea&&(0,s.jsx)("span",{className:"text-xs text-slate-500 mb-1 ml-1",children:eR(em.contact_name,em.platform)||e.sender_name||"Contact"}),(0,s.jsxs)("div",{className:(0,G.cn)("rounded-2xl overflow-hidden shadow-sm","image"===e.message_type?"p-1":"px-3.5 py-2",ee?(0,G.cn)(eW(null==em?void 0:em.platform).bubbleSent,"rounded-tr-sm"):(0,G.cn)(eW(null==em?void 0:em.platform).bubbleReceived,"rounded-tl-sm")),children:[(()=>{var t;let a=e.attachments,l=a&&a.length>0?a[0]:null,r=(null==l?void 0:l.type)==="image"&&(null==l?void 0:l.url)||e.content&&/\.(jpg|jpeg|png|gif|webp)(\?|$)/i.test(e.content)&&e.content||"image"===e.message_type&&(null===(t=e.content)||void 0===t?void 0:t.startsWith("http"))&&e.content||null,n="image"===e.message_type||(null==l?void 0:l.type)==="image"||e.content&&/\.(jpg|jpeg|png|gif|webp)(\?|$)/i.test(e.content);return n&&r?(0,s.jsxs)("div",{className:"relative",children:[(0,s.jsx)("img",{src:r,alt:"Shared image",className:"max-w-[280px] sm:max-w-[320px] max-h-[280px] sm:max-h-[320px] rounded-lg sm:rounded-xl object-cover cursor-pointer hover:opacity-90 transition-opacity",onClick:()=>window.open(r,"_blank"),onError:e=>{var t;let a=e.target;a.style.display="none",null===(t=a.nextElementSibling)||void 0===t||t.classList.remove("hidden")}}),(0,s.jsx)("div",{className:"hidden p-3",children:(0,s.jsx)("p",{className:"text-sm whitespace-pre-wrap leading-relaxed",children:e.content})})]}):n&&!r?(0,s.jsxs)("div",{className:"flex items-center gap-2 p-2",children:[(0,s.jsx)(A.Z,{className:"w-5 h-5 text-slate-400"}),(0,s.jsx)("span",{className:"text-sm",children:e.content||"Image"})]}):null})(),(()=>{var t,a;let l=e.attachments,r=l&&l.length>0?l[0]:null;if(!((null==r?void 0:r.type)==="template"||"story_reply"===e.message_type||"story_mention"===e.message_type||"Shared a story"===e.content||"Replied to your story"===e.content))return null;(null==r?void 0:r.url)||(null==r?void 0:null===(t=r.payload)||void 0===t?void 0:t.url)||null===(a=e.meta_data)||void 0===a||a.story_url;let n=e.content&&"Shared a story"!==e.content&&"Replied to your story"!==e.content&&"Mentioned you in story"!==e.content&&"Mentioned you in their story"!==e.content,i="story_reply"===e.message_type?"Replied to your story":"story_mention"===e.message_type?"Mentioned you in story":"Shared a story";return(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsxs)("a",{href:(null==em?void 0:em.instagram_username)?"https://instagram.com/".concat(em.instagram_username.replace("@","")):"https://instagram.com",target:"_blank",rel:"noopener noreferrer",className:"inline-flex items-center gap-1.5 px-2 py-1 bg-gradient-to-r from-pink-100 to-purple-100 dark:from-pink-900/40 dark:to-purple-900/40 rounded-full border border-pink-200 dark:border-pink-700 hover:from-pink-200 hover:to-purple-200 dark:hover:from-pink-800/50 dark:hover:to-purple-800/50 transition-colors text-xs",children:[(0,s.jsx)("div",{className:"w-4 h-4 rounded-full bg-gradient-to-br from-pink-500 to-purple-600 flex items-center justify-center flex-shrink-0",children:(0,s.jsx)("svg",{className:"w-2.5 h-2.5 text-white",f
1ill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:(0,s.jsx)("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M15 10l4.553-2.276A1 1 0 0121 8.618v6.764a1 1 0 01-1.447.894L15 14M5 18h8a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v8a2 2 0 002 2z"})})}),(0,s.jsx)("span",{className:"text-pink-700 dark:text-pink-300 font-medium",children:i}),(0,s.jsx)("svg",{className:"w-3 h-3 text-pink-500",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:(0,s.jsx)("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"})})]}),n&&(0,s.jsx)("p",{className:"text-sm whitespace-pre-wrap leading-relaxed",children:e.content})]})})(),!(null===(h=e.message_type)||void 0===h?void 0:h.includes("image"))&&!(null===(b=e.attachments)||void 0===b?void 0:b.some(e=>"image"===e.type))&&!(null===(j=e.attachments)||void 0===j?void 0:j.some(e=>"template"===e.type))&&!["story_reply","story_mention"].includes(e.message_type||"")&&("file"===e.message_type||"document"===e.message_type||e.content&&/\.(pdf|doc|docx|txt|xls|xlsx|zip)(\?|$)/i.test(e.content))&&(0,s.jsxs)("a",{href:e.content,target:"_blank",rel:"noopener noreferrer",className:(0,G.cn)("flex items-center gap-3 p-2 rounded-lg transition-colors",ee?"hover:bg-green-600":"hover:bg-slate-100 dark:hover:bg-slate-700"),children:[(0,s.jsx)("div",{className:(0,G.cn)("w-10 h-10 rounded-lg flex items-center justify-center flex-shrink-0",ee?"bg-green-600":"bg-slate-100 dark:bg-slate-700"),children:(0,s.jsx)(z.Z,{className:"w-5 h-5"})}),(0,s.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,s.jsx)("p",{className:"text-sm font-medium truncate",children:e.content.split("/").pop()||"File"}),(0,s.jsx)("p",{className:(0,G.cn)("text-xs",ee?"text-green-100":"text-slate-500"),children:"Click to download"})]})]}),("audio"===e.message_type||(null===(_=e.attachments)||void 0===_?void 0:_.some(e=>"audio"===e.type))||(null===(w=e.meta_data)||void 0===w?void 0:w.audio_url))&&(0,s.jsx)("div",{className:"space-y-2",children:(()=>{var t,a;let r=null===(t=e.attachments)||void 0===t?void 0:t.find(e=>"audio"===e.type),n=null==r?void 0:r.id,i=null===(a=e.meta_data)||void 0===a?void 0:a.audio_url,o=localStorage.getItem("workspace_id")||"",d=i||(n?"/api/whatsapp/media/".concat(n,"?workspace_id=").concat(o):null);return d?(0,s.jsx)(e=>{let{src:t,isAgent:a}=e,r=l.useRef(null),[n,i]=l.useState(!1),[o,d]=l.useState(!1),[c,m]=l.useState(0),[u,x]=l.useState(0),[p,h]=l.useState(!0),[g,b]=l.useState(null);return(0,s.jsxs)("div",{className:"flex items-center gap-2 p-2 rounded-xl",children:[(0,s.jsx)("audio",{ref:r,src:t,preload:"auto",onLoadedMetadata:()=>{var e;return x((null===(e=r.current)||void 0===e?void 0:e.duration)||0)},onTimeUpdate:()=>{var e;return m((null===(e=r.current)||void 0===e?void 0:e.currentTime)||0)},onEnded:()=>i(!1),onCanPlayThrough:()=>h(!1),onWaiting:()=>h(!0),onPlaying:()=>h(!1),onError:e=>{console.error("Audio error:",e),b("Failed to load"),h(!1)},onPlay:e=>{i(!0);let t=e.currentTarget;document.querySelectorAll("audio").forEach(e=>{e===t||e.paused||e.pause()})},onPause:()=>i(!1)}),(0,s.jsx)("div",{className:"w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 bg-green-600",children:a?(0,s.jsx)(T.Z,{className:"w-4 h-4 text-white"}):(0,s.jsx)(L.Z,{className:"w-4 h-4 text-white"})}),(0,s.jsx)("button",{onClick:()=>{!r.current||p||g||(n?r.current.pause():r.current.play().catch(e=>{console.error("Audio play error:",e),b("Failed to play")}),i(!n))},disabled:p||!!g,className:(0,G.cn)("w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0",g?"bg-red-100 dark:bg-red-900/30":"bg-green-100 hover:bg-green-200 dark:bg-green-900/30 dark:hover:bg-green-900/50",(p||g)&&"opacity-50 cursor-not-allowed"),children:p?(0,s.jsx)(v.Z,{className:"w-4 h-4 text-green-600 dark:text-green-400 animate-spin"}):g?(0,s.jsx)(P.Z,{className:"w-4 h-4 text-red-600 dark:text-red-400"}):n?(0,s.jsx)(R.Z,{className:"w-4 h-4 text-green-600 dark:text-green-400"}):(0,s.jsx)(D.Z,{className:"w-4 h-4 text-green-600 dark:text-green-400"})}),(0,s.jsxs)("div",{className:"flex-1 flex items-center gap-2",children:[(0,s.jsx)("input",{type:"range",min:0,max:u||100,value:c,onChange:e=>{r.current&&(r.current.currentTime=Number(e.target.value),m(Number(e.target.value)))},className:"flex-1 h-1 rounded-full appearance-none cursor-pointer bg-green-200 dark:bg-green-800 [&::-webkit-slider-thumb]:bg-green-600 [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-3 [&::-webkit-slider-thumb]:h-3 [&::-webkit-slider-thumb]:rounded-full"}),(0,s.jsx)("span",{className:"text-xs min-w-[35px] text-slate-600 dar
1k:text-slate-300",children:"".concat(Math.floor(c/60),":").concat(Math.floor(c%60).toString().padStart(2,"0"))})]}),(0,s.jsx)("button",{onClick:()=>{r.current&&(r.current.muted=!o,d(!o))},className:"w-6 h-6 flex items-center justify-center flex-shrink-0 text-slate-500 hover:text-slate-700 dark:text-slate-400 dark:hover:text-slate-200",children:o?(0,s.jsx)(W.Z,{className:"w-4 h-4"}):(0,s.jsx)(I.Z,{className:"w-4 h-4"})})]})},{src:d,isAgent:ee}):(0,s.jsxs)("div",{className:(0,G.cn)("flex items-center gap-2 p-2 rounded-lg",!ee&&"bg-slate-100 dark:bg-slate-700/50"),children:[ee?(0,s.jsx)(T.Z,{className:"w-4 h-4"}):(0,s.jsx)(L.Z,{className:"w-4 h-4"}),(0,s.jsx)("span",{className:"text-sm",children:"Voice message"})]})})()}),"image"!==e.message_type&&"file"!==e.message_type&&"document"!==e.message_type&&"audio"!==e.message_type&&"story_reply"!==e.message_type&&"story_mention"!==e.message_type&&!(null===(k=e.attachments)||void 0===k?void 0:k.some(e=>"image"===e.type))&&!(null===(N=e.attachments)||void 0===N?void 0:N.some(e=>"template"===e.type))&&!(null===(y=e.attachments)||void 0===y?void 0:y.some(e=>"audio"===e.type))&&"Shared a story"!==e.content&&"Replied to your story"!==e.content&&!(e.content&&/\.(jpg|jpeg|png|gif|webp|pdf|doc|docx|txt|xls|xlsx|zip)(\?|$)/i.test(e.content))&&(0,s.jsx)(s.Fragment,{children:es?(0,s.jsxs)("p",{className:"text-sm italic text-slate-400 flex items-center gap-1.5",children:[(0,s.jsx)(f.Z,{className:"w-3.5 h-3.5"}),"This message was deleted"]}):er?(0,s.jsxs)("div",{className:"flex flex-col gap-2 min-w-[200px]",children:[(0,s.jsx)(o.I,{value:tx,onChange:e=>tp(e.target.value),className:"text-sm bg-white dark:bg-slate-700 text-slate-900 dark:text-white",autoFocus:!0,onKeyDown:e=>{"Enter"!==e.key||e.shiftKey?"Escape"===e.key&&t6():(e.preventDefault(),t9())}}),(0,s.jsxs)("div",{className:"flex gap-1 justify-end",children:[(0,s.jsx)(i.z,{size:"sm",variant:"ghost",className:"h-6 px-2 text-xs",onClick:t6,children:"Cancel"}),(0,s.jsx)(i.z,{size:"sm",className:"h-6 px-2 text-xs bg-green-600 hover:bg-green-700",onClick:t9,children:"Save"})]})]}):(0,s.jsxs)(s.Fragment,{children:[((null===(C=e.meta_data)||void 0===C?void 0:C.post_url)||(null===(S=e.meta_data)||void 0===S?void 0:S.post_id))&&!ee&&(0,s.jsxs)("a",{href:(null===(Z=e.meta_data)||void 0===Z?void 0:Z.post_url)||"#",target:"_blank",rel:"noopener noreferrer",className:"block mb-1.5 rounded-lg overflow-hidden border border-slate-100 dark:border-slate-700 hover:border-slate-300 dark:hover:border-slate-500 transition-colors",children:[(null===(O=e.meta_data)||void 0===O?void 0:O.post_thumbnail)&&(0,s.jsx)("img",{src:e.meta_data.post_thumbnail,alt:"",className:"w-full max-w-[240px] h-[120px] object-cover",onError:e=>{e.target.style.display="none"}}),(0,s.jsx)("div",{className:"px-2.5 py-1.5 bg-slate-50/80 dark:bg-slate-700/50",children:(0,s.jsx)("p",{className:"text-[11px] text-slate-500 dark:text-slate-400 truncate leading-tight",children:(null===(X=e.meta_data)||void 0===X?void 0:X.post_caption)||"View post"})})]}),(null===(q=e.meta_data)||void 0===q?void 0:q.type)==="catalog"?(0,s.jsxs)("div",{className:"rounded-lg border border-green-200 dark:border-green-800 bg-green-50 dark:bg-green-900/20 p-3 max-w-[260px]",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2 mb-2",children:[(0,s.jsx)("div",{className:"w-8 h-8 rounded-lg bg-green-500 flex items-center justify-center",children:(0,s.jsx)(B.Z,{className:"w-4 h-4 text-white"})}),(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-xs font-semibold text-green-800 dark:text-green-300",children:"Product Catalog"}),(0,s.jsx)("p",{className:"text-[10px] text-green-600 dark:text-green-400",children:"Sent via WhatsApp"})]})]}),(0,s.jsx)("p",{className:"text-sm text-green-900 dark:text-green-200",children:(null===(H=e.content)||void 0===H?void 0:H.replace("[Catalog] ",""))||"Check out our products"}),(0,s.jsx)("div",{className:"mt-2 pt-2 border-t border-green-200 dark:border-green-700",children:(0,s.jsxs)("p",{className:"text-[10px] text-green-500 flex items-center gap-1",children:[(0,s.jsx)(E.Z,{className:"w-3 h-3"}),"View catalog on WhatsApp"]})})]}):(0,s.jsx)("p",{className:"text-sm whitespace-pre-wrap leading-relaxed",children:e.content})]})})]}),(0,s.jsxs)("div",{className:(0,G.cn)("flex items-center gap-1 mt-0.5 px-0.5",ee?"flex-row-reverse":"flex-row"),children:[(null===(K=e.meta_data)||void 0===K?void 0:K.channel)&&e.meta_data.channel!==(null==em?void 0:em.platform)&&(0,s.jsxs)("span",{className:(0,G.cn)("flex items-center gap-0.5 text-[9px] font-medium px-1.5 py-0.5 rounded-full","whatsapp"===e.meta_data.channel?"bg-green-100 text-green-600 dark:bg-green-900/30":(null===(Q=e.meta_data.channel)||void 0===Q?void 0:Q.includes("instagram"))?"bg-pink-100 text-pink-600 dark:bg-pink-900/30":(null===(J=e.meta_data.channel)||void 0===J?void 0:J.includes("messenger"))||(null===(Y=e.meta_data.channel)||void 0===Y?void 0:Y.includes("facebook"))?"bg-blue-100 text-blue-600 dar
1k:bg-blue-900/30":"bg-slate-100 text-slate-500"),children:[ae(e.meta_data.channel,"w-2 h-2"),(0,s.jsx)("span",{className:"ml-0.5",children:"whatsapp"===e.meta_data.channel?"WA":"instagram"===e.meta_data.channel?"IG DM":"instagram_comment"===e.meta_data.channel?"Comment":"messenger"===e.meta_data.channel?"Messenger":"facebook_comment"===e.meta_data.channel?"Comment":e.meta_data.channel})]}),(0,s.jsx)("span",{className:"text-[10px] text-slate-400 tabular-nums",children:t7(e.created_at)}),el&&!es&&(0,s.jsx)("span",{className:"text-[10px] text-slate-300 italic",children:"edited"}),ee&&!es&&(e.is_read?(0,s.jsx)(U.Z,{className:"w-3.5 h-3.5 text-blue-500"}):e.is_delivered?(0,s.jsx)(U.Z,{className:"w-3.5 h-3.5 text-slate-400"}):(0,s.jsx)(V.Z,{className:"w-3 h-3 text-slate-300"}))]})]})]})})]},e.id)}),(0,s.jsx)("div",{ref:tE})]})}),eG&&!["call","lead_form"].includes(em.platform)&&(0,s.jsx)("div",{className:"px-3 py-2 border-t border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900",children:(0,s.jsxs)("div",{className:"flex items-center gap-2.5 px-2.5 py-1.5 bg-slate-50 dark:bg-slate-800 rounded-lg",children:[eQ?(0,s.jsx)("img",{src:eQ,alt:"Preview",className:"w-10 h-10 rounded object-cover"}):(0,s.jsx)("div",{className:"w-8 h-8 rounded bg-slate-200 dark:bg-slate-700 flex items-center justify-center",children:(0,s.jsx)(z.Z,{className:"w-4 h-4 text-slate-500"})}),(0,s.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,s.jsx)("p",{className:"text-xs font-medium truncate",children:eG.name}),(0,s.jsxs)("p",{className:"text-[11px] text-slate-400",children:[((Number(eG.size)||0)/1024).toFixed(1)," KB"]})]}),(0,s.jsx)("button",{onClick:t5,className:"p-1 text-slate-400 hover:text-red-500 transition-colors",children:(0,s.jsx)(k.Z,{className:"w-3.5 h-3.5"})})]})}),e7&&(0,s.jsx)("div",{className:"px-4 py-1.5 border-t border-slate-100 dark:border-slate-800",children:(0,s.jsxs)("div",{className:"flex items-center gap-2 text-xs text-slate-400",children:[(0,s.jsxs)("div",{className:"flex space-x-0.5",children:[(0,s.jsx)("span",{className:"w-1.5 h-1.5 bg-slate-400 rounded-full animate-bounce",style:{animationDelay:"0ms"}}),(0,s.jsx)("span",{className:"w-1.5 h-1.5 bg-slate-400 rounded-full animate-bounce",style:{animationDelay:"150ms"}}),(0,s.jsx)("span",{className:"w-1.5 h-1.5 bg-slate-400 rounded-full animate-bounce",style:{animationDelay:"300ms"}})]}),(0,s.jsxs)("span",{children:[tt||"Contact"," is typing"]})]})}),["call","lead_form"].includes(em.platform)?(0,s.jsx)("div",{className:(0,G.cn)("px-4 py-3 border-t border-slate-200 dark:border-slate-800",eW(em.platform).inputBg),children:(0,s.jsxs)("div",{className:"flex items-start gap-2.5 rounded-xl border border-slate-200/80 bg-white/80 px-3 py-2.5 text-slate-600 shadow-sm dark:border-slate-700 dark:bg-slate-900/80 dark:text-slate-300",children:["call"===em.platform?(0,s.jsx)(x.Z,{className:"mt-0.5 h-4 w-4 flex-none text-sky-600"}):(0,s.jsx)(p.Z,{className:"mt-0.5 h-4 w-4 flex-none text-violet-600"}),(0,s.jsxs)("div",{children:[(0,s.jsx)("p",{className:"text-xs font-semibold text-slate-800 dark:text-slate-100",children:"Captured source â read only"}),(0,s.jsx)("p",{className:"mt-0.5 text-[11px] leading-4 text-slate-500 dark:text-slate-400",children:"call"===em.platform?"This transcript cannot be replied to. Continue by phone, WhatsApp or email.":"This form submission cannot receive replies. Continue on a connected messaging channel."})]})]})}):(0,s.jsxs)("div",{className:(0,G.cn)("px-3 py-2.5 border-t border-slate-200 dark:border-slate-800",eW(em.platform).inputBg),children:[(0,s.jsx)("input",{ref:tM,type:"file",className:"hidden",onChange:e=>t2(e,"file"),accept:".pdf,.doc,.docx,.txt,.xls,.xlsx,.zip"}),(0,s.jsx)("input",{ref:tz,type:"file",className:"hidden",onChange:e=>t2(e,"image"),accept:"image/*"}),td&&(0,s.jsxs)("div",{className:"flex items-center gap-2 mb-2 px-2 py-1.5 bg-blue-50 dark:bg-blue-950/20 rounded-lg",children:[(0,s.jsxs)("span",{className:"text-[11px] text-blue-600 dark:text-blue-400 flex-1 truncate",children:['Replying to: "',tm,'..."']}),(0,s.jsx)("button",{onClick:()=>{tc(null),tu("")},className:"text-[10px] text-blue-400 hover:text-blue-600",children:(0,s.jsx)(k.Z,{className:"w-3 h-3"})})]}),"facebook_comment"===em.platform&&(0,s.jsx)("div",{className:"flex items-center gap-2 mb-2 px-1",children:(0,s.jsx)("button",{onClick:()=>eC(!ey),className:(0,G.cn)("text-xs px-2 py-0.5 rounded-full transition-colors",ey?"bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400":"text-slate-400 hover:text-slate-600"),children:ey?"Private DM":"Public comment"})}),(0,s.jsxs)("div",{className:"flex items-center gap-1.5",children:[(0,s.jsxs)($.h_,{children:[(0,s.jsx)($.$F,{asChild:!0,children:(0,s.jsx)(i.z,{variant:"ghost",size:"icon",className:"h-8 w-8 flex-shrink-0",children:(0,s.jsx)(z.Z,{className:"w-4 h-4 text-slate-400"})})}),(0,s.jsxs)($.AW,{align:"start",children:[(0,s.jsxs)($.Xi,{onClick:()=>{var e;return null===(e=tz.current)||void 0===e?void 0:e.click()},children:[(0,s.jsx)(A.Z,{className:"w-4 h-4 mr-2"})," Image"]}),(0,s.jsxs)($.Xi,{onClick:()=>{var e;return null===(e=tM.current)||void 0===e?void 0:e.click()},children:[(0,s.jsx)(z.Z,{className:"w-4 h-4 mr-2"})," File"]})]})]}),(null==em?void 0:em.platform)==="whatsapp"&&(0,s.jsx)(i.z,{variant:"ghost",size:"icon",className:"h-8 w-8 flex-shrink-0 text-green-400 hover:text-green-600 hover:bg-green-50 dark:hover:bg-green-900/20",title:"Send Product Catalog",onClick:()=>{H.api.post("/conversations/".concat(em.id,"/catalog"),{conversation_id:em.id,body_text:"Check out our products"}).then(()=>{q.A.success("Catalog sent"),tO(em.id)}).catch(e=>{var t,a;return q.A.error((null===(a=e.response)||void 0===a?void 0:null===(t=a.data)||void 0===t?void 0:t.detail)||"Failed to send catalog")})},children:(0,s.jsx)(B.Z,{className:"w-4 h-4"})}),(0,s.jsx)(i.z,{variant:"ghost",size:"icon",className:"h-8 w-8 flex-shrink-0 text-purple-400 hover:text-purple-600 hover:bg-purple-50 dark:hover:bg-purple-900/20",onClick:async()=>{var e,t;let a=null===(e=ex[ex.length-1])||void 0===e?void 0:e.content;if(!a){q.A.error("No message to generate reply for");return}q.A.loading("Generating AI reply...");try{let e=await H.api.post("/conversations/indexer/auto-reply",{message:a,channel:null==em?void 0:em.platform,platform:null==em?void 0:em.platform});q.A.dismiss(),(null===(t=e.data)||void 0===t?void 0:t.reply)&&(eg(e.data.reply),q.A.success("AI reply generated"))}catch(e){q.A.dismiss(),q.A.error("Failed to generate AI reply")}},children:(0,s.jsx)(_.Z,{className:"w-4 h-4"})}),(0,s.jsx)(o.I,{ref:tF,placeholder:"facebook_comment"===em.platform&&ey?"Private message...":"Type a message...",value:eh,onChange:e=>{eg(e.target.value),em&&(!tL.current&&e.target.value.trim()&&(tL.current=!0,tR(em.id)),tT.current&&clearTimeout(tT.current),tT.current=setTimeout(()=>{tL.current&&em&&(tL.current=!1,tD(em.id))},2e3))},onKeyDown:e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),tL.current&&em&&(tL.current=!1,tD(em.id)),t4())},className:"flex-1 h-9 text-sm bg-white dark:bg-slate-800 border border-slate-200 dark:border-slate-700 rounded-full px-4"}),(0,s.jsx)(i.z,{onClick:t4,disabled:ek||!eh.trim()&&!eG,size:"icon",className:(0,G.cn)("h-9 w-9 rounded-full flex-shrink-0 text-white","whatsapp"===em.platform?"bg-[#075e54] hover:bg-[#064940]":"instagram"===em.platform?"bg-gradient-to-r from-purple-500 to-pink-500 hover:from-purple-600 hover:to-pink-600":"messenger"===em.platform?"bg-blue-500 hover:bg-blue-600":(null===(ea=em.platform)||void 0===ea?void 0:ea.includes("facebook"))?"bg-[#1877f2] hover:bg-[#166fe0]":(null===(es=em.platform)||void 0===es?void 0:es.includes("instagram"))?"bg-gradient-to-r from-purple-500 to-pink-500":"bg-emerald-500 hover:bg-emerald-600"),children:ek?(0,s.jsx)(v.Z,{className:"w-4 h-4 animate-spin"}):(0,s.jsx)(O.Z,{className:"w-4 h-4"})})]})]})]}):(0,s.jsxs)("div",{className:"flex-1 flex flex-col items-center justify-center p-8 bg-[#f0f2f5] dark:bg-slate-950",children:[(0,s.jsx)("div",{className:"w-14 h-14 roun
1ded-2xl bg-white dark:bg-slate-800 shadow-sm flex items-center justify-center mb-4 border border-slate-200 dark:border-slate-700",children:(0,s.jsx)(c.Z,{className:"w-6 h-6 text-emerald-500"})}),(0,s.jsx)("h2",{className:"text-lg font-semibold text-slate-800 dark:text-slate-200 mb-1",children:"Select a conversation"}),(0,s.jsx)("p",{className:"text-sm text-slate-400 text-center max-w-xs mb-5",children:"Choose from the sidebar or connect a new channel"}),(0,s.jsx)("div",{className:"flex items-center gap-3 mb-4",children:[{id:"facebook",label:"Facebook",icon:(0,s.jsx)(eM,{className:"w-3.5 h-3.5"}),bg:"bg-blue-50 dark:bg-blue-900/20",active:"text-blue-600"},{id:"instagram",label:"Instagram",icon:(0,s.jsx)(ez,{className:"w-3.5 h-3.5"}),bg:"bg-pink-50 dark:bg-pink-900/20",active:"text-pink-600"},{id:"whatsapp",label:"WhatsApp",icon:(0,s.jsx)(c.Z,{className:"w-3.5 h-3.5"}),bg:"bg-green-50 dark:bg-green-900/20",active:"text-green-600"}].map(e=>{var t,a;let l="whatsapp"===e.id?tA||(null===(t=tN.whatsapp)||void 0===t?void 0:t.isConnected)||tC.some(e=>"whatsapp"===e.platform):(null===(a=tN[e.id])||void 0===a?void 0:a.isConnected)||tC.some(t=>t.platform===e.id);return(0,s.jsxs)("div",{className:(0,G.cn)("flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs",l?(0,G.cn)(e.bg,e.active,"font-medium"):"bg-slate-100 dark:bg-slate-800 text-slate-400"),children:[(0,s.jsx)("div",{className:(0,G.cn)("w-1.5 h-1.5 rounded-full",l?"bg-green-500":"bg-slate-300 dark:bg-slate-600")}),e.label]},e.id)})}),(0,s.jsxs)(i.z,{variant:"outline",size:"sm",className:"text-xs",onClick:()=>tb(!0),children:[(0,s.jsx)(w.Z,{className:"w-3.5 h-3.5 mr-1.5"}),"Manage Channels"]})]})})]}),em&&(0,s.jsx)(ed,{open:eY,onOpenChange:e0,currentConversation:{id:em.id,contact_name:em.contact_name,last_message:em.last_message,last_message_at:em.last_message_at,platform:em.platform,avatar_url:em.avatar_url,lead_id:em.lead_id},allConversations:e5.map(e=>({id:e.id,contact_name:e.contact_name,last_message:e.last_message,last_message_at:e.last_message_at,platform:e.platform,avatar_url:e.avatar_url,lead_id:e.lead_id})),onMergeComplete:e=>{q.A.success("Merged ".concat(e.merged_count," conversation(s)")),tV()}}),er&&em&&(0,s.jsx)(eb,{open:e1,onOpenChange:e2,conversation:{id:em.id,contact_name:em.contact_name,platform:em.platform,raw_data:em.raw_data,meta_data:em.meta_data,instagram_username:em.instagram_username,external_platform_id:em.external_platform_id,lead_id:em.lead_id},onLeadCreated:e=>{q.A.success("Lead created and linked to conversation")}}),er&&(0,s.jsx)(eZ,{open:tr,onOpenChange:tn}),(0,s.jsx)(eE.E,{open:tv,onOpenChange:tb,onChanged:()=>tV()})]})}var eB=a(24746),eU=a(9310),eV=a(99801),eO=a(83578),eX=a(45886);let e$=(0,n.default)(()=>Promise.all([a.e(38846),a.e(38407),a.e(79044),a.e(24051)]).then(a.bind(a,24051)),{loadableGenerated:{webpack:()=>[24051]},ssr:!1,loading:()=>(0,s.jsx)("div",{className:"p-8 text-sm text-muted-foreground",children:"Loading Agentâ¦"})});function eq(){let{dashboardRole:e,isAdmin:t,loading:a}=(0,eB.DO)(),{user:n}=(0,eh.a)(),{endNavigation:o}=(0,eU.H)(),d=(0,r.useSearchParams)(),[c,m]=(0,l.useState)("conversations");(0,l.useEffect)(()=>{a||o()},[a,o]),(0,l.useEffect)(()=>{let e=d.get("tab");e&&"conversations"!==e&&m("agent")},[d]),(0,l.useEffect)(()=>{let e=d.get("success"),t=d.get("platform");if("true"===e&&t){let e=d.get("pages")||"0";q.A.success("".concat(t.charAt(0).toUpperCase()+t.slice(1)," connected â ").concat(e," page").concat("1"!==e?"s":""," linked")),window.history.replaceState({},"","/dashboard/inbox")}},[d]);let u=null==n?void 0:n.role;return a?(0,s.jsx)("div",{className:"flex items-center justify-center h-full bg-gradient-to-br from-green-50 to-emerald-50 dark:from-green-950/20 dark:to-emerald-950/20","data-loading":"true",children:(0,s.jsxs)("div",{className:"text-center",children:[(0,s.jsxs)("div",{className:"relative",children:[(0,s.jsx)("div",{className:"w-16 h-16 rounded-full bg-gradient-to-r from-green-500 to-emerald-600 flex items-center justify-center mx-auto mb-4 animate-pulse",children:(0,s.jsx)(eV.Z,{className:"w-8 h-8 text-white"})}),(0,s.jsx)("div",{className:"absolute inset-0 w-16 h-16 rounded-full bg-gradient-to-r from-green-500 to-emerald-600 mx-auto animate-ping opacity-20"})]}),(0,s.jsx)("p",{className:"text-muted-foreground font-medium",children:"Loading Customer Supportâ¦"})]})}):"SuperAdmin"===u||"Admin"===u||"admin"===e||"marketing"===e||t?(0,s.jsx)("div",{className:"h-[calc(100vh-4rem)] sm:h-[calc(100vh-3.5rem)] flex flex-col",children:(0,s.jsxs)(eu.Tabs,{value:c,onValueChange:e=>m(e),className:"flex-1 flex flex-col min-h-0",children:[(0,s.jsx)("div",{className:"border-b px-3 sm:px-4 py-2 bg-card flex items-center",children:(0,s.jsxs)("div",{className:"flex items-center gap-1 p-0.5 bg-slate-100 dar
1k:bg-slate-800 rounded-lg",children:[(0,s.jsxs)(i.z,{variant:"conversations"===c?"default":"ghost",size:"sm",onClick:()=>m("conversations"),className:"gap-1.5 rounded-md transition-all px-3 sm:px-4 ".concat("conversations"===c?"shadow-sm":"hover:bg-slate-200 dark:hover:bg-slate-700"),style:{height:"32px"},children:[(0,s.jsx)(eX.Z,{className:"h-3.5 w-3.5 sm:h-4 sm:w-4"}),(0,s.jsx)("span",{className:"hidden sm:inline text-xs sm:text-sm",children:"Conversations"})]}),(0,s.jsxs)(i.z,{variant:"agent"===c?"default":"ghost",size:"sm",onClick:()=>m("agent"),className:"gap-1.5 rounded-md transition-all px-3 sm:px-4 ".concat("agent"===c?"shadow-sm":"hover:bg-slate-200 dark:hover:bg-slate-700"),style:{height:"32px"},children:[(0,s.jsx)(T.Z,{className:"h-3.5 w-3.5 sm:h-4 sm:w-4"}),(0,s.jsx)("span",{className:"hidden sm:inline text-xs sm:text-sm",children:"Agent"})]})]})}),(0,s.jsx)(eu.TabsContent,{value:"conversations",className:"flex-1 min-h-0 m-0 data-[state=inactive]:hidden",children:(0,s.jsx)("div",{className:"h-full flex flex-col",children:(0,s.jsx)("div",{className:"flex-1 min-h-0",children:(0,s.jsx)(eI,{})})})}),(0,s.jsx)(eu.TabsContent,{value:"agent",className:"flex-1 min-h-0 m-0 overflow-y-auto data-[state=inactive]:hidden",children:(0,s.jsx)("div",{className:"min-h-full p-3 sm:p-4 md:p-6",children:(0,s.jsx)(e$,{})})})]})}):(0,s.jsxs)("div",{className:"flex flex-col items-center justify-center h-full space-y-6 p-8 bg-gradient-to-br from-slate-50 to-slate-100 dark:from-slate-900 dark:to-slate-950",children:[(0,s.jsx)("div",{className:"w-24 h-24 rounded-full bg-gradient-to-r from-red-100 to-orange-100 dark:from-red-900/30 dark:to-orange-900/30 flex items-center justify-center",children:(0,s.jsx)(eO.Z,{className:"h-12 w-12 text-red-500"})}),(0,s.jsxs)("div",{className:"text-center",children:[(0,s.jsx)("h1",{className:"text-2xl font-bold text-foreground mb-2",children:"Access Denied"}),(0,s.jsx)("p",{className:"text-muted-foreground text-center max-w-md",children:"You don't have permission to access Customer Support. This feature is only available to administrators and marketing team members."})]})]})}},68245:function(e,t,a){a.d(t,{X:function(){return o}});var s=a(57437),l=a(2265),r=a(9270),n=a(30401),i=a(93448);let o=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(r.fC,{ref:t,className:(0,i.cn)("peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",a),...l,children:(0,s.jsx)(r.z$,{className:(0,i.cn)("flex items-center justify-center text-current"),children:(0,s.jsx)(n.Z,{className:"h-4 w-4"})})})});o.displayName=r.fC.displayName},90641:function(e,t,a){a.d(t,{x:function(){return i}});var s=a(57437),l=a(2265),r=a(43643),n=a(93448);let i=l.forwardRef((e,t)=>{let{className:a,children:l,...i}=e;return(0,s.jsxs)(r.fC,{ref:t,className:(0,n.cn)("relative overflow-hidden",a),...i,children:[(0,s.jsx)(r.l_,{className:"h-full w-full rounded-[inherit]",children:l}),(0,s.jsx)(o,{}),(0,s.jsx)(r.Ns,{})]})});i.displayName=r.fC.displayName;let o=l.forwardRef((e,t)=>{let{className:a,orientation:l="vertical",...i}=e;return(0,s.jsx)(r.gb,{ref:t,orientation:l,forceMount:!0,className:(0,n.cn)("flex touch-none select-none transition-colors","vertical"===l&&"h-full w-2.5 border-l border-l-transparent p-[1px]","horizontal"===l&&"h-2.5 flex-col border-t border-t-transparent p-[1px]",a),...i,children:(0,s.jsx)(r.q4,{className:"relative flex-1 rounded-full bg-slate-300 dark:bg-slate-600 hover:bg-slate-400 dark:hover:bg-slate-500 transition-colors"})})});o.displayName=r.gb.displayName},9310:function(e,t,a){a.d(t,{H:function(){return o},K:function(){return i}});var s=a(57437),l=a(2265),r=a(99376);let n=(0,l.createContext)(void 0);function i(e){let{children:t}=e,[a,i]=(0,l.useState)(!1),o=(0,r.useRouter)(),d=(0,r.usePathname)(),c=(0,l.useRef)(d),m=(0,l.useRef)(null),u=(0,l.useCallback)(()=>{m.current&&(clearTimeout(m.current),m.current=null)},[]),x=(0,l.useCallback)(()=>{u(),i(!0),m.current=setTimeout(()=>{i(!1)},4e3)},[u]),p=(0,l.useCallback)(()=>{u(),i(!1)},[u]),h=(0,l.useCallback)(e=>{e!==d&&(x(),o.push(e))},[o,d,x]);return(0,l.useEffect)(()=>(d!==c.current&&(c.current=d),()=>u()),[d,u]),(0,s.jsx)(n.Provider,{value:{isNavigating:a,startNavigation:x,endNavigation:p,navigateTo:h},children:t})}function o(){let e=(0,l.useContext)(n);if(!e)throw Error("useNavigation must be used within a NavigationProvider");return e}},46034:function(e,t,a){a.d(t,{s:function(){return c},w:function(){return d}});
1var s=a(57437),l=a(2265),r=a(84881),n=a(40257);let i=(0,l.createContext)(void 0),o=n.env.NEXT_PUBLIC_WEBSOCKET_URL||"";function d(e){let{children:t}=e,{user:a,token:n}=(0,r.a)(),[d,c]=(0,l.useState)(null),[m,u]=(0,l.useState)(!1),[x,p]=(0,l.useState)(null),[h,g]=(0,l.useState)([]),[v,b]=(0,l.useState)([]),[f,j]=(0,l.useState)([]),_=(0,l.useRef)(0),w=(0,l.useRef)(null),k=(0,l.useRef)(null),N=(0,l.useRef)(!1),y=(0,l.useRef)(null),C=null==a?void 0:a.workspace_id,S=(0,l.useCallback)(()=>{if(!n||!a){console.log("[WebSocket] No token or user, skipping connection");return}if(N.current){console.log("[WebSocket] Already connecting, skipping");return}if(d&&d.readyState===WebSocket.OPEN&&y.current===n){console.log("[WebSocket] Already connected with same token, skipping");return}N.current=!0,y.current=n;let e=new URL("/api/ws",(o||"".concat("https:"===window.location.protocol?"wss:":"ws:","//").concat(window.location.host)).replace("http","ws"));e.searchParams.set("token",n),C&&e.searchParams.set("workspace_id",C),console.log("[WebSocket] Connecting to:",e.toString().replace(n,"***"));let t=new WebSocket(e.toString());t.onopen=()=>{console.log("[WebSocket] Connected"),u(!0),_.current=0,N.current=!1,w.current=setInterval(()=>{t.readyState===WebSocket.OPEN&&t.send(JSON.stringify({type:"ping"}))},25e3)},t.onmessage=e=>{try{let t=JSON.parse(e.data);A(t)}catch(e){console.error("[WebSocket] Error parsing message:",e)}},t.onclose=e=>{console.log("[WebSocket] Disconnected:",e.code,e.reason),u(!1),c(null),N.current=!1,w.current&&(clearInterval(w.current),w.current=null),1e3!==e.code&&_.current<5&&(_.current++,console.log("[WebSocket] Reconnecting (attempt ".concat(_.current,"/").concat(5,")...")),k.current=setTimeout(S,3e3))},t.onerror=e=>{console.error("[WebSocket] Error:",e),N.current=!1},c(t)},[n,a,C]),A=(0,l.useCallback)(e=>{switch(e.type){case"connection":console.log("[WebSocket] Connection confirmed:",e.status);break;case"pong":break;case"chat":let t={id:e.message_id||Date.now().toString(),room_id:e.conversation_id||x||"",user_id:e.sender_id,username:e.sender_name||"Unknown",message:e.content,timestamp:e.timestamp,type:e.is_ai?"ai":"text"};g(e=>[...e,t]);break;case"typing":e.is_typing?b(t=>t.find(t=>t.user_id===e.user_id)?t:[...t,{user_id:e.user_id,username:e.username||"Someone"}]):b(t=>t.filter(t=>t.user_id!==e.user_id));break;case"presence":"online"===e.status?j(t=>t.find(t=>t.user_id===e.user_id)?t.map(t=>t.user_id===e.user_id?{...t,status:e.status,current_room:e.current_room||null}:t):[...t,{user_id:e.user_id,username:e.username||"Unknown",status:e.status,current_room:e.current_room||null}]):"offline"===e.status&&j(t=>t.filter(t=>t.user_id!==e.user_id));break;case"new_message":console.log("[WebSocket] New message received:",e),window.dispatchEvent(new CustomEvent("ws:new_message",{detail:e}));break;case"conversation_update":console.log("[WebSocket] Conversation updated:",e),window.dispatchEvent(new CustomEvent("ws:conversation_update",{detail:e}));break;case"lead_update":console.log("[WebSocket] Lead updated:",e),window.dispatchEvent(new CustomEvent("ws:lead_update",{detail:e}));break;case"notification":console.log("[WebSocket] Notification received:",e),window.dispatchEvent(new CustomEvent("ws:notification",{detail:e}));break;case"agent_response":console.log("[WebSocket] Agent response:",e),window.dispatchEvent(new CustomEvent("ws:agent_response",{detail:e}));break;case"call_status_update":console.log("[WebSocket] Call status update:",e),window.dispatchEvent(new CustomEvent("ws:call_status_update",{detail:e}));break;case"messages_read":console.log("[WebSocket] Messages read:",e),window.dispatchEvent(new CustomEvent("ws:messages_read",{detail:e}));break;case"typing_indicator":console.log("[WebSocket] Typing indicator:",e),window.dispatchEvent(new CustomEvent("ws:typing_indicator",{detail:e}));break;case"agent_alert":console.log("[WebSocket] Agent alert:",e),window.dispatchEvent(new CustomEvent("ws:agent_alert",{detail:e}));break;case"dashboard_event":var a;"agent_alert"===e.event_type?(console.log("[WebSocket] Dashboard agent alert:",e),window.dispatchEvent(new CustomEvent("ws:agent_alert",{detail:e.data}))):"lead_updated"===e.event_type&&(null===(a=e.data)||void 0===a?void 0:a.ai_first_call_status)?(console.log("[WebSocket] Lead AI call status update:",e),window.dispatchEvent(new CustomEvent("ws:call_status_update",{detail:{type:"call_status_update",lead_id:e.data.lead_id,status:e.data.ai_first_call_status,call_type:"ai_call",...e.data}}))):window.dispatchEvent(new CustomEvent("ws:dashboard_event",{detail:e}));break;case"error":console.error("[WebSocket] Server error:",e.error);break;default:console.log("[WebSocket] Unknown message type:",e.type,e)}},[x]),Z=null==a?void 0:a.id;(0,l.useEffect)(()=>(Z&&n&&S(),()=>{w.current&&clearInterval(w.current),k.current&&clearTimeout(k.current)}),[Z,n,S]),(0,l.useEffect)(()=>()=>{d&&d.close(1e3,"Component unmounting")},[d]);let E=(0,l.useCallback)(e=>{d&&d.readyState===WebSocket.OPEN?d.send(JSON.stringify(e)):console.warn("[WebSocket] Cannot send message, not connected")},[d]),F=(0,l.useCallback)(e=>{p(e),E({type:"presence",status:"online",current_room:e}),console.log("[WebSocket] Joined room:",e)},[E]),M=(0,l.useCallback)(e=>{x===e&&(p(null),E({type:"presence",status:"online",current_room:null}),console.log("[WebSocket] Left room:",e))},[x,E]),z=(0,l.useCallback)(function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"text";if(!x){console.warn("[WebSocket] No room joined, cannot send message");return}E({type:"chat",content:e,message_type:t,workspace_id:C,conversation_id:x})},[x,C,E]),T=(0,l.useCallback)((e,t,a)=>{E({type:"chat",content:e,conversation_id:t,recipient_id:a,workspace_id:C})},[C,E]),L=(0,l.useCallback)(e=>{E({type:"typing",is_typing:!0,conversation_id:e||x,workspace_id:C})},[x,C,E]),P=(0,l.useCallback)(e=>{E({type:"typing",is_typing:!1,conversation_id:e||x,workspace_id:C})},[x,C,E]);return(0,s.jsx)(i.Provider,{value:{socket:d,connected:m,currentRoom:x,messages:h,typingUsers:v,onlineUsers:f,joinRoom:F,leaveRoom:M,sendMessage:z,sendChatMessage:T,startTyping:L,stopTyping:P,sendCustomMessage:E},children:t})}function c(){let e=(0,l.useContext)(i);if(void 0===e)throw Error("useSocket must be used within a SocketProvider");return e}},24746:function(e,t,a){a.d(t,{DO:function(){return n}});var s=a(2265),l=a(71837);let r={dashboardRole:"viewer",systemRole:"Customer",enterpriseRole:"staff",enterpriseTier:4,agentGroups:[],permissions:[],dashboardPermissions:[],canAssignLeads:!1,canMoveLeads:!1,canConvertLeads:!1,canDeleteLeads:!1,canManageTeam:!1,canViewReports:!1,canManageCampaigns:!1,canManageBilling:!1,canManageAgentGroups:!1,isAdmin:!1,isManager:!1,isSuperAdmin:!1,loading:!0,error:null,hasPermission:()=>!1,hasAnyPermission:()=>!1,hasAllPermissions:()=>!1,hasMinimumTier:()=>!1,refresh:async()=>{}};
1function n(){let[e,t]=(0,s.useState)({...r,loading:!1}),a=(0,s.useCallback)(async()=>{let e=localStorage.getItem("access_token");if(console.log("[useRolePermissions] Token found:",!!e),!e){console.log("[useRolePermissions] No token, using default permissions"),t(e=>({...e,loading:!1}));return}try{t(e=>({...e,loading:!0,error:null}));let e=(await l.api.get("/auth/me/dashboard-role")).data;console.log("[useRolePermissions] API response:",e);let s=e.permissions||[],r=e.enterprise_tier||4,n={dashboardRole:e.dashboard_role||"viewer",systemRole:e.system_role||"Customer",enterpriseRole:e.enterprise_role||"staff",enterpriseTier:r,agentGroups:e.agent_groups||[],permissions:s,dashboardPermissions:e.dashboard_permissions||[],canAssignLeads:e.can_assign_leads||!1,canMoveLeads:e.can_move_leads||!1,canConvertLeads:e.can_convert_leads||!1,canDeleteLeads:e.can_delete_leads||!1,canManageTeam:e.can_manage_team||!1,canViewReports:e.can_view_reports||!1,canManageCampaigns:e.can_manage_campaigns||!1,canManageBilling:e.can_manage_billing||!1,canManageAgentGroups:e.can_manage_agent_groups||!1,isAdmin:e.is_admin||!1,isManager:e.is_manager||!1,isSuperAdmin:e.is_super_admin||!1,loading:!1,error:null,hasPermission:e=>s.includes(e),hasAnyPermission:function(){for(var e=arguments.length,t=Array(e),a=0;a<e;a++)t[a]=arguments[a];return t.some(e=>s.includes(e))},hasAllPermissions:function(){for(var e=arguments.length,t=Array(e),a=0;a<e;a++)t[a]=arguments[a];return t.every(e=>s.includes(e))},hasMinimumTier:e=>r<=e,refresh:a};t(n)}catch(e){console.error("Failed to fetch role permissions:",e),t(t=>({...t,loading:!1,error:e.message||"Failed to fetch permissions"}))}},[]);return(0,s.useEffect)(()=>{a()},[a]),e}}}]);
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.