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