"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    useTableBlockProps: ()=>useTableBlockProps
});
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_flat_namespaceObject = require("flat");
var external_flat_default = /*#__PURE__*/ __webpack_require__.n(external_flat_namespaceObject);
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const index_js_namespaceObject = require("../../../../../block-provider/hooks/index.js");
const TableBlockProvider_js_namespaceObject = require("../../../../../block-provider/TableBlockProvider.js");
const FilterProvider_js_namespaceObject = require("../../../../../filter-provider/FilterProvider.js");
const utils_js_namespaceObject = require("../../../../../filter-provider/utils.js");
const external_schema_component_index_js_namespaceObject = require("../../../../../schema-component/index.js");
const useTableBlockProps = ()=>{
    var _fieldSchema_parent_xdecoratorprops, _fieldSchema_parent, _fieldSchema_parent_xdecoratorprops1, _fieldSchema_parent1, _ctx_service, _ctx_service1, _ctx_field_data, _ctx_field, _ctx_service2, _ctx_service3, _fieldSchema_xcomponentprops;
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const ctx = (0, TableBlockProvider_js_namespaceObject.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['dragSort']) === true ? null === (_fieldSchema_parent1 = fieldSchema.parent) || void 0 === _fieldSchema_parent1 ? void 0 : null === (_fieldSchema_parent_xdecoratorprops1 = _fieldSchema_parent1['x-decorator-props']) || void 0 === _fieldSchema_parent_xdecoratorprops1 ? void 0 : _fieldSchema_parent_xdecoratorprops1['dragSortBy'] : void 0;
    const { getDataBlocks } = (0, FilterProvider_js_namespaceObject.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, external_react_namespaceObject.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 (!external_lodash_default().isEqual(field.value, data)) {
                field.value = data;
                null == field || field.setInitialValue(data);
            }
            field.data = field.data || {};
            if (!external_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({
                ...ctx.service.params[0],
                page: current,
                pageSize,
                sort
            }, {
                filters: null === (_ctx_service_params_ = ctx.service.params[1]) || void 0 === _ctx_service_params_ ? void 0 : _ctx_service_params_.filters
            });
        },
        onClickRow (record, setSelectedRow, selectedRow) {
            const { targets, uid } = (0, index_js_namespaceObject.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]
            ];
            let prevMergedFilter = {};
            dataBlocks.forEach((block)=>{
                var _block_service_params, _block_service_params_, _block_service_params1, _block_service_params_1, _block_service_params2, _block_service;
                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: [
                        {
                            [(null == target ? void 0 : target.field) || ctx.rowKey]: {
                                [(null == target ? void 0 : target.field) ? '$in' : '$eq']: value
                            }
                        }
                    ]
                };
                const items = external_flat_default()((null === (_block_service = block.service) || void 0 === _block_service ? void 0 : null === (_block_service_params2 = _block_service.params) || void 0 === _block_service_params2 ? void 0 : null === (_block_service_params_1 = _block_service_params2[0]) || void 0 === _block_service_params_1 ? void 0 : _block_service_params_1.filter) || {});
                Object.entries(items).forEach((param)=>{
                    let [key, value] = param;
                    if (key.includes(ctx.rowKey) && selectedRow.includes(value)) delete items[key];
                });
                const mergedFilter = (0, utils_js_namespaceObject.mergeFilter)([
                    ...Object.values(storedFilter).map((filter)=>(0, external_schema_component_index_js_namespaceObject.removeNullCondition)(filter)),
                    external_flat_default().unflatten(items),
                    block.defaultFilter,
                    prevMergedFilter
                ]);
                const currFilter = (0, index_js_namespaceObject.filterByCleanedFields)(mergedFilter);
                prevMergedFilter = currFilter;
                return block.doFilter({
                    ...param,
                    page: 1,
                    filter: currFilter
                }, {
                    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);
        }
    };
};
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
});
