import { jsx } from "react/jsx-runtime";
import { useMemo } from "react";
import { FieldContext, FormContext, createForm, observer, useField, useFieldSchema } from "@tachybase/schema";
import { cloneDeep } from "lodash";
import { AsyncDataProvider, useAsyncData, useRequest } from "../../../index.mjs";
import { useAttach } from "../../hooks/index.mjs";
import { withAutoQuickJumper } from "../pagination/utils.mjs";
import { TableArray } 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 : null == (_props_request_params = _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 : null == (_props_request_params1 = _props_request1.params) ? void 0 : _props_request_params1.pageSize;
    }
    if (!pagination.total && (null == service ? void 0 : null == (_service_data = service.data) ? void 0 : _service_data.meta)) {
        const { count, page, pageSize } = service.data.meta;
        pagination.total = count;
        pagination.current = page;
        pagination.pageSize = pageSize;
    }
    return withAutoQuickJumper({
        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[0],
                page,
                pageSize
            });
        }
    });
};
const useRequestProps = (props)=>{
    const { request, pagination, dataSource } = props;
    if (request) {
        if (false === pagination) return request;
        const params = 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)=>useRequest(useRequestProps(props), options);
const useDefSelectedRowKeys = ()=>{
    var _result_state;
    const result = useAsyncData();
    return [
        null == result ? void 0 : null == (_result_state = 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.call(result, {
                selectedRowKeys
            });
        }
    ];
};
const TableVoid = observer((props)=>{
    const { rowKey = 'id', useDataSource = useDef, useSelectedRowKeys = useDefSelectedRowKeys } = props;
    const field = useField();
    const fieldSchema = useFieldSchema();
    const form = useMemo(()=>createForm(), []);
    const f = 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.pagination) === false) return;
            field.componentProps.pagination = field.componentProps.pagination || {};
            if (null == data ? void 0 : null == (_data_meta = 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.count;
            }
            field.componentProps.pagination.current = (null == data ? void 0 : null == (_data_meta1 = data.meta) ? void 0 : _data_meta1.page) || 1;
            field.componentProps.pagination.pageSize = (null == data ? void 0 : null == (_data_meta2 = data.meta) ? void 0 : _data_meta2.pageSize) || 10;
        }
    }, props);
    return /*#__PURE__*/ jsx(AsyncDataProvider, {
        value: result,
        children: /*#__PURE__*/ jsx(FormContext.Provider, {
            value: form,
            children: /*#__PURE__*/ jsx(FieldContext.Provider, {
                value: f,
                children: /*#__PURE__*/ jsx(TableArray, {
                    ...props,
                    rowKey: rowKey,
                    useSelectedRowKeys: useSelectedRowKeys,
                    loading: null == result ? void 0 : result.loading,
                    pagination: usePaginationProps(props, result)
                })
            })
        })
    });
}, {
    displayName: 'TableVoid'
});
export { TableVoid };
