const formattedPrice = (price: number) =>  price.toLocaleString("en-IN", {
    style: "currency",
    currency: "INR",
});

const getCartIdentifier = () => {
  // Check logged in user from localStorage
  const customerRaw = localStorage.getItem("customer");
  const customer = customerRaw ? JSON.parse(customerRaw) : null;

  // If logged in use userId
  if (customer?.id) {
    let guestId = localStorage.getItem("guestId");
    if (!guestId) {
      localStorage.removeItem("guestId");
    }
    return { user: customer.id };
  }

  // If guest generate/reuse guestId
  let guestId = localStorage.getItem("guestId");
  if (!guestId) {
    guestId =
  Date.now() +
  "_" +
  Math.random().toString(36).substring(2, 15);
    localStorage.setItem("guestId", guestId);
  }

  return { guestId };
}

export { formattedPrice, getCartIdentifier }
