"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__, {
    InternalCascadeSelect: ()=>InternalCascadeSelect,
    transformNestedData: ()=>transformNestedData
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const components_namespaceObject = require("@tachybase/components");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_dayjs_namespaceObject = require("dayjs");
var external_dayjs_default = /*#__PURE__*/ __webpack_require__.n(external_dayjs_namespaceObject);
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../../api-client/index.js");
const useCollectionManager_deprecated_js_namespaceObject = require("../../../collection-manager/hooks/useCollectionManager_deprecated.js");
const utils_js_namespaceObject = require("../../../filter-provider/utils.js");
const external_index_js_namespaceObject = require("../../index.js");
const external_hooks_js_namespaceObject = require("./hooks.js");
var external_hooks_js_default = /*#__PURE__*/ __webpack_require__.n(external_hooks_js_namespaceObject);
const EMPTY = 'N/A';
const SchemaField = (0, schema_namespaceObject.createSchemaField)({
    components: {
        Space: external_antd_namespaceObject.Space,
        Input: external_antd_namespaceObject.Input,
        ArrayItems: components_namespaceObject.ArrayItems,
        FormItem: components_namespaceObject.FormItem
    }
});
const CascadeSelect = (0, schema_namespaceObject.connect)((props)=>{
    var _associationField_componentProps;
    const { data, mapOptions, onChange, value } = props;
    const [selectedOptions, setSelectedOptions] = (0, external_react_namespaceObject.useState)([
        {
            key: void 0,
            children: [],
            value: null
        }
    ]);
    const [options, setOptions] = (0, external_react_namespaceObject.useState)(data);
    const [loading, setLoading] = (0, external_react_namespaceObject.useState)(false);
    const compile = (0, external_index_js_namespaceObject.useCompile)();
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const service = external_hooks_js_default()(props);
    const { options: collectionField, field: associationField } = (0, external_hooks_js_namespaceObject.useAssociationFieldContext)();
    const resource = api.resource(collectionField.target);
    const { getCollectionJoinField, getInterface } = (0, useCollectionManager_deprecated_js_namespaceObject.useCollectionManager_deprecated)();
    const fieldNames = null == associationField ? void 0 : null === (_associationField_componentProps = associationField.componentProps) || void 0 === _associationField_componentProps ? void 0 : _associationField_componentProps.fieldNames;
    const [change, setChange] = (0, external_react_namespaceObject.useState)(false);
    const targetField = (null == collectionField ? void 0 : collectionField.target) && (null == fieldNames ? void 0 : fieldNames.label) && getCollectionJoinField("".concat(collectionField.target, ".").concat(fieldNames.label));
    const operator = (0, external_react_namespaceObject.useMemo)(()=>{
        if (null == targetField ? void 0 : targetField.interface) {
            var _getInterface_filterable, _getInterface;
            return (null === (_getInterface = getInterface(targetField.interface)) || void 0 === _getInterface ? void 0 : null === (_getInterface_filterable = _getInterface.filterable) || void 0 === _getInterface_filterable ? void 0 : _getInterface_filterable.operators[0].value) || '$includes';
        }
        return '$includes';
    }, [
        targetField
    ]);
    const field = (0, schema_namespaceObject.useField)();
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (value && !change) {
            var _values_map;
            const values = Array.isArray(value) ? extractLastNonNullValueObjects(null == value ? void 0 : value.filter((v)=>v.value), true) : transformNestedData(value);
            const options = null == values ? void 0 : null === (_values_map = values.map) || void 0 === _values_map ? void 0 : _values_map.call(values, (v)=>({
                    key: v.parentId,
                    children: [],
                    value: v
                }));
            setSelectedOptions(options);
        }
    }, [
        value
    ]);
    const mapOptionsToTags = (0, external_react_namespaceObject.useCallback)((options)=>{
        try {
            return null == options ? void 0 : options.filter((v)=>[
                    'number',
                    'string'
                ].includes(typeof v[fieldNames.value])).map((option)=>{
                var _targetField_uiSchema;
                let label = compile(option[fieldNames.label]);
                if (null == targetField ? void 0 : null === (_targetField_uiSchema = targetField.uiSchema) || void 0 === _targetField_uiSchema ? void 0 : _targetField_uiSchema.enum) {
                    if (Array.isArray(label)) label = label.map((item, index)=>{
                        var _targetField_uiSchema_enum_find, _targetField_uiSchema_enum;
                        const option = null === (_targetField_uiSchema_enum_find = (_targetField_uiSchema_enum = targetField.uiSchema.enum).find) || void 0 === _targetField_uiSchema_enum_find ? void 0 : _targetField_uiSchema_enum_find.call(_targetField_uiSchema_enum, (i)=>i.value === item);
                        if (option) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                            color: option.color,
                            style: {
                                marginRight: 3
                            },
                            children: (null == option ? void 0 : option.label) || item
                        }, index);
                        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                            children: item
                        }, item);
                    }).reverse();
                    else {
                        var _targetField_uiSchema_enum_find, _targetField_uiSchema_enum;
                        const item = null === (_targetField_uiSchema_enum_find = (_targetField_uiSchema_enum = targetField.uiSchema.enum).find) || void 0 === _targetField_uiSchema_enum_find ? void 0 : _targetField_uiSchema_enum_find.call(_targetField_uiSchema_enum, (i)=>i.value === label);
                        if (item) label = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                            color: item.color,
                            children: item.label
                        });
                    }
                }
                if ((null == targetField ? void 0 : targetField.type) === 'date') label = external_dayjs_default()(label).format('YYYY-MM-DD');
                if (mapOptions) return mapOptions({
                    [fieldNames.label]: label || EMPTY,
                    [fieldNames.value]: option[fieldNames.value]
                });
                return {
                    ...option,
                    [fieldNames.label]: label || EMPTY,
                    [fieldNames.value]: option[fieldNames.value]
                };
            }).filter(Boolean);
        } catch (err) {
            console.error(err);
            return options;
        }
    }, [
        null == targetField ? void 0 : targetField.uiSchema,
        fieldNames
    ]);
    const handleGetOptions = async (filter)=>{
        var _service_params, _response_data;
        const response = await resource.list({
            pageSize: 200,
            params: null == service ? void 0 : service.params,
            filter: (0, utils_js_namespaceObject.mergeFilter)([
                null == service ? void 0 : null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params.filter,
                filter
            ]),
            tree: !filter.parentId || void 0
        });
        return null == response ? void 0 : null === (_response_data = response.data) || void 0 === _response_data ? void 0 : _response_data.data;
    };
    const handleSelect = async (value, option, index)=>{
        setChange(true);
        const data = await handleGetOptions({
            parentId: null == option ? void 0 : option.id
        });
        const options = [
            ...selectedOptions
        ];
        options.splice(index + 1);
        options[index] = {
            ...options[index],
            value: option
        };
        if (null == option ? void 0 : option.id) options[index + 1] = {
            key: null == option ? void 0 : option.id,
            children: (null == data ? void 0 : data.length) > 0 ? data : null
        };
        setSelectedOptions(options);
        if ([
            'o2m',
            'm2m'
        ].includes(collectionField.interface)) {
            const fieldValue = Array.isArray(associationField.fieldValue) ? associationField.fieldValue : [];
            fieldValue[field.index] = option;
            associationField.fieldValue = fieldValue;
        } else associationField.value = option;
        null == onChange || onChange(options);
    };
    const onDropdownVisibleChange = async (visible, selectedValue, index)=>{
        if (visible) {
            var _selectedValue_value;
            setLoading(true);
            const result = await handleGetOptions({
                parentId: null == selectedValue ? void 0 : selectedValue.key
            });
            setLoading(false);
            setOptions(result);
            if (index === (null == selectedOptions ? void 0 : selectedOptions.length) - 1 && (null == selectedValue ? void 0 : null === (_selectedValue_value = selectedValue.value) || void 0 === _selectedValue_value ? void 0 : _selectedValue_value.id)) {
                var _selectedValue_value1, _selectedValue_value2;
                const data = await handleGetOptions({
                    parentId: null == selectedValue ? void 0 : null === (_selectedValue_value1 = selectedValue.value) || void 0 === _selectedValue_value1 ? void 0 : _selectedValue_value1.id
                });
                const options = [
                    ...selectedOptions
                ];
                options.splice(index + 1);
                options[index] = {
                    ...options[index],
                    value: null == selectedValue ? void 0 : selectedValue.value
                };
                options[index + 1] = {
                    key: null == selectedValue ? void 0 : null === (_selectedValue_value2 = selectedValue.value) || void 0 === _selectedValue_value2 ? void 0 : _selectedValue_value2.id,
                    children: (null == data ? void 0 : data.length) > 0 ? data : null
                };
                setSelectedOptions(options);
                null == onChange || onChange(options);
            }
        }
    };
    const onSearch = async (search, selectedValue)=>{
        const serachParam = search ? {
            [fieldNames.label]: {
                [operator]: search
            }
        } : {};
        setLoading(true);
        const result = await handleGetOptions({
            ...serachParam,
            parentId: null == selectedValue ? void 0 : selectedValue.key
        });
        setLoading(false);
        setOptions(result);
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Space, {
        wrap: true,
        children: selectedOptions.map((value, index)=>{
            var _value_value, _value_value1, _value_value2;
            return value.children && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
                disabled: associationField.disabled,
                allowClear: true,
                showSearch: true,
                autoClearSearchValue: true,
                filterOption: false,
                filterSort: null,
                defaultValue: {
                    label: null == value ? void 0 : null === (_value_value = value.value) || void 0 === _value_value ? void 0 : _value_value[fieldNames.label],
                    value: null == value ? void 0 : null === (_value_value1 = value.value) || void 0 === _value_value1 ? void 0 : _value_value1[fieldNames.value]
                },
                labelInValue: true,
                onSearch: (search)=>onSearch(search, value),
                fieldNames: fieldNames,
                style: {
                    minWidth: 150
                },
                onChange: (value, option)=>handleSelect(value, option, index),
                options: loading ? [] : mapOptionsToTags(options),
                onDropdownVisibleChange: (open)=>onDropdownVisibleChange(open, value, index),
                notFoundContent: loading ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Spin, {
                    size: "small"
                }) : null
            }, "".concat(null === (_value_value2 = value.value) || void 0 === _value_value2 ? void 0 : _value_value2.id, "+ ").concat(value.key, " + ").concat(fieldNames.label));
        })
    });
});
const AssociationCascadeSelect = (0, schema_namespaceObject.connect)((props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CascadeSelect, {
            ...props
        })
    }));
const InternalCascadeSelect = (0, schema_namespaceObject.observer)((props)=>{
    const { options: collectionField } = (0, external_hooks_js_namespaceObject.useAssociationFieldContext)();
    const selectForm = (0, external_react_namespaceObject.useMemo)(()=>(0, schema_namespaceObject.createForm)(), []);
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    (0, external_react_namespaceObject.useEffect)(()=>{
        const id = (0, schema_namespaceObject.uid)();
        selectForm.addEffects(id, ()=>{
            (0, schema_namespaceObject.onFormValuesChange)((form)=>{
                if ('m2o' === collectionField.interface) {
                    var _form_values;
                    const value = extractLastNonNullValueObjects(null === (_form_values = form.values) || void 0 === _form_values ? void 0 : _form_values[fieldSchema.name]);
                    setTimeout(()=>{
                        form.setValuesIn(fieldSchema.name, value);
                        props.onChange(value);
                        field.value = value;
                    });
                } else {
                    var _form_values1;
                    const value = extractLastNonNullValueObjects(null === (_form_values1 = form.values) || void 0 === _form_values1 ? void 0 : _form_values1.select_array).filter((v)=>v && Object.keys(v).length > 0);
                    setTimeout(()=>{
                        field.value = value;
                        props.onChange(value);
                    });
                }
            });
        });
        return ()=>{
            selectForm.removeEffects(id);
        };
    }, []);
    const toValue = ()=>{
        if (Array.isArray(field.value) && field.value.length > 0) return field.value;
        return [
            {}
        ];
    };
    const defaultValue = toValue();
    (0, external_react_namespaceObject.useEffect)(()=>{
        var _selectForm_values_select_array, _selectForm_values;
        const isChange = null === (_selectForm_values = selectForm.values) || void 0 === _selectForm_values ? void 0 : null === (_selectForm_values_select_array = _selectForm_values.select_array) || void 0 === _selectForm_values_select_array ? void 0 : _selectForm_values_select_array.filter((value)=>value && Object.keys(value).length);
        if ('m2o' !== collectionField.interface && props.value && !isChange.length) {
            const value = extractLastNonNullValueObjects(defaultValue).filter((v)=>v && Object.keys(v).length > 0);
            selectForm.setValues({
                select_array: value
            });
        }
    }, [
        props.value
    ]);
    const schema = {
        type: 'object',
        properties: {
            select_array: {
                type: 'array',
                'x-component': 'ArrayItems',
                'x-decorator': 'FormItem',
                default: defaultValue,
                items: {
                    type: 'void',
                    'x-component': 'Space',
                    properties: {
                        sort: {
                            type: 'void',
                            'x-decorator': 'FormItem',
                            'x-component': 'ArrayItems.SortHandle'
                        },
                        select: {
                            type: 'string',
                            'x-decorator': 'FormItem',
                            'x-component': AssociationCascadeSelect,
                            'x-component-props': {
                                ...props
                            }
                        },
                        remove: {
                            type: 'void',
                            'x-decorator': 'FormItem',
                            'x-component': 'ArrayItems.Remove'
                        }
                    }
                },
                properties: {
                    add: {
                        type: 'void',
                        title: t('Add new'),
                        'x-component': 'ArrayItems.Addition'
                    }
                }
            }
        }
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.FormProvider, {
        form: selectForm,
        children: 'm2o' === collectionField.interface ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.SchemaComponent, {
            components: {
                FormItem: components_namespaceObject.FormItem
            },
            schema: {
                ...fieldSchema,
                default: field.value,
                title: '',
                'x-component': AssociationCascadeSelect,
                'x-component-props': {
                    ...props
                }
            }
        }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(SchemaField, {
            schema: schema
        })
    });
}, {
    displayName: 'InternalCascadeSelect'
});
function extractLastNonNullValueObjects(data, flag) {
    let result = [];
    if (!Array.isArray(data)) return data;
    for (const sublist of data){
        let lastNonNullValue = null;
        if (Array.isArray(sublist)) {
            for(let i = (null == sublist ? void 0 : sublist.length) - 1; i >= 0; i--)if (sublist[i].value) {
                lastNonNullValue = sublist[i].value;
                break;
            }
            if (lastNonNullValue) result.push(lastNonNullValue);
        } else {
            lastNonNullValue = (null == sublist ? void 0 : sublist.value) ? sublist.value : null;
            if (lastNonNullValue) {
                if (flag) {
                    var _result_push;
                    null == result || null === (_result_push = result.push) || void 0 === _result_push || _result_push.call(result, lastNonNullValue);
                } else result = lastNonNullValue;
            } else {
                var _result_push1;
                null == result || null === (_result_push1 = result.push) || void 0 === _result_push1 || _result_push1.call(result, sublist);
            }
        }
    }
    return result;
}
function transformNestedData(inputData) {
    const resultArray = [];
    function recursiveTransform(data) {
        if (null == data ? void 0 : data.parent) {
            const { parent } = data;
            recursiveTransform(parent);
        }
        const { parent, ...other } = data;
        resultArray.push(other);
    }
    if (inputData) recursiveTransform(inputData);
    return resultArray;
}
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
});
