import { errorRecord } from "../../common/libEventLog/libCrashlytics/libCrashlyticsEvent";
import { CHASSSIS_POSE_DETECTION } from "../../Api/ApiUrls";

import {
  UELogEvent,
  UELogMessage,
  UEStartTimer,
  UEEndTimer,
} from "../../common/libEventLog/UserExperior/LibUserExperior";

// instructions
export const chassiesPoseDetetction = async ({ body }) => {
  UELogEvent("Chassies Pose Detection call started", {
    payload: JSON.stringify(body),
    url: `${CHASSSIS_POSE_DETECTION}`,
  });

  UEStartTimer(CHASSSIS_POSE_DETECTION);
  try {
    const res = await fetch(`${CHASSSIS_POSE_DETECTION}`, {
      method: "POST",
      headers: {
        "Content-Type": "multipart/form-data",
      },
      body,
    });
    let data = await res.json();

    if (res.status !== 200) {
      UELogMessage(`Status ${res.status}`, {
        Error: JSON.stringify(data?.error),
      });
      throw new Error(data?.error);
    }

    UELogEvent("Chassies Pose Detection call ended in Try", {
      response: JSON.stringify(data),
    });

    UEEndTimer(CHASSSIS_POSE_DETECTION);
    return data;
  } catch (err) {
    UELogEvent("Chassies Pose Detection call ended in Catch", {
      error: JSON.stringify(err),
    });
    errorRecord(err);
    console.log(err);
  }
};
