1import{J as Ie,C as Ee,r as i,aj as r,p as e,L as p,T as h,w as j,bh as S,B as x,ag as g,S as y,b2 as U,af as B,v as Pe,f0 as ke,bD as _e,bE as De,bj as C}from"./index-C9IH3k-2.js";import{a1 as Le,a7 as Oe,a8 as Ne,aE as Me,T as H,g as qe}from"./TemplateUtil-D1DzmWLm.js";import{E as We,a as G,M as Ve,O as Y,b as Ue,A as $,F as Be,c as He,d as Ge,R as z,U as Ye,C as $e,e as J,P as ze,f as Je,D as Qe,S as Xe}from"./AutomationService-B660_zi5.js";import{u as Ze}from"./HelperMethods-C-ewwU_x.js";import{P as Ke}from"./PageDescription-BrOUsEZE.js";import{F as et}from"./Table-isZ0WXp4.js";import{R as Q}from"./MinusCircleOutlined-616tjIKH.js";import{S as tt}from"./index-8jSYgZzf.js";import{P as st}from"./index-C0dhduHx.js";import"./Wrapper-ChjuDa3w.js";import"./index-BOwvFOZJ.js";import"./Pagination-D1z-xPUv.js";import"./HolderOutlined-YOHjCGKZ.js";const at="manage_chatbots",X=(I,u)=>I.find(b=>b.value===u)?.label||u,yt=()=>{const u=Ze()(at),{token:b}=Ie.useToken(),Z=Ee(),[K,E]=i.useState([]),[ee,P]=i.useState(!1),[k,_]=i.useState(!1),[te,D]=i.useState(null),[se,L]=i.useState(null),[ae,f]=i.useState(!1),[A,O]=i.useState(!1),[ne,N]=i.useState(null),[d]=r.useForm(),[T,M]=i.useState(!1),[re,oe]=i.useState([]),[le,ie]=i.useState([]),[ce,de]=i.useState([]),[ue,he]=i.useState([]),[q,pe]=i.useState([]),v=r.useWatch("eventType",d),W=r.useWatch("conditionRules",d)||[],me=r.useWatch("actions",d)||[],R=i.useMemo(()=>We[v]||[],[v]),w=async()=>{try{P(!0);const t=await Be();E(Array.isArray(t?.data)?t.data:[])}catch(t){console.error("Failed to fetch automation rules:",t),E([])}finally{P(!1)}},xe=async()=>{try{M(!0);const[t,s,a,n,o]=await Promise.allSettled([Oe({}),Ne({}),Me({}),He(""),Ge()]);oe(t.value?.data?.results||[]),ie(s.value?.data?.results||[]),de(a.value?.data||[]),he(n.value?.data||[]),pe(Array.isArray(o.value?.data)?o.value.data:[])}catch(t){console.error("Failed to fetch action dropdown data:",t)}finally{M(!1)}};i.useEffect(()=>{w(),xe()},[]);const ge=()=>{O(!1),N(null),d.resetFields(),d.setFieldsValue({match:"AND",priority:1,conditionRules:[],actions:[{type:void 0,params:{}}]}),f(!0)},ye=t=>{O(!0),N(t.id),d.resetFields(),d.setFieldsValue({name:t.name,description:t.description,eventType:t.eventType,priority:t.priority??1,match:t.conditions?.match||"AND",conditionRules:t.conditions?.rules||[],actions:(t.actions||[]).map(s=>({type:s.type,params:s.params||{}}))}),f(!0)},be=t=>{const s=(t.conditionRules||[]).filter(n=>n&&n.field&&n.operator).map(n=>{const o={field:n.field,operator:n.operator};return!J.includes(n.operator)&&n.value!==void 0&&n.value!==""&&(o.value=n.value),Y.includes(n.operator)&&n.value2!==void 0&&n.value2!==""&&(o.value2=n.value2),o}),a=(t.actions||[]).filter(n=>n&&n.type).map(n=>{const o={};return Object.entries(n.params||{}).forEach(([m,c])=>{c==null||c===""||Array.isArray(c)&&c.length===0||(o[m]=c)}),{type:n.type,params:o}});return{name:t.name,description:t.description,eventType:t.eventType,priority:Number(t.priority??0),conditions:{match:t.match||"AND",rules:s},actions:a}},je=t=>t.length?t.some(a=>a.type==="schedule_whatsapp_message"&&Xe.filter(n=>a.params[n]!==void 0).length!==1)?"Schedule WhatsApp Message needs exactly one of: delay, exact date-time, or cron expression.":null:"Add at least one action.",fe=async t=>{const s=be(t),a=je(s.actions);if(a){C.error({title:"Error",description:a});return}try{_(!0),(A?await Ye(ne,s):await $e(s))&&(C.success({title:"Success",description:`Rule ${A?"updated":"created"} successfully.`}),f(!1),w())}catch(n){console.error("Failed to save automation rule:",n)}finally{_(!1)}},ve=async t=>{try{D(t),await Qe(t)&&(C.success({title:"Success",description:"Rule deleted successfully."}),w())}catch(s){console.error("Failed to delete automation rule:",s)}finally{D(null)}},Se=async(t,s)=>{try{L(t.id),(s?await ze(t.id):await Je(t.id))&&(C.success({title:"Success",description:s?"Rule published â it will now run on live events.":"Rule moved back to draft â it no longer runs on live events, and can be tested again."}),w())}catch(a){console.error("Failed to change automation rule publish state:",a)}finally{L(null)}}
1,Te=[{title:"Name",dataIndex:"name",key:"name",sorter:(t,s)=>(t.name||"").localeCompare(s.name||"")},{title:"Event",dataIndex:"eventType",key:"eventType",render:t=>e.jsx(H,{color:"geekblue",children:X(G,t)})},{title:"Conditions",key:"conditions",render:(t,s)=>e.jsxs(h.Text,{type:"secondary",children:[s.conditions?.rules?.length||0," condition(s)"]})},{title:"Actions",key:"actionsList",render:(t,s)=>e.jsx(p,{gap:"4px 0",wrap:!0,children:(s.actions||[]).map(a=>e.jsx(H,{color:"purple",children:X($,a.type)},`${s.id}-${a.type}`))})},{title:"Priority",dataIndex:"priority",key:"priority",sorter:(t,s)=>(t.priority||0)-(s.priority||0)},{title:"Active",dataIndex:"status",key:"status",sorter:(t,s)=>String(t.status||"").localeCompare(String(s.status||"")),render:(t,s)=>{const a=s.status===z.DRAFT;return e.jsx(j,{title:u?a?"Draft â switch on to publish and start running on live events":"Published â switch off to move back to draft and stop running":S,children:e.jsx(tt,{checked:!a,loading:se===s.id,disabled:!u,onChange:n=>Se(s,n)})})}},{title:"Created",dataIndex:"creationTime",key:"creationTime",sorter:(t,s)=>new Date(t.creationTime)-new Date(s.creationTime),defaultSortOrder:"descend",render:t=>e.jsx(h.Text,{children:t?qe(t):"-"})},{title:"Manage",key:"manage",render:(t,s)=>{const a=s.status===z.DRAFT;return e.jsxs(p,{gap:"small",children:[e.jsx(j,{title:u?a?"Test against a live or saved event":"Only draft rules can be tested":S,children:e.jsx(x,{icon:e.jsx(ke,{}),disabled:!u||!a,onClick:()=>Z(`/Automation/EventsActions/${s.id}/test`)})}),e.jsx(j,{title:u?"":S,children:e.jsx(x,{onClick:()=>ye(s),disabled:!u,children:e.jsx(_e,{})})}),e.jsx(st,{title:"Delete this rule?",description:"This action cannot be undone.",onConfirm:()=>ve(s.id),okText:"Yes, delete",cancelText:"Cancel",children:e.jsx(j,{title:u?"":S,children:e.jsx(x,{danger:!0,loading:te===s.id,disabled:!u,children:e.jsx(De,{})})})})]})}}],V=(t,s)=>q.find(a=>a.eventType===t&&a.actionType===s)?.params||{},we=(t,s)=>{const a=d.getFieldValue("actions")||[],n={...V(v,s)};d.setFieldsValue({actions:a.map((o,m)=>m===t?{...o,params:n}:o)})},Fe=(t,s)=>q.filter(a=>a.actionType===t).map(a=>a.params?.[s]).filter(Boolean),Ce=t=>{const s=d.getFieldValue("actions")||[];s.length&&d.setFieldsValue({actions:s.map(a=>{if(!a?.type)return a;const n={...a.params||{}};return Object.entries(V(t,a.type)).forEach(([o,m])=>{const c=n[o];(c===void 0||c===""||Fe(a.type,o).includes(c))&&(n[o]=m)}),{...a,params:n}})})},Ae=t=>{Ce(t),d.setFieldValue("conditionRules",[])},Re=t=>{const s=me[t.name]?.type,a=l=>[t.name,"params",l],n=e.jsx(r.Item,{name:a("preComposedMessageId"),label:"Canned Message",rules:[{required:!0,message:"Select a canned message"}],children:e.jsx(y,{showSearch:!0,loading:T,placeholder:"Select canned message",optionFilterProp:"label",options:ce.map(l=>({value:l.id,label:l.name||l.id}))})}),o=e.jsx(r.Item,{name:a("receiverNumber"),label:"Send message to",tooltip:"This is prefilled with the contact who triggered the rule. You can replace it with another phone number or token.",rules:[{required:!0,message:"Recipient is required"}],children:e.jsx(g,{placeholder:"The contact who triggered this rule"})}),m=e.jsx(r.Item,{name:a("templateVariables"),label:"Message values",tooltip:"Add one value for each placeholder in the saved message, in the same order.",children:e.jsx(y,{mode:"tags",placeholder:"Add a value, then press Enter",tokenSeparators:[","],open:!1})}),c=e.jsx(r.Item,{name:a("receiverNumber"),label:"Contact to update",tooltip:"This is prefilled with the contact who triggered the rule. You can replace it with another phone number or token.",rules:[{required:!0,message:"Contact phone is required"}],children:e.jsx(g,{placeholder:"The contact who triggered this rule"})});switch(s){case"execute_chatbot":return e.jsxs(e.Fragment,{children:[e.jsx(r.Item,{name:a("chatbotId"),label:"Choose chatbot",rules:[{required:!0,message:"Select a chatbot"}],children:e.jsx(y,{showSearch:!0,loading:T,placeholder:"Choose a chatbot to start",optionFilterProp:"label",options:ue.map(l=>({value:l.id,label:l.name||l.id}))})}),e.jsx(r.Item,{name:a("receiverNumber"),label:"Start the chatbot for",tooltip:"This is prefilled with the contact who triggered the rule. You can replace it with another phone number or token. Leave empty to use the chatbot's own trigger settings.",children:e.jsx(g,{placeholder:"The contact who triggered this rule"})})]});case"send_whatsapp_message":return e.jsxs(e.Fragment,{children:[o,n,m]});case"schedule_whatsapp_message":return e.jsxs(e.Fragment,{children:[o,n,m,e.jsx(h.Paragraph,{type:"secondary",children:"Choose one way to schedule this message."}),e.jsx(r.Item,{label:"Wait before sending (minutes)",name:a("delayInMinutes"),tooltip:"
1Choose only one scheduling option.",children:e.jsx(U,{min:1,style:{width:"100%"},placeholder:"e.g. 30"})}),e.jsx(r.Item,{label:"Send at a specific date and time",name:a("exactDateTime"),tooltip:"Choose only one scheduling option. Use ISO format, for example 2026-08-20T10:00:00.",children:e.jsx(g,{placeholder:"For example: 2026-08-20T10:00:00"})}),e.jsx(r.Item,{label:"Repeat using a cron expression",name:a("cronExpression"),tooltip:"Choose only one scheduling option.",children:e.jsx(g,{placeholder:"For example: 0 0 10 * * ?"})})]});case"set_contact_attribute":return e.jsxs(e.Fragment,{children:[c,e.jsx(r.Item,{name:a("attributeId"),label:"Attribute to update",rules:[{required:!0,message:"Select an attribute"}],children:e.jsx(y,{showSearch:!0,loading:T,placeholder:"Select attribute",optionFilterProp:"label",options:le.map(l=>({value:l.id,label:l.name}))})}),e.jsx(r.Item,{name:a("attributeValue"),label:"New value",rules:[{required:!0,message:"Value is required"}],children:e.jsx(g,{placeholder:"Enter the value to save"})})]});case"set_contact_label":return e.jsxs(e.Fragment,{children:[c,e.jsx(r.Item,{name:a("labelId"),label:"Label to add",rules:[{required:!0,message:"Select a label"}],children:e.jsx(y,{showSearch:!0,loading:T,placeholder:"Select label",optionFilterProp:"label",options:re.map(l=>({value:l.id,label:l.name}))})})]});default:return null}};return e.jsxs(p,{vertical:!0,gap:"large",children:[e.jsxs(p,{justify:"space-between",wrap:!0,children:[e.jsx(h.Title,{level:4,style:{margin:0},children:"Events and Actions"}),e.jsx(j,{title:u?"":S,children:e.jsx(x,{type:"primary",onClick:ge,disabled:!u,children:"Create Rule"})})]}),e.jsx(Ke,{description:"How automation rules work",children:"Choose the event that starts this rule, add optional conditions to narrow down when it runs, then select what should happen. For example: when an incoming message contains âpricingâ, send a saved pricing message. Lower priority numbers run first when more than one rule matches."}),e.jsx(et,{rowKey:"id",loading:ee,columns:Te,dataSource:K,scroll:{x:"max-content"}}),e.jsx(Le,{title:A?"Edit Rule":"Create Rule",size:"large",open:ae,onClose:()=>f(!1),destroyOnHidden:!0,extra:e.jsxs(Pe,{children:[e.jsx(x,{onClick:()=>f(!1),disabled:k,children:"Cancel"}),e.jsx(x,{type:"primary",loading:k,onClick:()=>d.submit(),children:"Save"})]}),children:e.jsxs(r,{form:d,layout:"vertical",onFinish:fe,children:[e.jsx(r.Item,{name:"name",label:"Rule Name",rules:[{required:!0,message:"Name is required"}],children:e.jsx(g,{placeholder:"e.g. Tag order enquiries"})}),e.jsx(r.Item,{name:"description",label:"Description",children:e.jsx(g.TextArea,{rows:2,placeholder:"Optional description"})}),e.jsxs(p,{gap:"middle",wrap:!0,children:[e.jsx(r.Item,{name:"eventType",label:"When should this rule start?",rules:[{required:!0,message:"Select an event"}],style:{flex:"1 1 260px"},children:e.jsx(y,{placeholder:"Choose the event that starts this rule",options:G,optionRender:t=>e.jsxs(p,{vertical:!0,children:[e.jsx(h.Text,{children:t.data.label}),e.jsx(h.Text,{type:"secondary",style:{fontSize:12},children:t.data.description})]}),onChange:Ae})}),e.jsx(r.Item,{name:"priority",label:"Priority",tooltip:"Lower runs first",style:{width:120},children:e.jsx(U,{min:0,style:{width:"100%"}})})]}),e.jsx(h.Title,{level:5,children:"Only run this rule whenâ¦"}),e.jsx(h.Paragraph,{type:"secondary",children:"Add conditions only when the rule should run for specific event data. Leave this section empty to run every time."}),e.jsx(r.Item,{name:"match",label:"When there is more than one condition",tooltip:"Choose whether every condition or just one condition must match.",children:e.jsx(y,{options:Ve,style:{maxWidth:260}})}),e.jsx(r.List,{name:"conditionRules",children:(t,{add:s,remove:a})=>e.jsxs(p,{vertical:!0,gap:"small",children:[t.map(n=>{const o=W[n.name]?.operator,m=R.find(F=>F.value===W[n.name]?.field),c=J.includes(o),l=Y.includes(o);return e.jsxs(p,{gap:"small",align:"start",wrap:!0,style:{border:`1px solid ${b.colorBorderSecondary}`,borderRadius:b.borderRadiusLG,padding:12},children:[e.jsx(r.Item,{name:[n.name,"field"],label:"What should we check?",style:{flex:"1 1 240px",marginBottom:8},rules:[{required:!0,message:"Choose what to check"}],children:e.jsx(y,{showSearch:!0,placeholder:"Choose event data",optionFilterProp:"label",options:R,optionRender:F=>e.jsxs(p,{vertical:!0,children:[e.jsx(h.Text,{children:F.data.label}),e.jsx(h.Text,{type:"secondary",style:{fontSize:12},children:F.data.description})]})})}),e.jsx(r.Item,{name:[n.name,"operator"],label:"How should it match?",style:{flex:"1 1 190px",marginBottom:8}
1,rules:[{required:!0,message:"Choose how it should match"}],children:e.jsx(y,{placeholder:"Choose a condition",options:Ue})}),!c&&e.jsx(r.Item,{name:[n.name,"value"],label:l?"From":"Value to match",style:{flex:"1 1 180px",marginBottom:8},children:e.jsx(g,{placeholder:l?"For example: 10":m?.placeholder||"Enter a value"})}),l&&e.jsx(r.Item,{name:[n.name,"value2"],label:"To",style:{flex:"1 1 140px",marginBottom:8},children:e.jsx(g,{placeholder:"For example: 20"})}),e.jsx(x,{type:"text",danger:!0,"aria-label":"Remove condition",icon:e.jsx(Q,{}),onClick:()=>a(n.name)})]},n.key)}),e.jsx(j,{title:v?"Add a condition":"Choose an event before adding a condition",children:e.jsx(x,{type:"dashed",style:{alignSelf:"flex-start"},disabled:!v,onClick:()=>s({field:R[0]?.value,operator:"contains"}),icon:e.jsx(B,{}),children:"Add condition"})})]})}),e.jsx(h.Title,{level:5,style:{marginTop:20},children:"Then do this"}),e.jsx(r.List,{name:"actions",children:(t,{add:s,remove:a})=>e.jsxs(p,{vertical:!0,gap:"middle",children:[t.map(n=>e.jsxs("div",{style:{border:`1px solid ${b.colorBorder}`,borderRadius:b.borderRadiusLG,padding:12},children:[e.jsxs(p,{justify:"space-between",align:"center",gap:"small",children:[e.jsx(r.Item,{name:[n.name,"type"],label:"What should happen?",style:{flex:1,marginBottom:12},rules:[{required:!0,message:"Select an action"}],children:e.jsx(y,{placeholder:"Choose what should happen",options:$,optionRender:o=>e.jsxs(p,{vertical:!0,children:[e.jsx(h.Text,{children:o.data.label}),e.jsx(h.Text,{type:"secondary",style:{fontSize:12},children:o.data.description})]}),onChange:o=>we(n.name,o)})}),e.jsx(x,{type:"text",danger:!0,icon:e.jsx(Q,{}),onClick:()=>a(n.name)})]}),Re(n)]},n.key)),e.jsx(x,{type:"dashed",style:{alignSelf:"flex-start"},onClick:()=>s({type:void 0,params:{}}),icon:e.jsx(B,{}),children:"Add action"})]})})]})})]})};export{yt as default};
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.