import { jsx } from "react/jsx-runtime";
import "react";
import { usePrefixCls } from "@tachybase/components";
import { isArr, observer, useField } from "@tachybase/schema";
import { Tag } from "antd";
import classnames from "classnames";
const ReadPretty = observer((props)=>{
    var _field_dataSource, _props_treeData;
    const field = useField();
    const { placeholder } = props;
    const prefixCls = usePrefixCls("description-tree-select", props);
    const dataSource = (null == field ? void 0 : null == (_field_dataSource = field.dataSource) ? void 0 : _field_dataSource.length) ? field.dataSource : (null == props ? void 0 : null == (_props_treeData = props.treeData) ? void 0 : _props_treeData.length) ? props.treeData : [];
    const getSelected = ()=>{
        const value = props.value;
        if (props.multiple) if (props.labelInValue) return isArr(value) ? value : [];
        else return isArr(value) ? value.map((val)=>({
                label: val,
                value: val
            })) : [];
        if (props.labelInValue) return value ? [
            value
        ] : [];
        return value ? [
            {
                label: value,
                value
            }
        ] : [];
    };
    const findLabel = (value, dataSource)=>{
        for(let i = 0; i < (null == dataSource ? void 0 : dataSource.length); i++){
            const item = dataSource[i];
            if ((null == item ? void 0 : item.value) === value) return null == item ? void 0 : item.label;
            {
                const childLabel = findLabel(value, null == item ? void 0 : item.children);
                if (childLabel) return childLabel;
            }
        }
    };
    const getLabels = ()=>{
        const selected = getSelected();
        if (!(null == selected ? void 0 : selected.length)) return /*#__PURE__*/ jsx(Tag, {
            children: placeholder
        });
        return selected.map((param, key)=>{
            let { value, label } = param;
            return /*#__PURE__*/ jsx(Tag, {
                children: findLabel(value, dataSource) || label || placeholder
            }, key);
        });
    };
    return /*#__PURE__*/ jsx("div", {
        className: classnames(prefixCls, props.className),
        style: props.style,
        children: getLabels()
    });
}, {
    displayName: 'ReadPretty'
});
export { ReadPretty };
