import Database from "../../../../Components/common/DAsyncStorage";
export var emailId = undefined;
export var name = undefined;
export var phoneNo = undefined;
export var panHolderName = undefined;
export var panNo = undefined;
export var agentNumber = undefined;

export async function getUserDetails() {
  try {
    var AppLoginStatus = await Database.get("AppLoginStatus");
    if (AppLoginStatus === undefined || AppLoginStatus === undefined) {
      throw new Error("We are unable to find your details!");
    } else if (AppLoginStatus.userType === "Customer") {
      // Customer logged-in
      const RealmCustomerDetails = await Database.get("CustomerDetails");
      (phoneNo = RealmCustomerDetails?.mobileNumber),
        (emailId = RealmCustomerDetails?.email),
        (name = RealmCustomerDetails?.name);
    } else if (AppLoginStatus.userType === "DealerOrAgent") {
      // DearlerOrAgent logged-in
      const agentData = await Database.get("AgentDetails");
      name = agentData?.name;
      emailId = agentData?.emailId;
      phoneNo = agentData?.phoneNo;
      panHolderName = agentData?.panHolderName;
      panNo = agentData?.panNo;
      agentNumber = agentData?.agentNumber;
    } else {
      // OEM Dealer logged-in
      const oemDealerInfo = await Database.get("OEMDealerInfo");

      // agentNumber =oemDealerInfo?.dealerCode;
      name = oemDealerInfo?.dealerName;
      agentNumber = oemDealerInfo?.agentCode;
      // =oemDealerInfo?.agentName;
      emailId = oemDealerInfo?.email;
      phoneNo = oemDealerInfo?.mobileNumber;
      // emailId=oemDealerInfo?.runnerEmail;
      // phoneNo=oemDealerInfo?.runnerMobile;
      // name=oemDealerInfo?.runnerName;
    }
  } catch (error) {
    throw new Error("getUserDetails " + error);
  }
}

export default {
  getUserDetails,
  emailId,
  name,
  phoneNo,
  panHolderName,
  panNo,
  agentNumber,
};
