"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePage = void 0;
// 第三方依赖
const React = __importStar(require("react"));
const isEqual_1 = __importDefault(require("lodash/isEqual"));
const pick_1 = __importDefault(require("lodash/pick"));
const ahooks_1 = require("ahooks");
// 其他组件
const safe_call_function_1 = require("../utils/safe-call-function");
// 当前目录的相对依赖
const global_config_1 = require("../constants/global-config");
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] = (0, ahooks_1.useControllableValue)((0, pick_1.default)(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] = (0, ahooks_1.useControllableValue)({
        ...(0, pick_1.default)(pageProps, ['current']),
        onChange: (_currentPage, type) => handlePageChange({ currentPage: _currentPage, type }),
    }, {
        defaultValue: defaultCurrentPage !== null && defaultCurrentPage !== void 0 ? defaultCurrentPage : 1,
        valuePropName: 'current',
    });
    const [pageSizeList, setPageSizeList] = (0, ahooks_1.useControllableValue)({
        ...pageSizeListValue,
        onChange: (_pageSizeList) => handlePageChange({ pageSizeList: _pageSizeList }),
    }, {
        defaultValue: global_config_1.globalConfig.pageSizeList,
    });
    const [pageSize, setPageSize] = (0, ahooks_1.useControllableValue)({
        ...(0, pick_1.default)(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 : global_config_1.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 (!(0, isEqual_1.default)(pageInfo.current, resultPageInfo)) {
            pageHasChange.current = true;
            (0, safe_call_function_1.safeCallFunction)(onPageChange, resultPageInfo);
        }
    }
    pageInfo.current = { ...resultPageInfo };
    return {
        total,
        setTotal,
        pageSize,
        setPageSize,
        currentPage,
        setCurrent,
        setCurrentPage: setCurrent,
        pageSizeList,
        setPageSizeList,
        pageHasChange,
    };
};
exports.usePage = usePage;
