@@ -123,6 +123,13 @@ const bodyHasVariables = (template: WhatsAppMessageTemplate): boolean => {
123123 return body ? body . text . match ( VARIABLE_REGEX ) !== null : false ;
124124} ;
125125
126+ const textHeaderVariables = ( template : WhatsAppMessageTemplate ) : string [ ] => {
127+ const header = findComponentByType ( template , 'HEADER' ) ;
128+ if ( header ?. format !== 'TEXT' || ! header . text ) return [ ] ;
129+
130+ return extractVariables ( header . text ) ;
131+ } ;
132+
126133// Collects the URL/COPY_CODE buttons that require a user-supplied parameter,
127134// preserving their positional index (sparse array).
128135const buildButtonParams = (
@@ -158,22 +165,20 @@ const buildButtonParams = (
158165} ;
159166
160167/**
161- * Builds the empty WhatsApp processed_params scaffold for a template: body keys,
162- * a media header block, and the button parameters that need filling.
168+ * Builds the empty WhatsApp processed_params scaffold for a template: text or
169+ * media header values, body keys, and button parameters that need filling.
163170 */
164171export const buildWhatsAppProcessedParams = (
165172 template : WhatsAppMessageTemplate
166173) : WhatsAppProcessedParams => {
167174 const params : WhatsAppProcessedParams = { } ;
168175
169176 const body = findComponentByType ( template , 'BODY' ) ;
170- if ( ! body ) return params ;
171-
172- const matchedVariables = body . text . match ( VARIABLE_REGEX ) ;
173- if ( matchedVariables ) {
177+ const bodyVariables = body ? extractVariables ( body . text ) : [ ] ;
178+ if ( bodyVariables . length > 0 ) {
174179 const bodyParams : Record < string , string > = { } ;
175- matchedVariables . forEach ( variable => {
176- bodyParams [ processVariable ( variable ) ] = '' ;
180+ bodyVariables . forEach ( variable => {
181+ bodyParams [ variable ] = '' ;
177182 } ) ;
178183 params . body = bodyParams ;
179184 }
@@ -182,6 +187,14 @@ export const buildWhatsAppProcessedParams = (
182187 const format = getMediaType ( template ) ;
183188 params . header = { media_url : '' , media_type : format } ;
184189 if ( format === 'document' ) params . header . media_name = '' ;
190+ } else {
191+ const headerVariables = textHeaderVariables ( template ) ;
192+ if ( headerVariables . length > 0 ) {
193+ params . header = { } ;
194+ headerVariables . forEach ( variable => {
195+ params . header ! [ variable ] = '' ;
196+ } ) ;
197+ }
185198 }
186199
187200 const buttons = buildButtonParams ( template ) ;
@@ -191,22 +204,27 @@ export const buildWhatsAppProcessedParams = (
191204} ;
192205
193206/**
194- * Whether every required WhatsApp input has been filled. A template with no body
195- * variables and no media header is considered complete even if it has buttons
196- * (mirrors the composer's validation).
207+ * Whether every required WhatsApp input has been filled. A template with no
208+ * text variables and no media header is considered complete even if it has
209+ * buttons (mirrors the composer's validation).
197210 */
198211export const isWhatsAppComplete = (
199212 template : WhatsAppMessageTemplate ,
200213 processedParams : WhatsAppProcessedParams
201214) : boolean => {
202- const hasVariables = bodyHasVariables ( template ) ;
215+ const hasBodyVariables = bodyHasVariables ( template ) ;
216+ const headerVariables = textHeaderVariables ( template ) ;
203217 const media = hasMediaHeader ( template ) ;
204218
205- if ( ! hasVariables && ! media ) return true ;
219+ if ( ! hasBodyVariables && headerVariables . length === 0 && ! media ) return true ;
206220
207221 if ( media && ! processedParams . header ?. media_url ) return false ;
208222
209- if ( hasVariables && processedParams . body ) {
223+ if ( headerVariables . some ( variable => ! processedParams . header ?. [ variable ] ) ) {
224+ return false ;
225+ }
226+
227+ if ( hasBodyVariables && processedParams . body ) {
210228 const hasEmptyBodyVariable = Object . keys ( processedParams . body ) . some (
211229 key => ! processedParams . body ?. [ key ]
212230 ) ;
0 commit comments