import { standardsService } from "@/services";
import { httpUtil } from "@/utils";
import { useSetState,useDebounceEffect } from "ahooks"
import {useEffect} from "react"

interface p1{
    metadataIds:Array<string>;
    reload?:number|string;
    checkStandard?:boolean;
}

export const useGetStandardMatchByIds=(props:p1)=>{

    const {metadataIds,reload=0,checkStandard} = props

    const [state,setState] = useSetState<{mapload:boolean,standardmap:any}>({
        standardmap:{},
        mapload:false
    })

    useDebounceEffect(()=>{
        if(metadataIds.length&&checkStandard){
            getInfo()
        }else{

        }
    },[metadataIds,reload,checkStandard],{wait:300})

    const getInfo=async()=>{
        setState({mapload:true})
        const payload = {
            data:{hasConfirmed:true,
                filter:{metadataId:metadataIds},
            },
            param:{
                orOperator:true,
                exactMatch:false,
                page:1,
                size:metadataIds.length,
            }
        }
        const result:any = await standardsService.findMatchList(payload)
        if(httpUtil.checkSuccess(result,1)){
            if(httpUtil.checkSuccess(result,1)){
                let map:any = {};
                (result.data.content||[]).forEach((item:any)=>{
                    map[item.metadataId] = item
                })
                setState({mapload:false,standardmap:map})
            }else{
                setState({mapload:false,standardmap:{}})
            }
        }
    }

    return state
}