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