/* eslint-disable @typescript-eslint/no-explicit-any */
import { evaluateDynamicString } from "../evaluateDynamicString"
import { wrapFunctionCode } from "../evaluateDynamicString/utils"
import { IMPAKTAPPSEditorRuntimePropsCollectorInstance } from "../executionTreeHelper/runtimePropsCollector"

export function runTransformer(transformer: any, rawData: any,store) {
  let calcResult: any = rawData
  if (transformer?.enable) {
    const evaluateTransform = wrapFunctionCode(transformer.rawData)
    const canEvalString = `{{${evaluateTransform}()}}`
    const finalContext =
      IMPAKTAPPSEditorRuntimePropsCollectorInstance.getGlobalCalcContext({
        data: rawData,
      },store)
    try {
      calcResult = evaluateDynamicString("events", canEvalString, finalContext)
    } catch (e) {
      console.log(e)
    }
  }
  return calcResult
}
