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__index_mjs__ from "../../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_TableSelectorProvider_mjs__ from "../../../block-provider/TableSelectorProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_record_picker_index_mjs__ from "../../../schema-component/antd/record-picker/index.mjs";
const useTableSelectorProps = ()=>{
    const { setSelectedRows } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__schema_component_antd_record_picker_index_mjs__.RecordPickerContext);
    const { onRowSelectionChange, rowKey = 'id', ...others } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_TableSelectorProvider_mjs__.useTableSelectorProps)();
    return {
        ...others,
        rowKey,
        rowSelection: {
            type: 'checkbox'
        },
        onRowSelectionChange (selectedRowKeys, selectedRows) {
            null == setSelectedRows || setSelectedRows(selectedRowKeys);
            null == onRowSelectionChange || onRowSelectionChange(selectedRowKeys, selectedRows);
        }
    };
};
const AssociateActionProvider = (props)=>{
    const [selectedRows, setSelectedRows] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCollection)();
    const { resource, service, block, __parent } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useBlockRequestContext)();
    const pickerProps = {
        size: 'small',
        onChange: null == props ? void 0 : props.onChange,
        selectedRows,
        setSelectedRows
    };
    const usePickActionProps = ()=>{
        const { selectedRows } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__schema_component_antd_record_picker_index_mjs__.RecordPickerContext);
        const { setVisible, setFormValueChanged } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useActionContext)();
        return {
            async onClick (e, callBack) {
                await resource.add({
                    values: selectedRows
                });
                null == callBack || callBack();
                null == setVisible || setVisible(false);
                if (block && 'TableField' !== block) {
                    var __parent_service_refresh, __parent_service;
                    null == __parent || null === (__parent_service = __parent.service) || void 0 === __parent_service || null === (__parent_service_refresh = __parent_service.refresh) || void 0 === __parent_service_refresh || __parent_service_refresh.call(__parent_service);
                    null == setFormValueChanged || setFormValueChanged(false);
                }
            }
        };
    };
    const getFilter = ()=>{
        var _service_data;
        const targetKey = (null == collection ? void 0 : collection.filterTargetKey) || 'id';
        if (null === (_service_data = service.data) || void 0 === _service_data ? void 0 : _service_data.data) {
            var _service_data1;
            const list = null === (_service_data1 = service.data) || void 0 === _service_data1 ? void 0 : _service_data1.data.map((option)=>option[targetKey]).filter(Boolean);
            const filter = list.length ? {
                $and: [
                    {
                        ["".concat(targetKey, ".$ne")]: list
                    }
                ]
            } : {};
            return filter;
        }
        return {};
    };
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_antd_record_picker_index_mjs__.RecordPickerProvider, {
        ...pickerProps,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaComponentOptions, {
            scope: {
                useTableSelectorProps,
                usePickActionProps
            },
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__block_provider_TableSelectorProvider_mjs__.TableSelectorParamsProvider, {
                params: {
                    filter: getFilter()
                },
                children: props.children
            })
        })
    });
};
export { AssociateActionProvider };
