package com.hubspot.jinjava.lib.fn.eager;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hubspot.jinjava.el.ext.AbstractCallableMethod;
import com.hubspot.jinjava.interpret.DeferredValue;
import com.hubspot.jinjava.interpret.DeferredValueException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
import com.hubspot.jinjava.lib.fn.MacroFunction;
import com.hubspot.jinjava.lib.tag.MacroTag;
import com.hubspot.jinjava.util.ChunkResolver;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.StringJoiner;

public class EagerMacroFunction extends AbstractCallableMethod {
  private String fullName;
  private MacroFunction macroFunction;
  private JinjavaInterpreter interpreter;

  public EagerMacroFunction(
    String fullName,
    MacroFunction macroFunction,
    JinjavaInterpreter interpreter
  ) {
    super(
      macroFunction.getName(),
      getLinkedHashmap(macroFunction.getArguments(), macroFunction.getDefaults())
    );
    this.fullName = fullName;
    this.macroFunction = macroFunction;
    this.interpreter = interpreter;
  }

  private static LinkedHashMap<String, Object> getLinkedHashmap(
    List<String> args,
    Map<String, Object> defaults
  ) {
    LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
    for (String arg : args) {
      linkedHashMap.put(arg, defaults.get(arg));
    }
    return linkedHashMap;
  }

  public Object doEvaluate(
    Map<String, Object> argMap,
    Map<String, Object> kwargMap,
    List<Object> varArgs
  ) {
    Optional<String> importFile = macroFunction.getImportFile(interpreter);
    try (InterpreterScopeClosable c = interpreter.enterScope()) {
      return macroFunction.getEvaluationResult(argMap, kwargMap, varArgs, interpreter);
    } finally {
      importFile.ifPresent(path -> interpreter.getContext().getCurrentPathStack().pop());
    }
  }

  public String getStartTag(JinjavaInterpreter interpreter) {
    StringJoiner argJoiner = new StringJoiner(", ");
    for (String arg : macroFunction.getArguments()) {
      try {
        if (macroFunction.getDefaults().get(arg) != null) {
          argJoiner.add(
            String.format(
              "%s=%s",
              arg,
              ChunkResolver.getValueAsJinjavaString(macroFunction.getDefaults().get(arg))
            )
          );
          continue;
        }
      } catch (JsonProcessingException ignored) {}
      argJoiner.add(arg);
    }
    return new StringJoiner(" ")
      .add(interpreter.getConfig().getTokenScannerSymbols().getExpressionStartWithTag())
      .add(MacroTag.TAG_NAME)
      .add(String.format("%s(%s)", fullName, argJoiner.toString()))
      .add(interpreter.getConfig().getTokenScannerSymbols().getExpressionEndWithTag())
      .toString();
  }

  public String getEndTag(JinjavaInterpreter interpreter) {
    return new StringJoiner(" ")
      .add(interpreter.getConfig().getTokenScannerSymbols().getExpressionStartWithTag())
      .add(String.format("end%s", MacroTag.TAG_NAME))
      .add(interpreter.getConfig().getTokenScannerSymbols().getExpressionEndWithTag())
      .toString();
  }

  public String reconstructImage() {
    String result;
    try {
      result =
        (String) evaluate(
          macroFunction
            .getArguments()
            .stream()
            .map(arg -> DeferredValue.instance())
            .toArray()
        );
    } catch (DeferredValueException e) {
      return macroFunction.reconstructImage();
    }

    return (getStartTag(interpreter) + result + getEndTag(interpreter));
  }
}

@@ -3309,28 +3309,16 @@
 result;%0A
-    try %7B%0A  
     resu
@@ -3322,18 +3322,16 @@
 esult =%0A
-  
       (S
@@ -3347,18 +3347,16 @@
 aluate(%0A
-  
         
@@ -3379,18 +3379,16 @@
         
-  
 .getArgu
@@ -3405,18 +3405,16 @@
         
-  
 .stream(
@@ -3415,18 +3415,16 @@
 tream()%0A
-  
         
@@ -3459,26 +3459,24 @@
 instance())%0A
-  
           .t
@@ -3494,107 +3494,10 @@
     
-  );%0A    %7D catch (DeferredValueException e) %7B%0A      return macroFunction.reconstructImage();%0A    %7D%0A
+);
 %0A   

Remove unnecessary try catch