import { Variable } from '@typebot.io/schemas' import { defaultParseVariablesOptions, parseVariables, ParseVariablesOptions, } from './parseVariables' export const deepParseVariables = ( variables: Variable[], options: ParseVariablesOptions = defaultParseVariablesOptions ) => >(object: T): T => Object.keys(object).reduce((newObj, key) => { const currentValue = object[key] if (typeof currentValue === 'string') { const parsedVariable = parseVariables(variables, options)(currentValue) return { ...newObj, [key]: parsedVariable, } } if (currentValue instanceof Object && currentValue.constructor === Object) return { ...newObj, [key]: deepParseVariables( variables, options )(currentValue as Record), } if (currentValue instanceof Array) return { ...newObj, [key]: currentValue.map(deepParseVariables(variables, options)), } return { ...newObj, [key]: currentValue } }, {} as T)