export function stringifyFunction(fn: (() => void) | null | undefined, args?: Record<string, unknown>) {
  if (!fn) return '';
  const functionAsString = fn.toString();
  const bodyStart = functionAsString.indexOf('{') + 1;
  const bodyEnd = functionAsString.lastIndexOf('}');
  let functionBody = functionAsString.substring(bodyStart, bodyEnd).trim();

  if (args) {
    for (const [key, value] of Object.entries(args)) {
      const regex = new RegExp(`\\b${key}\\b`, 'g');
      functionBody = functionBody.replace(regex, JSON.stringify(value));
    }
  }

  functionBody = functionBody
    .replace(/\bev\./g, 'event.')
    .replace(/\}\s\= ev/g, '} = event')
    .replace(/(\r\n|\n|\r)/gm, ' ')
    .replace(/\s+/g, ' ');

  return `(function(){${functionBody}})();`;
}
