"use client";
export function save(key, value) {
  // 只在客户端执行
  if (
    typeof window !== "undefined" &&
    typeof window.localStorage !== "undefined"
  ) {
    if (typeof value == "object") {
      localStorage.setItem(key, JSON.stringify(value));
    } else {
      localStorage.setItem(key, value);
    }
  }
}
export function get(key) {
  if (
    typeof window !== "undefined" &&
    typeof window.localStorage !== "undefined"
  ) {
    let data = localStorage.getItem(key);
    // console.log(key, data);
    return data?.indexOf("{") != -1 ? JSON.parse(data) : data;
  }
}
export function remove(key) {
  if (
    typeof window !== "undefined" &&
    typeof window.localStorage !== "undefined"
  ) {
    localStorage.removeItem(key);
  }
}
export function clear() {
  if (
    typeof window !== "undefined" &&
    typeof window.localStorage !== "undefined"
  ) {
    localStorage.clear();
  }
}

export function saveUserInfo(data, mobilePhone, regionCode) {
  save("token", data.data.token);
  save("account", JSON.stringify(data.data.account));
  save("mobile_phone", mobilePhone);
  save("region_code", regionCode);
}
export function removeUserInfo() {
  remove("token");
  remove("account");
  remove("mobile_phone");
  remove("region_code");
}