let list = [];
/**
 * {id ,
 * ref={},
 * groupname}
 */

/**
 *  Rule Lat in , First out
 */

export function add(id, ref, groupname, others) {
  // others data move to ref....
  // others separate
  list.push({
    id,
    ref,
    groupname,
    ...others
  });
} // export function update(id, ref, groupname, others) {
//   list.forEach(() => {});
// }

export function popOut() {
  let obj = list.pop();
  obj && obj.ref.cancel();
}
export function cancel(id, groupname) {
  list = list.reverse().filter(obj => {
    if (obj.id === id && groupname === obj.groupname) {
      obj.ref.cancel();
      return false;
    }

    return true;
  }).reverse();
}
export function cancelGroup(groupname) {
  list = list.reverse().filter(obj => {
    if (groupname === obj.groupname) {
      obj.ref.cancel();
      return false;
    }

    return true;
  }).reverse();
}
export function groupBasedFilter(groupname) {
  let out = list.map(obj => {
    if (groupname === obj.groupname) {
      return obj;
    }
  });
  return out.filter(Boolean);
}
export function getData() {
  return list;
}
export function cancelOthers(id, groupname) {
  list = list.reverse().filter(obj => {
    if (obj.id !== id && groupname === obj.groupname) {
      obj.ref.cancel();
      return false;
    }

    return true;
  }).reverse(); // const group = groupName;
  // // const { isPopupOpen } = this.state;
  // const groupPopups = groupBasedFilter(group);
  // lastOpenedGroup =
  //   !isPopupOpen && lastOpenedGroupFunc().indexOf(group) === -1
  //     ? [group, ...lastOpenedGroupFunc()]
  //     : lastOpenedGroupFunc();
  // isPopupOpen && popOut();
  // groupPopups.forEach((popup) => {
  //   if (popup.id !== id && popup.isPopupOpen) {
  //     popup.ref.cancel();
  //   }
  // });
}