// 暂时把地区信息打包在本地, 后续移到COS桶内
import provinces from './provinces.json';
import { CITIES } from './cities';
import { REGIONS } from './regions';
import weui from '../../../../utils/weui';

let tree;

export function getRegionTree() {
  if (!tree) {
    tree = provinces.map((province) => {
      return {
        label: province.name,
        value: province.name,
        children: CITIES.filter(
          (city) => city.parent_code === province.code
        ).map((city) => {
          return {
            label: city.name,
            value: city.name,
            children: REGIONS.filter(
              (region) => region.parent_code === city.code
            ).map((region) => {
              return {
                label: region.name,
                value: region.name,
              };
            }),
          };
        }),
      };
    });
  }

  return tree;
}

export function regionPicker(options) {
  weui.picker(getRegionTree(), options);
}
