import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Table_Array_mjs__ from "./Table.Array.mjs";
const usePaginationProps = (props, service)=>{
    var _props_request_params, _props_request, _service_data;
    if (false === props.pagination) return false;
    const pagination = props.pagination || {};
    if (null == props ? void 0 : null === (_props_request = props.request) || void 0 === _props_request ? void 0 : null === (_props_request_params = _props_request.params) || void 0 === _props_request_params ? void 0 : _props_request_params.pageSize) {
        var _props_request_params1, _props_request1;
        pagination.defaultPageSize = null == props ? void 0 : null === (_props_request1 = props.request) || void 0 === _props_request1 ? void 0 : null === (_props_request_params1 = _props_request1.params) || void 0 === _props_request_params1 ? void 0 : _props_request_params1.pageSize;
    }
    if (!pagination.total && (null == service ? void 0 : null === (_service_data = service.data) || void 0 === _service_data ? void 0 : _service_data.meta)) {
        const { count, page, pageSize } = service.data.meta;
        pagination.total = count;
        pagination.current = page;
        pagination.pageSize = pageSize;
    }
    return {
        showSizeChanger: true,
        ...pagination,
        onChange (page, pageSize) {
            var _service_params;
            null == service || service.run({
                ...null == service ? void 0 : null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params[0],
                page,
                pageSize
            });
        }
    };
};
const useRequestProps = (props)=>{
    const { request, pagination, dataSource } = props;
    if (request) {
        if (false === pagination) return request;
        const params = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.cloneDeep)(request.params || {});
        if (!params.page) params.page = (null == pagination ? void 0 : pagination.current) || (null == pagination ? void 0 : pagination.defaultCurrent) || 1;
        if (!params.pageSize) params.pageSize = (null == pagination ? void 0 : pagination.pageSize) || (null == pagination ? void 0 : pagination.defaultPageSize) || 10;
        request.params = params;
        return request;
    }
    return function() {
        let params = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
        const { page = 1, pageSize = 10 } = params;
        const startIndex = (page - 1) * pageSize;
        const endIndex = startIndex + pageSize - 1;
        return Promise.resolve({
            data: false === pagination ? dataSource : null == dataSource ? void 0 : dataSource.slice(startIndex, endIndex + 1),
            meta: {
                page,
                pageSize,
                count: (null == dataSource ? void 0 : dataSource.length) || 0
            }
        });
    };
};
const useDef = (options, props)=>(0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRequest)(useRequestProps(props), options);
const useDefSelectedRowKeys = ()=>{
    var _result_state;
    const result = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useAsyncData)();
    return [
        null == result ? void 0 : null === (_result_state = result.state) || void 0 === _result_state ? void 0 : _result_state.selectedRowKeys,
        (selectedRowKeys)=>{
            var _result_setState;
            return null == result ? void 0 : null === (_result_setState = result.setState) || void 0 === _result_setState ? void 0 : _result_setState.call(result, {
                selectedRowKeys
            });
        }
    ];
};
const TableVoid = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { rowKey = 'id', useDataSource = useDef, useSelectedRowKeys = useDefSelectedRowKeys } = props;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.createForm)(), []);
    const f = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useAttach)(form.createArrayField({
        ...field.props,
        basePath: ''
    }));
    const result = useDataSource({
        uid: fieldSchema['x-uid'],
        onSuccess (data) {
            var _field_componentProps, _data_meta, _data_meta1, _data_meta2;
            form.setValues({
                [fieldSchema.name]: null == data ? void 0 : data.data
            });
            if ((null == field ? void 0 : null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : _field_componentProps.pagination) === false) return;
            field.componentProps.pagination = field.componentProps.pagination || {};
            if (null == data ? void 0 : null === (_data_meta = data.meta) || void 0 === _data_meta ? void 0 : _data_meta.count) {
                var _data_meta3;
                field.componentProps.pagination.total = null == data ? void 0 : null === (_data_meta3 = data.meta) || void 0 === _data_meta3 ? void 0 : _data_meta3.count;
            }
            field.componentProps.pagination.current = (null == data ? void 0 : null === (_data_meta1 = data.meta) || void 0 === _data_meta1 ? void 0 : _data_meta1.page) || 1;
            field.componentProps.pagination.pageSize = (null == data ? void 0 : null === (_data_meta2 = data.meta) || void 0 === _data_meta2 ? void 0 : _data_meta2.pageSize) || 10;
        }
    }, props);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.AsyncDataProvider, {
        value: result,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FormContext.Provider, {
            value: form,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FieldContext.Provider, {
                value: f,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__Table_Array_mjs__.TableArray, {
                    ...props,
                    rowKey: rowKey,
                    useSelectedRowKeys: useSelectedRowKeys,
                    loading: null == result ? void 0 : result.loading,
                    pagination: usePaginationProps(props, result)
                })
            })
        })
    });
}, {
    displayName: 'TableVoid'
});
export { TableVoid };
