import { getToken } from "@/utils/Cookies";
import { useState } from "react";

/**
 * @description 加载更多 hooks
 * @param {Function} func 请求方法
 * @param {Object} params 请求参数
 * @return haMore 是否有更多
 * @return loadMore 加载更多方法
 * @return getData 获取数据方法
 */
export default function useLoadMore(func: Function, params: any) {
    const token = getToken();

    const [hasMore, setHasMore] = useState(true);
    const loadMore = async () => {
        await func({
            ...params.current,
        });
    };
    const getData = async (params: any) => {
        if (!token) {
            setHasMore(false);
            return;
        }
        try {
            const res = await func(params);
            // setDataSource((val) => ({ ...val, list: [...val.list, ...res.data] }));
            setHasMore(!res.page.is_end!);
        } catch (error) {
            setHasMore(false);
            return Promise.reject();
        }
    };
    return {
        hasMore,
        loadMore,
        getData,
    };
}
