import { POSE_DETECTION } from "../../Api/ApiUrls";
import { errorRecord } from "../../common/libEventLog/libCrashlytics/libCrashlyticsEvent";
import {
  UEEndTimer,
  UELogEvent, UELogMessage, UEStartTimer
} from "../../common/libEventLog/UserExperior/LibUserExperior"

// instructions
export const poseDetection = async ({ body }) => {
  UELogEvent("poseDetection API Execution Started", {
    "payload": JSON.stringify(body)
  })
  try {
    console.log("POSE API ->", POSE_DETECTION);
    UEStartTimer(POSE_DETECTION);

    const res = await fetch(`${POSE_DETECTION}`, {
      method: "POST",
      headers: {
        "Content-Type": "multipart/form-data",
      },
      body,
    });
    let data = await res.json();
    if (res.status !== 200) {
      UELogMessage(`Status ${res.status}`, {
        "status": res.status,
        "error": JSON.stringify(data?.error)
      })
      throw new Error(data?.error);
    }

    UELogEvent("poseDetection API Execution Ended in Try", {
      "response": JSON.stringify(data)
    })

    UEEndTimer(POSE_DETECTION);

    return data;
  } catch (err) {
    UELogEvent("poseDetection API Execution Ended in Catch", {
      "error": JSON.stringify(err),
    })

    errorRecord(err);
    console.log("FROM POSE DETECTION ", err);
  }
};
