import { useEffect, useState } from 'react'
import { fetch, apis } from '@/utils/request'
import { notification } from 'antd'

export default function useData(id, setLoading, callback) {
  const [data, setData] = useState()

  useEffect(() => {
    if (!id) {
      setData();
      return;
    }

    setLoading(true);
    fetch(apis.building.buildingGetById)({ urlData: { id } }).then((res) => {

      // 标签
      if (res.buildingTag) {
        res.tag = res.buildingTag.map((x) => x.tagName)
      }

      // POI 相关
      if (res.mapJson) {
        res.mapJson = JSON.parse(res.mapJson) || []
      }

      setData(res)
      setLoading(false);

      if (callback) {
        callback(res)
      }
    }).catch((err) => {
      setLoading(false);
      notification.error({message: err.message || err})
    })
  }, [id])

  return [data, setData]
}