import pcaData from 'china-division/dist/pca-code.json';

export const buildData = (limitLevel) => {
  let areaCodeMap = {};

  const transferData = (list, parent, parentLevel) => {
    let curLevel = parentLevel + 1;
    // if (list.length === 1 && list[0].name === '市辖区' && limitLevel === 2) {
    //   return transferData(list[0].children, parent, parentLevel);
    // }
    return list.map((area) => {
      let narea = { ...area };
      if (narea.name === '市辖区' && parent) {
        narea.name = parent.name;
      }
      let pathInfo = { pathName: '', pathCode: '' };
      if (parent) {
        pathInfo.pathName = `${parent.pathName}_${narea.name}`;
        pathInfo.pathCode = `${parent.pathCode}_${narea.code}`;
      } else {
        pathInfo.pathName = `${narea.name}`;
        pathInfo.pathCode = `${narea.code}`;
      }
      areaCodeMap[narea.code] = narea.name;

      if (area.children && area.children.length && curLevel < limitLevel) {
        return {
          label: narea.name,
          value: narea.code,
          children: transferData(
            area.children,
            { ...area, ...pathInfo },
            curLevel
          ),
        };
      } else {
        areaCodeMap[pathInfo.pathName] = pathInfo.pathCode;

        return {
          label: narea.name,
          value: narea.code,
        };
      }
    });
  };
  const areaData = transferData(pcaData, null, 0);

  return { areaData, areaCodeMap };
};
