module WolfCore::LambdaFunctionOperations

def deep_parse_json(input)

def deep_parse_json(input)
  while input.is_a?(String)
    begin
      input = JSON.parse(input)
    rescue JSON::ParserError
      break
    end
  end
  input&.with_indifferent_access || {}
end

def get_event_params(event)

def get_event_params(event)
  event_body = event['body']
  return deep_parse_json(event_body) if event_body.present?
  event.with_indifferent_access
end

def invoke_lambda(function_name:, payload:)

def invoke_lambda(function_name:, payload:)
  WolfCore::LambdaFunctionDataSource.invoke(function_name: function_name, payload: payload)
end

def result_to_response(result)

def result_to_response(result)
  if result.success?
    { statusCode: 200, body: result.data.to_h.to_json }
  else
    { statusCode: result.error.status || 422, body: result.error.to_h.to_json }
  end
end