"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__, {
    AssociationSelect: ()=>AssociationSelect,
    filterAnalyses: ()=>filterAnalyses,
    AssociationSelectReadPretty: ()=>AssociationSelectReadPretty
});
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_mathjs_namespaceObject = require("mathjs");
const external_react_i18next_namespaceObject = require("react-i18next");
const external_index_js_namespaceObject = require("../../../index.js");
const isVariable_js_namespaceObject = require("../../../variables/utils/isVariable.js");
const uitls_js_namespaceObject = require("../../common/utils/uitls.js");
const index_js_namespaceObject = require("../remote-select/index.js");
const external_hooks_js_namespaceObject = require("./hooks.js");
var external_hooks_js_default = /*#__PURE__*/ __webpack_require__.n(external_hooks_js_namespaceObject);
const filterAnalyses = (filters)=>{
    if (!filters) return;
    const type = Object.keys(filters)[0] || '$and';
    const conditions = filters[type];
    const results = [];
    null == conditions || conditions.map((c)=>{
        var _jsonlogic_value_match, _jsonlogic_value_match1, _jsonlogic_value;
        const jsonlogic = (0, uitls_js_namespaceObject.getInnermostKeyAndValue)(c);
        const operator = null == jsonlogic ? void 0 : jsonlogic.key;
        if (!operator) return true;
        const regex = /\{\{\$(?:[a-zA-Z_]\w*)\.([a-zA-Z_]\w*)(?:\.id)?\}\}/;
        const fieldName = null == jsonlogic ? void 0 : null === (_jsonlogic_value = jsonlogic.value) || void 0 === _jsonlogic_value ? void 0 : null === (_jsonlogic_value_match1 = _jsonlogic_value.match) || void 0 === _jsonlogic_value_match1 ? void 0 : null === (_jsonlogic_value_match = _jsonlogic_value_match1.call(_jsonlogic_value, regex)) || void 0 === _jsonlogic_value_match ? void 0 : _jsonlogic_value_match[1];
        if (fieldName) results.push(fieldName);
    });
    return results;
};
const InternalAssociationSelect = (0, schema_namespaceObject.observer)((props)=>{
    var _fieldSchema_xcomponentprops, _field_componentProps_service_params, _field_componentProps_service, _field_componentProps;
    const { objectValue = true } = props;
    const { message } = external_antd_namespaceObject.App.useApp();
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const service = external_hooks_js_default()(props);
    const { options: collectionField } = (0, external_hooks_js_namespaceObject.useAssociationFieldContext)();
    const initValue = (0, isVariable_js_namespaceObject.isVariable)(props.value) ? void 0 : props.value;
    const value = Array.isArray(initValue) ? initValue.filter(Boolean) : initValue;
    const [innerValue, setInnerValue] = (0, external_react_namespaceObject.useState)(value);
    const addMode = null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.addMode;
    const isAllowAddNew = fieldSchema['x-add-new'];
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const { multiple } = props;
    const form = (0, schema_namespaceObject.useForm)();
    const api = (0, external_index_js_namespaceObject.useAPIClient)();
    const resource = api.resource(collectionField.target);
    const linkageFields = filterAnalyses(null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : null === (_field_componentProps_service = _field_componentProps.service) || void 0 === _field_componentProps_service ? void 0 : null === (_field_componentProps_service_params = _field_componentProps_service.params) || void 0 === _field_componentProps_service_params ? void 0 : _field_componentProps_service_params.filter);
    const recordData = (0, external_index_js_namespaceObject.useCollectionRecordData)();
    (0, external_react_namespaceObject.useEffect)(()=>{
        const initValue = (0, isVariable_js_namespaceObject.isVariable)(field.value) ? void 0 : field.value;
        const value = Array.isArray(initValue) ? initValue.filter(Boolean) : initValue;
        setInnerValue(value);
    }, [
        field.value
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        const id = (0, schema_namespaceObject.uid)();
        form.addEffects(id, ()=>{
            if ((null == linkageFields ? void 0 : linkageFields.length) > 0) (0, schema_namespaceObject.onFieldInputValueChange)('*', (fieldPath)=>{
                if (linkageFields.includes(fieldPath.props.name) && field.value) {
                    props.onChange(field.initialValue);
                    setInnerValue(field.initialValue);
                }
            });
        });
        return ()=>{
            form.removeEffects(id);
        };
    }, []);
    const handleCreateAction = async (props)=>{
        var _field_componentProps_fieldNames, _field_componentProps;
        const { search: value, callBack } = props;
        const { data: { data } } = await resource.create({
            values: {
                [(null == field ? void 0 : null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : null === (_field_componentProps_fieldNames = _field_componentProps.fieldNames) || void 0 === _field_componentProps_fieldNames ? void 0 : _field_componentProps_fieldNames.label) || 'id']: value
            }
        });
        if (data) {
            if ([
                'm2m',
                'o2m'
            ].includes(null == collectionField ? void 0 : collectionField.interface) && false !== multiple) {
                const values = form.getValuesIn(field.path) || [];
                values.push(data);
                form.setValuesIn(field.path, values);
                field.onInput(values);
            } else {
                form.setValuesIn(field.path, data);
                field.onInput(data);
            }
            (0, external_mathjs_namespaceObject.isFunction)(callBack) && (null == callBack || callBack());
            message.success(t('Saved successfully'));
        }
    };
    const QuickAddContent = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
            onClick: ()=>handleCreateAction(props),
            style: {
                cursor: 'pointer',
                padding: '5px 12px',
                color: '#0d0c0c'
            },
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {}),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                    style: {
                        paddingLeft: 5
                    },
                    children: t('Add') + " “".concat(props.search, "” ")
                })
            ]
        });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Space.Compact, {
            style: {
                display: 'flex',
                lineHeight: '32px'
            },
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.RemoteSelect, {
                    style: {
                        width: '100%'
                    },
                    ...props,
                    size: 'middle',
                    objectValue: objectValue,
                    value: value || innerValue,
                    service: service,
                    onChange: (value)=>{
                        var _props_onChange;
                        const val = (null == value ? void 0 : value.length) !== 0 ? value : null;
                        null === (_props_onChange = props.onChange) || void 0 === _props_onChange || _props_onChange.call(props, val);
                    },
                    CustomDropdownRender: 'quickAdd' === addMode && QuickAddContent
                }),
                ('modalAdd' === addMode || isAllowAddNew) && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.RecordProvider, {
                    isNew: true,
                    record: null,
                    parent: recordData,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.RecursionField, {
                        onlyRenderProperties: true,
                        basePath: field.address,
                        schema: fieldSchema,
                        filterProperties: (s)=>'Action' === s['x-component']
                    })
                })
            ]
        })
    }, fieldSchema.name);
}, {
    displayName: 'AssociationSelect'
});
const AssociationSelect = InternalAssociationSelect;
const AssociationSelectReadPretty = (0, schema_namespaceObject.connect)((props)=>{
    const service = external_hooks_js_default()(props);
    (0, external_index_js_namespaceObject.useApp)();
    if (props.fieldNames) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.RemoteSelect.ReadPretty, {
        ...props,
        service: service
    });
    return null;
}, (0, schema_namespaceObject.mapProps)({
    dataSource: 'options',
    loading: true
}, (props, field)=>({
        ...props,
        fieldNames: props.fieldNames && {
            ...props.fieldNames,
            ...field.componentProps.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
    })));
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
});
