export const getCurrentKey = () => {
  return localStorage.getItem('demo-key') || 'default';
};

export const setCurrentKey = (key = 'default') => {
  localStorage.setItem('demo-key', key);
};

export const setDocValue = (value: string, key: string = getCurrentKey()) => {
  localStorage.setItem(`${key}-demo-value`, value);
};

export const getDocValue = (key: string = getCurrentKey()) => {
  return localStorage.getItem(`${key}-demo-value`);
};

export const listToTree = (list: Array<any> = [], parentKey = 'pid') => {
  const map: any = {};
  const treeData = [];
  for (let i = 0; i < list.length; i += 1) {
    const item = list[i];
    map[item.id] = i;
    item.children = [];
  }

  for (let i = 0; i < list.length; i += 1) {
    const node = list[i];
    if (node[parentKey] && list[map[node[parentKey]]]) {
      list[map[node[parentKey]]].children.push(node);
    } else {
      treeData.push(node);
    }
  }
  return treeData;
};