import { jsx } from "react/jsx-runtime";
import { useContext, useState } from "react";
import { SchemaComponentOptions, useActionContext, useBlockRequestContext, useCollection } from "../../../index.mjs";
import { TableSelectorParamsProvider, useTableSelectorProps } from "../../../block-provider/TableSelectorProvider.mjs";
import { RecordPickerContext, RecordPickerProvider } from "../../../schema-component/antd/record-picker/index.mjs";
const AssociateActionProvider_useTableSelectorProps = ()=>{
    const { setSelectedRows } = useContext(RecordPickerContext);
    const { onRowSelectionChange, rowKey = 'id', ...others } = 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] = useState([]);
    const collection = useCollection();
    const { resource, service, block, __parent } = useBlockRequestContext();
    const pickerProps = {
        size: 'small',
        onChange: null == props ? void 0 : props.onChange,
        selectedRows,
        setSelectedRows
    };
    const usePickActionProps = ()=>{
        const { selectedRows } = useContext(RecordPickerContext);
        const { setVisible, setFormValueChanged } = 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) || null == (__parent_service_refresh = __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.data) {
            var _service_data1;
            const list = null == (_service_data1 = service.data) ? 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__*/ jsx(RecordPickerProvider, {
        ...pickerProps,
        children: /*#__PURE__*/ jsx(SchemaComponentOptions, {
            scope: {
                useTableSelectorProps: AssociateActionProvider_useTableSelectorProps,
                usePickActionProps
            },
            children: /*#__PURE__*/ jsx(TableSelectorParamsProvider, {
                params: {
                    filter: getFilter()
                },
                children: props.children
            })
        })
    });
};
export { AssociateActionProvider };
