"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    Select: ()=>Select,
    default: ()=>Select_rslib_entry_
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
require("react");
const schema_namespaceObject = require("@tachybase/schema");
const client_namespaceObject = require("@tachybase/utils/client");
const icons_namespaceObject = require("@ant-design/icons");
const external_antd_namespaceObject = require("antd");
const external_FormulaSelect_js_namespaceObject = require("./FormulaSelect.js");
var external_FormulaSelect_js_default = /*#__PURE__*/ __webpack_require__.n(external_FormulaSelect_js_namespaceObject);
const external_ReadPretty_js_namespaceObject = require("./ReadPretty.js");
const external_utils_js_namespaceObject = require("./utils.js");
const isEmptyObject = (val)=>!(0, schema_namespaceObject.isValid)(val) || 'object' == typeof val && 0 === Object.keys(val).length;
const ObjectSelect = (props)=>{
    const { value, options, onChange, fieldNames, mode, loading, rawOptions, defaultValue, ...others } = props;
    const toValue = (v)=>{
        var _getCurrentOptions;
        if (isEmptyObject(v)) return;
        const values = (0, schema_namespaceObject.toArr)(v).filter((item)=>item).map((val)=>(0, client_namespaceObject.isPlainObject)(val) ? val[fieldNames.value] : val);
        const currentOptions = null === (_getCurrentOptions = (0, external_utils_js_namespaceObject.getCurrentOptions)(values, options, fieldNames)) || void 0 === _getCurrentOptions ? void 0 : _getCurrentOptions.map((val)=>({
                label: val[fieldNames.label],
                value: val[fieldNames.value]
            }));
        if ([
            'tags',
            'multiple'
        ].includes(mode) || props.multiple) return currentOptions;
        return currentOptions.shift();
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
        role: "button",
        "data-testid": "select-object-".concat(mode || 'single'),
        value: toValue(value),
        defaultValue: toValue(defaultValue),
        allowClear: {
            clearIcon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.CloseCircleFilled, {
                role: "button",
                "aria-label": "icon-close-select"
            })
        },
        labelInValue: true,
        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
        }),
        options: options,
        fieldNames: fieldNames,
        showSearch: true,
        popupMatchSelectWidth: false,
        filterOption: (input, option)=>{
            var _option_;
            return (null !== (_option_ = null == option ? void 0 : option[fieldNames.label || 'label']) && void 0 !== _option_ ? _option_ : '').includes(input);
        },
        filterSort: (optionA, optionB)=>{
            var _optionA_, _optionB_;
            return (null !== (_optionA_ = null == optionA ? void 0 : optionA[fieldNames.label || 'label']) && void 0 !== _optionA_ ? _optionA_ : '').toLowerCase().localeCompare((null !== (_optionB_ = null == optionB ? void 0 : optionB[fieldNames.label || 'label']) && void 0 !== _optionB_ ? _optionB_ : '').toLowerCase());
        },
        onChange: (changed)=>{
            const current = (0, external_utils_js_namespaceObject.getCurrentOptions)((0, schema_namespaceObject.toArr)(changed).map((v)=>v.value), rawOptions || options, fieldNames);
            if ([
                'tags',
                'multiple'
            ].includes(mode) || props.multiple) null == onChange || onChange(current);
            else null == onChange || onChange(current.shift() || null);
        },
        mode: mode,
        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
            }),
        ...others
    });
};
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 InternalSelect = (0, schema_namespaceObject.connect)((props)=>{
    const { objectValue, loading, value, rawOptions, defaultValue, ...others } = props;
    let mode = props.multiple ? 'multiple' : props.mode;
    if (mode && ![
        'multiple',
        'tags'
    ].includes(mode)) mode = void 0;
    if ('formula' in others.fieldNames || 'collection' in props) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_FormulaSelect_js_default(), {
        ...props
    });
    if (objectValue) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ObjectSelect, {
        rawOptions: rawOptions,
        ...others,
        defaultValue: defaultValue,
        value: value,
        mode: mode,
        loading: loading
    });
    const toValue = (v)=>{
        if ([
            'tags',
            'multiple'
        ].includes(props.mode) || props.multiple) {
            if (v) return (0, schema_namespaceObject.toArr)(v);
            return;
        }
        return v;
    };
    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(value),
        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
            }),
        ...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_utils_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)(external_ReadPretty_js_namespaceObject.ReadPretty));
const Select = InternalSelect;
Select.ReadPretty = external_ReadPretty_js_namespaceObject.ReadPretty;
const Select_rslib_entry_ = Select;
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
});
