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__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_classnames__ from "classnames";
import * as __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__ from "../__builtins__/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__ from "../../../application/hoc/withDynamicSchemaProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__common_index_mjs__ from "../../common/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__input_index_mjs__ from "../input/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_picker_index_mjs__ from "../record-picker/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__AssociationFilter_mjs__ from "./AssociationFilter.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__AssociationFilter_Item_style_mjs__ from "./AssociationFilter.Item.style.mjs";
const { Panel } = __WEBPACK_EXTERNAL_MODULE_antd__.Collapse;
const AssociationFilterItem = (0, __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__.withDynamicSchemaProps)((props)=>{
    var _fieldSchema_xcomponentprops_fieldNames, _fieldSchema_xcomponentprops, _collectionField_uiSchema;
    const { wrapSSR, componentCls, hashId } = (0, __WEBPACK_EXTERNAL_MODULE__AssociationFilter_Item_style_mjs__["default"])();
    const { token } = (0, __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__.useToken)();
    const collectionField = __WEBPACK_EXTERNAL_MODULE__AssociationFilter_mjs__.AssociationFilter.useAssociationField();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const Designer = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useDesigner)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const { list, onSelected, handleSearchInput: _handleSearchInput, params, run, valueKey: _valueKey, labelKey: _labelKey, defaultCollapse } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useProps)(props);
    const [searchVisible, setSearchVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const defaultActiveKeyCollapse = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>defaultCollapse && (null == collectionField ? void 0 : collectionField.name) ? [
            collectionField.name
        ] : [], []);
    const valueKey = _valueKey || (null == collectionField ? void 0 : collectionField.targetKey) || 'id';
    const labelKey = _labelKey || (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : null === (_fieldSchema_xcomponentprops_fieldNames = _fieldSchema_xcomponentprops.fieldNames) || void 0 === _fieldSchema_xcomponentprops_fieldNames ? void 0 : _fieldSchema_xcomponentprops_fieldNames.label) || valueKey;
    const fieldNames = {
        title: labelKey || valueKey,
        key: valueKey
    };
    const [expandedKeys, setExpandedKeys] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const [selectedKeys, setSelectedKeys] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const [autoExpandParent, setAutoExpandParent] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(true);
    const labelUiSchema = (0, __WEBPACK_EXTERNAL_MODULE__record_picker_index_mjs__.useLabelUiSchema)(collectionField, (null == fieldNames ? void 0 : fieldNames.title) || 'label');
    if (!collectionField) return null;
    const onExpand = (expandedKeysValue)=>{
        setExpandedKeys(expandedKeysValue);
        setAutoExpandParent(false);
    };
    const onSelect = (selectedKeysValue)=>{
        setSelectedKeys(selectedKeysValue);
        onSelected(selectedKeysValue);
    };
    const handleSearchToggle = (e)=>{
        var _params_;
        const filter = null == params ? void 0 : null === (_params_ = params[0]) || void 0 === _params_ ? void 0 : _params_.filter;
        if (searchVisible || filter) run({
            ...null == params ? void 0 : params[0],
            filter: void 0
        });
        setSearchVisible(!searchVisible);
        e.stopPropagation();
    };
    const handleSearchClick = (e)=>{
        e.stopPropagation();
    };
    const handleSearchInput = (e)=>{
        _handleSearchInput(e);
    };
    var _fieldSchema_title;
    const title = null !== (_fieldSchema_title = fieldSchema.title) && void 0 !== _fieldSchema_title ? _fieldSchema_title : null == collectionField ? void 0 : null === (_collectionField_uiSchema = collectionField.uiSchema) || void 0 === _collectionField_uiSchema ? void 0 : _collectionField_uiSchema.title;
    return wrapSSR(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__common_index_mjs__.SortableItem, {
        className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])(componentCls, hashId, 'tb-block-item', props.className, 'SortableItem'),
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Designer, {}),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Collapse, {
                defaultActiveKey: defaultActiveKeyCollapse,
                ghost: true,
                expandIcon: searchVisible ? ()=>null : void 0,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Panel, {
                    className: "Panel",
                    header: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Row, {
                        className: "headerRow",
                        style: {
                            borderBottom: searchVisible ? "1px solid ".concat(token.colorBorder) : 'none'
                        },
                        gutter: 5,
                        children: [
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Col, {
                                title: compile(title),
                                className: "headerCol",
                                children: searchVisible ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Input, {
                                    bordered: false,
                                    autoFocus: true,
                                    placeholder: "Search...",
                                    className: "search",
                                    onClick: handleSearchClick,
                                    onChange: handleSearchInput
                                }) : compile(title)
                            }),
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Col, {
                                style: {
                                    flex: '0 0 auto'
                                },
                                children: searchVisible ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.CloseOutlined, {
                                    className: "CloseOutlined",
                                    onClick: handleSearchToggle
                                }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.SearchOutlined, {
                                    className: "SearchOutlined",
                                    onClick: handleSearchToggle
                                })
                            })
                        ]
                    }),
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Tree, {
                        className: "Tree",
                        onExpand: onExpand,
                        expandedKeys: expandedKeys,
                        autoExpandParent: autoExpandParent,
                        treeData: list,
                        onSelect: onSelect,
                        fieldNames: fieldNames,
                        titleRender: (node)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__input_index_mjs__.EllipsisWithTooltip, {
                                ellipsis: true,
                                children: (0, __WEBPACK_EXTERNAL_MODULE__record_picker_index_mjs__.getLabelFormatValue)(labelUiSchema, compile(node[labelKey]))
                            }),
                        selectedKeys: selectedKeys,
                        blockNode: true
                    })
                }, defaultActiveKeyCollapse[0])
            })
        ]
    }));
}, {
    displayName: 'AssociationFilterItem'
});
export { AssociationFilterItem };
