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) {
  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()?.role2 === eRole.staff ||
    getUserMsg()?.role2 === eRole.manager
  ) {
    return staffAvatarUrl;
  } else if (getUserMsg()?.role2 === eRole.user) {
    return userAvatarUrl;
  } else {
    return userAvatarUrl;
  }
}
