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

// instructions
export const instructionData = async ({ typeId, requestValue, activityId }) => {
  var url = `${INSTRUCTIONS_URL}${typeId}?isWebRequest=${requestValue}&activityId=${activityId}`;
  if (canGoVideoPi(typeId)) {
    // Video PI only for Four Wheeler
    url = `${INSTRUCTIONS_URL}${typeId}?appId=2&activityId=${activityId}`;
  }

  UELogEvent("Instruction Data Api call started", {
    typeId: typeId,
    requestValue: requestValue,
    url: `url`,
  });
  UEStartTimer(url);
  console.log("URL >>> ", url);
  try {
    const res = await fetch(url, {
      method: "GET",
    });
    let data = await res.json();
    if (res.status !== 200) {
      throw new Error(data?.error);
    }
    UELogEvent("Instruction Data Api call ended in try", {
      data: JSON.stringify(data),
    });
    UEEndTimer(url);
    return data;
  } catch (err) {
    UELogEvent("Instruction Data Api call ended in catch", {
      error: JSON.stringify(err),
    });
    errorRecord(err);
  }
};
