"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__, {
    SecondLevelSelect: ()=>SecondLevelSelect
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const icons_namespaceObject = require("@ant-design/icons");
const external_antd_namespaceObject = require("antd");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../../../data-source/index.js");
const external_schema_component_index_js_namespaceObject = require("../../../../schema-component/index.js");
const ReadPretty_js_namespaceObject = require("../../../../schema-component/antd/select/ReadPretty.js");
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, schema_namespaceObject.connect)((props)=>{
    const { objectValue, loading, value, rawOptions, defaultValue, firstLevelValue, collectionField, ...others } = props;
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const cm = (0, index_js_namespaceObject.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, schema_namespaceObject.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, external_react_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
        role: "button",
        "data-testid": "select-".concat(mode || 'single'),
        showSearch: true,
        filterOption: filterOption,
        allowClear: {
            clearIcon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.CloseCircleFilled, {
                role: "button",
                "aria-label": "icon-close-select"
            })
        },
        popupMatchSelectWidth: false,
        notFoundContent: loading ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Spin, {}) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Empty, {
            image: external_antd_namespaceObject.Empty.PRESENTED_IMAGE_SIMPLE
        }),
        value: toValue(newValue),
        defaultValue: toValue(defaultValue),
        tagRender: (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                role: "button",
                "aria-label": props.label,
                closeIcon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.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, schema_namespaceObject.mapProps)({
    dataSource: 'options'
}, (props, field)=>({
        ...props,
        fieldNames: {
            ...external_schema_component_index_js_namespaceObject.defaultFieldNames,
            ...props.fieldNames
        },
        suffixIcon: (null == field ? void 0 : field['loading']) || (null == field ? void 0 : field['validating']) ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.LoadingOutlined, {}) : props.suffixIcon
    })), (0, schema_namespaceObject.mapReadPretty)(ReadPretty_js_namespaceObject.ReadPretty));
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
});
