// 把游客token编码
export const encodeJWT = (payload: object): string => {
  try {
    const jsonStr = JSON.stringify(payload);
    const encodedPayload = btoa(encodeURIComponent(jsonStr));

    const header = btoa(JSON.stringify({ alg: "none", typ: "JWT" }));

    const token = `${header}.${encodedPayload}`;

    return encodeURIComponent(token);
  } catch (e) {
    console.error("Error encoding JWT:", e);
    return "";
  }
};


export const decodeJWT = <T = any>(token: string, isGuest: boolean = true): T | null => {
  try {
    if (!isGuest) {
      return { sessionToken: token } as unknown as T;
    }

    const decodedToken = decodeURIComponent(token);
    const parts = decodedToken.split(".");

    // if (parts.length !== 2) {
    //   return null;
    // }

    const payloadPart = parts[1];
    if (!payloadPart) return null;

    const jsonStr = decodeURIComponent(atob(payloadPart));
    console.log("jsonStr", jsonStr);
    return JSON.parse(jsonStr) as T;
  } catch (e) {
    console.warn("Error decoding JWT:", e);
    return null;
  }
};
