import { useAjax } from '@/Hook/useAjax'
import { getTargetingGagsApi } from '@/services/adqV3/global'
import { useState } from 'react'

/**
 * 获取所有品牌手机 所有地域列表
 * @returns 
 */
export default function useTargeting() {

    const getTargetingGags = useAjax((params) => getTargetingGagsApi(params))

    const [geoLocationList, setGeoLocationList] = useState<any>({}) // 所有地域列表
    const [modelList, setModelList] = useState<any>({})  // 所有品牌手机


    const initTargeting = async () => {
        if (!(geoLocationList && Object.keys(geoLocationList).length)) {
            await getTargetingGags.run({ type: 'REGION' }).then(res => {
                if (res && Array.isArray(res)) {
                    setGeoLocationList(() => (res as any[])?.reduce((prev: any, cur: { id: number }) => {
                        prev[cur.id] = cur
                        return prev
                    }, {}))
                }
            })
        }
        if (!(modelList && Object.keys(modelList).length)) {
            await getTargetingGags.run({ type: 'DEVICE_BRAND_MODEL' }).then(res => {
                if (res && Array.isArray(res)) {
                    setModelList(() => (res as any[])?.reduce((prev: any, cur: { id: number }) => {
                        prev[cur.id] = cur
                        return prev
                    }, {}))
                }
            })
        }
    }

    return {
        initTargeting,
        geoLocationList,
        modelList
    }
}