/*
 * @Author: donghao donghao@supervision.ltd
 * @Date: 2024-04-19 17:10:21
 * @LastEditors: donghao donghao@supervision.ltd
 * @LastEditTime: 2024-05-24 17:39:02
 * @FilePath: \general-ai-platform-web\src\hooks\useCity.tsx
 * @Description: 省份城市数据转换
 */
import { provinceAndCityEnum } from '@/enums/city';
export type useCityProps = {
  formatProvinceByData: () => { label: string; value: string; children?: Record<string, any>[] }[];
  formatCityByProvinceData: (
    arg1: Record<string, any>,
  ) => { label: string; value: string; children?: Record<string, any>[] }[];
  formatCityByProvince: (
    arg1: string,
  ) => { label: string; value: string; children?: Record<string, any>[] }[];
};
export const useCity: useCityProps = () => {
  function formatProvinceByData() {
    const provinceCityData = provinceAndCityEnum.map((item) => {
      item.label = item.provinceName;
      item.value = item.provinceName;
      item.children = item.citys;
      return item;
    });
    return provinceCityData;
  }

  function formatCityByProvinceData(arg1) {
    const cityData = arg1?.children?.map((item) => {
      item.label = item.cityName;
      item.value = item.cityName;
      return item;
    });
    return cityData;
  }

  function formatCityByProvince(arg1) {
    const startData = formatProvinceByData();
    let finalItem = startData[0];
    startData.forEach((item) => {
      if (item.value === arg1) {
        finalItem = item;
      }
    });
    const cityData = formatCityByProvinceData(finalItem);
    return cityData;
  }

  return {
    formatProvinceByData,
    formatCityByProvinceData,
    formatCityByProvince,
  };
};
