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_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__ from "../../../../data-source/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_select_ReadPretty_mjs__ from "../../../../schema-component/antd/select/ReadPretty.mjs";
const filterOption = (input, option)=>{
    var _option_label;
    return (null !== (_option_label = null == option ? void 0 : option.label) && void 0 !== _option_label ? _option_label : '').toLowerCase().includes((input || '').toLowerCase());
};
const SecondLevelSelect = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const { objectValue, loading, value, rawOptions, defaultValue, firstLevelValue, collectionField, ...others } = props;
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const cm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollectionManager)();
    let mode = props.multiple ? 'multiple' : props.mode;
    if (mode && ![
        'multiple',
        'tags'
    ].includes(mode)) mode = void 0;
    const toValue = (v)=>{
        if ([
            'tags',
            'multiple'
        ].includes(props.mode) || props.multiple) {
            if (v) return (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.toArr)(v);
            return;
        }
        return v;
    };
    const Parentoptions = cm.getCollectionFields("".concat(collectionField, ".").concat(firstLevelValue));
    const fieldParentTabsOptions = Parentoptions.map((item)=>{
        if ('m2o' === item.interface) return {
            ...item,
            label: compile(item.uiSchema.title),
            value: item.name
        };
    }).filter(Boolean);
    fieldParentTabsOptions.unshift({
        label: t('none'),
        value: 'none'
    });
    const matchedOption = fieldParentTabsOptions.find((option)=>option.value === value);
    const newValue = matchedOption ? value : 'none';
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (!matchedOption) {
            var _props_onChange;
            null === (_props_onChange = props.onChange) || void 0 === _props_onChange || _props_onChange.call(props, 'none');
        }
    }, [
        value,
        fieldParentTabsOptions
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Select, {
        role: "button",
        "data-testid": "select-".concat(mode || 'single'),
        showSearch: true,
        filterOption: filterOption,
        allowClear: {
            clearIcon: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.CloseCircleFilled, {
                role: "button",
                "aria-label": "icon-close-select"
            })
        },
        popupMatchSelectWidth: false,
        notFoundContent: loading ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Spin, {}) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Empty, {
            image: __WEBPACK_EXTERNAL_MODULE_antd__.Empty.PRESENTED_IMAGE_SIMPLE
        }),
        value: toValue(newValue),
        defaultValue: toValue(defaultValue),
        tagRender: (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Tag, {
                role: "button",
                "aria-label": props.label,
                closeIcon: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.CloseOutlined, {
                    role: "button",
                    "aria-label": "icon-close-tag"
                }),
                ...props,
                children: props.label
            }),
        options: fieldParentTabsOptions,
        ...others,
        onChange: (changed)=>{
            var _props_onChange;
            null === (_props_onChange = props.onChange) || void 0 === _props_onChange || _props_onChange.call(props, void 0 === changed ? null : changed);
        },
        mode: mode
    });
}, (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapProps)({
    dataSource: 'options'
}, (props, field)=>({
        ...props,
        fieldNames: {
            ...__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.defaultFieldNames,
            ...props.fieldNames
        },
        suffixIcon: (null == field ? void 0 : field['loading']) || (null == field ? void 0 : field['validating']) ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.LoadingOutlined, {}) : props.suffixIcon
    })), (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapReadPretty)(__WEBPACK_EXTERNAL_MODULE__schema_component_antd_select_ReadPretty_mjs__.ReadPretty));
export { SecondLevelSelect };
