// 第三方依赖
import * as React from 'react';
import isEqual from 'lodash/isEqual';
import pick from 'lodash/pick';
import { useControllableValue } from 'ahooks';
// 其他组件
import { safeCallFunction } from '../utils/safe-call-function';
// 当前目录的相对依赖
import { globalConfig } from '../constants/global-config';
export const usePage = (props) => {
    var _a;
    const pageInfo = React.useRef({});
    const pageHasChange = React.useRef(false);
    const { defaultPageSize, defaultCurrentPage, onPageChange, pageProps = {}, pageSizeList: propsPageSizeList,
    // dataSource, // 不在这里使用，避免根据数据长度自动更新总数
     } = props;
    const pageSizeListValue = React.useMemo(() => {
        if ('pageSizeList' in pageProps)
            return { value: pageProps.pageSizeList };
        if (propsPageSizeList)
            return { value: propsPageSizeList };
        return {};
    }, [pageProps === null || pageProps === void 0 ? void 0 : pageProps.pageSizeList, propsPageSizeList]);
    const [total, setTotal] = useControllableValue(pick(pageProps, ['total']), {
        valuePropName: 'total',
        defaultValue: 0,
    });
    // 注意：不要根据 dataSource 长度自动更新 total
    // 因为 dataSource 可能是分页后的数据，总数应该由接口返回的 paging.totalCount 决定
    // React.useEffect(() => {
    //   if (dataSource !== undefined) {
    //     const dataSourceLength = dataSource?.length ?? 0;
    //     setTotal(dataSourceLength);
    //   }
    // }, [dataSource, setTotal]);
    const [currentPage, setCurrent] = useControllableValue({
        ...pick(pageProps, ['current']),
        onChange: (_currentPage, type) => handlePageChange({ currentPage: _currentPage, type }),
    }, {
        defaultValue: defaultCurrentPage !== null && defaultCurrentPage !== void 0 ? defaultCurrentPage : 1,
        valuePropName: 'current',
    });
    const [pageSizeList, setPageSizeList] = useControllableValue({
        ...pageSizeListValue,
        onChange: (_pageSizeList) => handlePageChange({ pageSizeList: _pageSizeList }),
    }, {
        defaultValue: globalConfig.pageSizeList,
    });
    const [pageSize, setPageSize] = useControllableValue({
        ...pick(pageProps, ['pageSize']),
        onChange: (_pageSize) => handlePageChange({ pageSize: _pageSize }),
    }, {
        valuePropName: 'pageSize',
        defaultValue: (_a = defaultPageSize !== null && defaultPageSize !== void 0 ? defaultPageSize : pageSizeList === null || pageSizeList === void 0 ? void 0 : pageSizeList[0]) !== null && _a !== void 0 ? _a : globalConfig.defaultPageSize,
    });
    const resultPageInfo = {
        pageSizeList,
        pageSize,
        total,
        currentPage,
    };
    function handlePageChange(params) {
        if (params.type === 'linkage')
            return;
        delete params.type;
        // 如果pagesize发生了变化把当前页重置成第一页
        if ('pageSize' in params) {
            setCurrent(1, 'linkage');
            Object.assign(params, { currentPage: 1 });
        }
        Object.assign(resultPageInfo, params);
        if (!isEqual(pageInfo.current, resultPageInfo)) {
            pageHasChange.current = true;
            safeCallFunction(onPageChange, resultPageInfo);
        }
    }
    pageInfo.current = { ...resultPageInfo };
    return {
        total,
        setTotal,
        pageSize,
        setPageSize,
        currentPage,
        setCurrent,
        setCurrentPage: setCurrent,
        pageSizeList,
        setPageSizeList,
        pageHasChange,
    };
};
