import{b as T,i as s}from"./useflows-BpG3C9m1.js";import{l as N,e as o}from"./main-few-lands-burn.js";import{s as _}from"./machine.-BRuqPpdF.js";import{h,a as C,b as A,c as i,d as I,r as L}from"./whatsapp-lSZheXFS.js";import"./mutative-C6xjlr6O.js";import"./lodash-YpNkAOPN.js";const f=(n,e,a)=>N({headers:{Authorization:["Bearer ",{encryption:"hmac_decrypt",value:n}]},method:"GET",url:`https://graph.facebook.com/v19.0/${e}/message_templates?limit=10&after=${a}`});let d="";const l=async(n,e=0)=>{n.setComponent("template-name",t=>{t.loading=!0});const a=await n.getConnection();if(!a)return;const p=a.auth_details?.extraData?.businessId;e===0&&(d="");const{data:m,status:r}=await f(a.auth_details.token,p,d);if(r!=="success"){n.setComponent("template-name",t=>{n.util.isSelectComponent(t)&&(t.loading=!1,t.status=r,t.invalidMessage=m)}),console.error("Failed to fetch list");return}d=m.paging.cursors.after,m.data.length===0&&n.setComponent("template-name",t=>{n.util.isSelectComponent(t)&&(t.isLoadable=!1)});const c=m.data.map(t=>{const u=t.components.find(E=>E.format)?.format;return{label:`${t.name} (${t.language})`,value:`${t.name} ${t.language} ${u}`}});n.setComponent("template-name",t=>{n.util.isSelectComponent(t)&&(t.options=e===0?c:t.options?.concat(c),t.loading=!1)})},x=T(({helpers:n})=>({states:{components:[I(n),L(n),{componentName:n.componentName.select,helperText:o("Select the name of the message template you want to send as message."),id:"template-name",label:o("Select Template Name"),onChange:"SET_TEMPLATE_NAME",onLoadData:"ON_LOAD_CHANNEL_DATA",onRefetch:"REFETCH_TEMPLATE",options:[],path:"template.name",render:"IF_CONNECTION_SELECTED",required:!0,showSearch:!0,value:void 0},{componentName:n.componentName.mixInput,helperText:o("Enter the direct link to the media (image, video or document) you want to send. Must be publicly accessible. Ex: https://example.com/file.jpg"),id:"media-url",label:o("Media URL"),onChange:"SET_MEDIA_URL",path:"link",render:"IF_MEDIA_TEMPLATE",required:!0,value:[]},{componentName:n.componentName.mixInput,helperText:o("Enter the latitude of the location in decimal format. Ex: 23.810331"),id:"latitude",label:o("Latitude"),onChange:"SET_LATITUDE",path:"location.latitude",render:"IF_LOCATION_TEMPLATE",required:!0,value:[]},{componentName:n.componentName.mixInput,helperText:o("Enter the longitude of the location in decimal format. Ex: 90.412521"),id:"longitude",label:o("Longitude"),onChange:"SET_LONGITUDE",path:"location.longitude",render:"IF_LOCATION_TEMPLATE",required:!0,value:[]},{componentName:n.componentName.mixInput,helperText:o("Enter the name of the location. Ex: Dhaka City"),id:"location-name",label:o("Name"),onChange:"SET_LOCATION_NAME",path:"location.name",render:"IF_LOCATION_TEMPLATE",required:!0,value:[]},{componentName:n.componentName.mixInput,helperText:o("Enter the full address of the location. Ex: Dhaka, Bangladesh"),id:"address",label:o("Address"),onChange:"SET_ADDRESS",path:"location.address",render:"IF_LOCATION_TEMPLATE",required:!0,value:[]}]},actions:{CONNECTION_ADD_CHANGE:A,HANDLE_BUSINESS_ID:C,IF_CONNECTION_SELECTED:({$:e})=>s(e.getComponent("connection-id")?.value),ON_LOAD_CHANNEL_DATA:async({$:e,e:a})=>await l(e,a.count),SET_ADDRESS:i("address"),SET_LATITUDE:i("latitude"),SET_LOCATION_NAME:i("locationName"),SET_LONGITUDE:i("longitude"),SET_MEDIA_URL:i("mediaLink"),SET_PHONE_NUMBER_ID:h,SET_RECIPIENT_NUMBER:i("recipientNumber"),SET_TEMPLATE_NAME:i("templateName"),IF_MEDIA_TEMPLATE:({$:e})=>{const a=e.getComponent("template-name")?.value?.split(" ").pop();return s(e.getComponent("connection-id")?.value)&&a==="IMAGE"||a==="VIDEO"||a==="DOCUMENT"},IF_LOCATION_TEMPLATE:({$:e})=>{const a=e.getComponent("template-name")?.value?.split(" ").pop();return s(e.getComponent("connection-id")?.value)&&a==="LOCATION"},SET_CONNECTION:async({$:e,e:a})=>{i("connectionId")({$:e,e:a}),await l(e)},REFETCH_TEMPLATE:async({$:e})=>{await l(e),e.setComponent("template-name",a=>{e.util.isSelectComponent(a)&&(a.isLoadable=!0)})},ON_MACHINE_LOAD:async({$:e})=>{_(e,[{db:"connectionId",id:"connection-id"},{db:"recipientNumber",id:"recipient-phone-number"},{db:"templateName",id:"template-name"},{db:"mediaLink",id:"media-url"},{db:"longitude",id:"longitude"},{db:"latitude",id:"latitude"},{db:"locationName",id:"location-name"},{db:"address",id:"address"}]),e.db?.connectionId&&await l(e)}}}));export{x as default};
