import { DOCUMENT_LAST } from "react-native-digitcamera/src/Api/ApiUrls";
import status from "../Status";
import { errorRecord } from "../../common/libEventLog/libCrashlytics/libCrashlyticsEvent";
import {
  UELogEvent, UELogMessage, UEStartTimer, UEEndTimer
} from "../../common/libEventLog/UserExperior/LibUserExperior"

export const getDocumentLast = async ({ body }) => {
  UELogEvent("Get Document Last Api Call started", {
    "payload": JSON.stringify(body),
    "url": DOCUMENT_LAST
  })
  try {
    UEStartTimer(DOCUMENT_LAST);
    console.log("Service", DOCUMENT_LAST);
    const res = await fetch(DOCUMENT_LAST, {
      method: "POST",
      body: JSON.stringify(body),
    });
    const checkd = status.checkStatus(res);
    const json = await checkd.json();
    if (res.status !== 200) {
      UELogMessage(`Status ${res.status}`, {
        "Error": JSON.stringify(data?.error)
      })
      throw new Error(data?.error);
    }
    UELogEvent("Get Document Last Api Call ended in Try", {
      "response": JSON.stringify(json),
      "status":JSON.stringify(res.status)
    })
    UEEndTimer(DOCUMENT_LAST);
    return json;
  } catch (err) {
    UELogEvent("Get Document Last Api Call ended in Catch", {
      "error": JSON.stringify(err),
    })
    errorRecord(err);
    console.log("Document Last Error", err);
    throw new Error(err.message);
  }
};
