import { dispatchLoginAction } from "@/store/chatStore";
import { iUserMsg } from "@/types/userMsg";
import Bridge from "@/utils/jsBridge2";
import userAvatarUrl from "@/assets/icons/avatar.png";
import staffAvatarUrl from "@/assets/icons/staff.png";
import { eRole } from "@/types/roleType";

const USER_MSG = "USER_MSG";

export function setUserMsg(msg: iUserMsg) {
  const { token } = msg;
  new Bridge().bridge_sendToken({
    msg,
    url:
      window.location.protocol +
      "//" +
      window.location.host +
      "/proxyApi/api/v1",
  });
  localStorage.setItem(USER_MSG, JSON.stringify(msg));
}

export function getUserMsg(): iUserMsg | undefined {
  const value = localStorage.getItem(USER_MSG);
  return (value && JSON.parse(value)) || undefined;
}

export function deleteUserMsg() {
  dispatchLoginAction("logout");
  localStorage.removeItem(USER_MSG);
}

export function getCurrentUserAvatarUrl() {
  if (getUserMsg()?.role === eRole.staff) {
    return staffAvatarUrl;
  } else if (getUserMsg()?.role === eRole.user) {
    return userAvatarUrl;
  } else {
    return userAvatarUrl;
  }
}
