"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    TableVoid: ()=>TableVoid
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_lodash_namespaceObject = require("lodash");
const external_index_js_namespaceObject = require("../../../index.js");
const index_js_namespaceObject = require("../../hooks/index.js");
const external_Table_Array_js_namespaceObject = require("./Table.Array.js");
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, external_lodash_namespaceObject.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, external_index_js_namespaceObject.useRequest)(useRequestProps(props), options);
const useDefSelectedRowKeys = ()=>{
    var _result_state;
    const result = (0, external_index_js_namespaceObject.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, schema_namespaceObject.observer)((props)=>{
    const { rowKey = 'id', useDataSource = useDef, useSelectedRowKeys = useDefSelectedRowKeys } = props;
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const form = (0, external_react_namespaceObject.useMemo)(()=>(0, schema_namespaceObject.createForm)(), []);
    const f = (0, index_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.AsyncDataProvider, {
        value: result,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.FormContext.Provider, {
            value: form,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.FieldContext.Provider, {
                value: f,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_Table_Array_js_namespaceObject.TableArray, {
                    ...props,
                    rowKey: rowKey,
                    useSelectedRowKeys: useSelectedRowKeys,
                    loading: null == result ? void 0 : result.loading,
                    pagination: usePaginationProps(props, result)
                })
            })
        })
    });
}, {
    displayName: 'TableVoid'
});
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
