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__block_provider_hooks_index_mjs__ from "../../../../../block-provider/hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_TableBlockProvider_mjs__ from "../../../../../block-provider/TableBlockProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__filter_provider_FilterProvider_mjs__ from "../../../../../filter-provider/FilterProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__ from "../../../../../filter-provider/utils.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../../../schema-component/index.mjs";
const useTableBlockProps = ()=>{
    var _fieldSchema_parent_xdecoratorprops, _fieldSchema_parent, _ctx_service, _ctx_service1, _ctx_field_data, _ctx_field, _ctx_service2, _ctx_service3, _fieldSchema_xcomponentprops;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_TableBlockProvider_mjs__.useTableBlockContext)();
    const globalSort = null === (_fieldSchema_parent = fieldSchema.parent) || void 0 === _fieldSchema_parent ? void 0 : null === (_fieldSchema_parent_xdecoratorprops = _fieldSchema_parent['x-decorator-props']) || void 0 === _fieldSchema_parent_xdecoratorprops ? void 0 : _fieldSchema_parent_xdecoratorprops['dragSortBy'];
    const { getDataBlocks } = (0, __WEBPACK_EXTERNAL_MODULE__filter_provider_FilterProvider_mjs__.useFilterBlock)();
    const isLoading = null == ctx ? void 0 : null === (_ctx_service = ctx.service) || void 0 === _ctx_service ? void 0 : _ctx_service.loading;
    null == ctx || null === (_ctx_service1 = ctx.service) || void 0 === _ctx_service1 || _ctx_service1.params;
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (!isLoading) {
            var _ctx_service, _ctx_field_data, _ctx_field;
            const serviceResponse = null == ctx ? void 0 : null === (_ctx_service = ctx.service) || void 0 === _ctx_service ? void 0 : _ctx_service.data;
            const data = (null == serviceResponse ? void 0 : serviceResponse.data) || [];
            const meta = (null == serviceResponse ? void 0 : serviceResponse.meta) || {};
            const selectedRowKeys = null == ctx ? void 0 : null === (_ctx_field = ctx.field) || void 0 === _ctx_field ? void 0 : null === (_ctx_field_data = _ctx_field.data) || void 0 === _ctx_field_data ? void 0 : _ctx_field_data.selectedRowKeys;
            if (!__WEBPACK_EXTERNAL_MODULE_lodash__["default"].isEqual(field.value, data)) {
                field.value = data;
                null == field || field.setInitialValue(data);
            }
            field.data = field.data || {};
            if (!__WEBPACK_EXTERNAL_MODULE_lodash__["default"].isEqual(field.data.selectedRowKeys, selectedRowKeys)) field.data.selectedRowKeys = selectedRowKeys;
            field.componentProps.pagination = field.componentProps.pagination || {};
            field.componentProps.pagination.pageSize = null == meta ? void 0 : meta.pageSize;
            field.componentProps.pagination.total = null == meta ? void 0 : meta.count;
            field.componentProps.pagination.current = null == meta ? void 0 : meta.page;
        }
    }, [
        null == ctx ? void 0 : null === (_ctx_field = ctx.field) || void 0 === _ctx_field ? void 0 : null === (_ctx_field_data = _ctx_field.data) || void 0 === _ctx_field_data ? void 0 : _ctx_field_data.selectedRowKeys,
        null == ctx ? void 0 : null === (_ctx_service2 = ctx.service) || void 0 === _ctx_service2 ? void 0 : _ctx_service2.data,
        field,
        isLoading
    ]);
    return {
        childrenColumnName: ctx.childrenColumnName,
        loading: null == ctx ? void 0 : null === (_ctx_service3 = ctx.service) || void 0 === _ctx_service3 ? void 0 : _ctx_service3.loading,
        showIndex: ctx.showIndex,
        dragSort: ctx.dragSort && ctx.dragSortBy,
        rowKey: ctx.rowKey || 'id',
        pagination: (null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.pagination) !== false && field.componentProps.pagination,
        onRowSelectionChange (selectedRowKeys) {
            var _ctx_field, _ctx_field_onRowSelect, _ctx_field1;
            ctx.field.data = (null == ctx ? void 0 : null === (_ctx_field = ctx.field) || void 0 === _ctx_field ? void 0 : _ctx_field.data) || {};
            ctx.field.data.selectedRowKeys = selectedRowKeys;
            null == ctx || null === (_ctx_field1 = ctx.field) || void 0 === _ctx_field1 || null === (_ctx_field_onRowSelect = _ctx_field1.onRowSelect) || void 0 === _ctx_field_onRowSelect || _ctx_field_onRowSelect.call(_ctx_field1, selectedRowKeys);
        },
        async onRowDragEnd (param) {
            let { from, to } = param;
            await ctx.resource.move({
                sourceId: from[ctx.rowKey || 'id'],
                targetId: to[ctx.rowKey || 'id'],
                sortField: ctx.dragSort && ctx.dragSortBy
            });
            ctx.service.refresh();
        },
        onChange (param, filters, sorter) {
            let { current, pageSize } = param;
            var _fieldSchema_parent_xdecoratorprops_params, _fieldSchema_parent_xdecoratorprops, _fieldSchema_parent, _ctx_service_params;
            const parentSort = null === (_fieldSchema_parent = fieldSchema.parent) || void 0 === _fieldSchema_parent ? void 0 : null === (_fieldSchema_parent_xdecoratorprops = _fieldSchema_parent['x-decorator-props']) || void 0 === _fieldSchema_parent_xdecoratorprops ? void 0 : null === (_fieldSchema_parent_xdecoratorprops_params = _fieldSchema_parent_xdecoratorprops['params']) || void 0 === _fieldSchema_parent_xdecoratorprops_params ? void 0 : _fieldSchema_parent_xdecoratorprops_params.sort;
            const sortParams = ctx.params.sort || [];
            const parentSortReal = [
                ...new Set([
                    ...sortParams,
                    ...parentSort || []
                ])
            ];
            const sort = globalSort ? globalSort : sorter.order ? "ascend" === sorter.order ? [
                sorter.field
            ] : [
                "-".concat(sorter.field)
            ] : parentSortReal;
            ctx.service.run({
                ...null === (_ctx_service_params = ctx.service.params) || void 0 === _ctx_service_params ? void 0 : _ctx_service_params[0],
                page: current,
                pageSize,
                sort
            });
        },
        onClickRow (record, setSelectedRow, selectedRow) {
            const { targets, uid } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.findFilterTargets)(fieldSchema);
            const dataBlocks = getDataBlocks();
            if (!targets || !targets.some((target)=>dataBlocks.some((dataBlock)=>dataBlock.uid === target.uid))) {
                setSelectedRow((prev)=>prev.length ? [] : prev);
                return;
            }
            const value = [
                record[ctx.rowKey]
            ];
            dataBlocks.forEach((block)=>{
                var _block_service_params, _block_service_params_, _block_service_params1;
                const target = targets.find((target)=>target.uid === block.uid);
                if (!target) return;
                const param = (null === (_block_service_params = block.service.params) || void 0 === _block_service_params ? void 0 : _block_service_params[0]) || {};
                const storedFilter = (null === (_block_service_params1 = block.service.params) || void 0 === _block_service_params1 ? void 0 : null === (_block_service_params_ = _block_service_params1[1]) || void 0 === _block_service_params_ ? void 0 : _block_service_params_.filters) || {};
                if (selectedRow.includes(record[ctx.rowKey])) {
                    if ('manual' === block.dataLoadingMode) return block.clearData();
                    delete storedFilter[uid];
                } else storedFilter[uid] = {
                    $and: [
                        {
                            [target.field || ctx.rowKey]: {
                                [target.field ? '$in' : '$eq']: value
                            }
                        }
                    ]
                };
                const mergedFilter = (0, __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__.mergeFilter)([
                    ...Object.values(storedFilter).map((filter)=>(0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.removeNullCondition)(filter)),
                    block.defaultFilter
                ]);
                return block.doFilter({
                    ...param,
                    page: 1,
                    filter: mergedFilter
                }, {
                    filters: storedFilter
                });
            });
            setSelectedRow((prev)=>(null == prev ? void 0 : prev.includes(record[ctx.rowKey])) ? [] : [
                    ...value
                ]);
        },
        onExpand (expanded, record) {
            var _ctx_field_onExpandClick, _ctx_field;
            null == ctx || null === (_ctx_field_onExpandClick = (_ctx_field = ctx.field).onExpandClick) || void 0 === _ctx_field_onExpandClick || _ctx_field_onExpandClick.call(_ctx_field, expanded, record);
        }
    };
};
export { useTableBlockProps };
