import {
  remember_password_phone,
  remember_password_email,
} from '@/constants/token';

export const getRememberList = (type: string) => {
  //   localStorage.setItem(remember_password_phone, JSON.stringify([]));
  //   localStorage.setItem(remember_password_email, JSON.stringify([]));
  let new_type = '';
  if (type == 'phone') {
    new_type = remember_password_phone;
  } else {
    new_type = remember_password_email;
  }
  let list = localStorage.getItem(new_type);
  if (list) {
    return JSON.parse(list);
  } else {
    return [];
  }
};

export const setRememberList = (obj: any, type: string) => {
  let list = [obj];
  let new_type = '';
  if (type == 'phone') {
    new_type = remember_password_phone;
  } else if (type == 'email') {
    new_type = remember_password_email;
  }
  //   本地已经存储的地址
  let locaList: any = getRememberList(new_type);
  if (locaList.length) {
    //   判断本地是否存在相同账号
    let index = locaList.findIndex((item: any) => item.account == obj.account);
    if (index !== -1) {
      // 找到相同的，删除
      locaList.splice(index, 1);
    }
    list = list.concat(locaList);
  }
  //   存储
  localStorage.setItem(new_type, JSON.stringify(list));
};
