"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__, {
    SourceKey: ()=>SourceKey,
    TargetKey: ()=>TargetKey,
    ThroughForeignKey: ()=>ThroughForeignKey,
    ForeignKey: ()=>ForeignKey,
    SourceCollection: ()=>SourceCollection,
    TargetForeignKey: ()=>TargetForeignKey,
    ThroughCollection: ()=>ThroughCollection,
    SourceForeignKey: ()=>SourceForeignKey
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const index_js_namespaceObject = require("../../../record-provider/index.js");
const external_schema_component_index_js_namespaceObject = require("../../../schema-component/index.js");
const external_hooks_index_js_namespaceObject = require("../../hooks/index.js");
const supportTypes = [
    'string',
    'bigInt',
    'integer',
    'uuid',
    'uid'
];
const SourceForeignKey = (0, schema_namespaceObject.observer)(()=>{
    const record = (0, index_js_namespaceObject.useRecord)();
    const { getCollection } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    const collection = (null == record ? void 0 : record.collectionName) ? getCollection(record.collectionName) : record;
    const field = (0, schema_namespaceObject.useField)();
    (0, schema_namespaceObject.useForm)();
    const { getCollectionFields } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
            allowClear: true,
            placeholder: '留空时，自动生成 FK 字段',
            disabled: field.disabled,
            value: field.value,
            options: getCollectionFields(collection.name).filter((field)=>field.type).map((field)=>{
                var _field_uiSchema;
                return {
                    label: (null == field ? void 0 : null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema.title) || field.name,
                    value: field.name
                };
            })
        })
    });
}, {
    displayName: 'SourceForeignKey'
});
const ThroughForeignKey = (0, schema_namespaceObject.observer)(()=>{
    const field = (0, schema_namespaceObject.useField)();
    const form = (0, schema_namespaceObject.useForm)();
    const { getCollectionFields } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
            allowClear: true,
            popupMatchSelectWidth: false,
            placeholder: '留空时，自动生成 FK 字段',
            disabled: field.disabled,
            value: field.value,
            options: getCollectionFields(form.values.through).filter((field)=>field.type).map((field)=>{
                var _field_uiSchema;
                return {
                    label: (null == field ? void 0 : null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema.title) || field.name,
                    value: field.name
                };
            })
        })
    });
}, {
    displayName: 'ThroughForeignKey'
});
const TargetForeignKey = (0, schema_namespaceObject.observer)(()=>{
    const field = (0, schema_namespaceObject.useField)();
    const form = (0, schema_namespaceObject.useForm)();
    const { getCollectionFields } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
            allowClear: true,
            popupMatchSelectWidth: false,
            placeholder: '留空时，自动生成 FK 字段',
            disabled: field.disabled,
            value: field.value,
            options: getCollectionFields(form.values.target).filter((field)=>field.type).map((field)=>{
                var _field_uiSchema;
                return {
                    label: (null == field ? void 0 : null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema.title) || field.name,
                    value: field.name
                };
            })
        })
    });
}, {
    displayName: 'TargetForeignKey'
});
const SourceCollection = (0, schema_namespaceObject.observer)(()=>{
    const record = (0, index_js_namespaceObject.useRecord)();
    const { getCollection } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    const collection = (null == record ? void 0 : record.collectionName) ? getCollection(record.collectionName) : record;
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
            disabled: true,
            popupMatchSelectWidth: false,
            value: collection.name,
            options: [
                {
                    value: collection.name,
                    label: compile(collection.title)
                }
            ]
        })
    });
}, {
    displayName: 'SourceCollection'
});
const SourceKey = (0, schema_namespaceObject.observer)((props)=>{
    var _getCollection_fields, _getCollection, _options_;
    const { sourceKey, collectionName, name } = (0, index_js_namespaceObject.useRecord)();
    const { getCollection } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    const field = (0, schema_namespaceObject.useField)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const options = null === (_getCollection = getCollection(collectionName || name)) || void 0 === _getCollection ? void 0 : null === (_getCollection_fields = _getCollection.fields) || void 0 === _getCollection_fields ? void 0 : _getCollection_fields.filter((v)=>v.primaryKey || v.unique).map((k)=>{
        var _k_uiSchema;
        return {
            value: k.name,
            label: compile((null === (_k_uiSchema = k.uiSchema) || void 0 === _k_uiSchema ? void 0 : _k_uiSchema.title) || k.name)
        };
    });
    (0, external_react_namespaceObject.useEffect)(()=>{
        var _options_;
        field.initialValue = (null == options ? void 0 : null === (_options_ = options[0]) || void 0 === _options_ ? void 0 : _options_.value) || sourceKey;
    }, []);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
            disabled: sourceKey,
            options: options,
            defaultValue: sourceKey || (null == options ? void 0 : null === (_options_ = options[0]) || void 0 === _options_ ? void 0 : _options_.value),
            onChange: null == props ? void 0 : props.onChange,
            showSearch: true
        })
    });
}, {
    displayName: 'SourceKey'
});
const TargetKey = (0, schema_namespaceObject.observer)((props)=>{
    const { value, disabled } = props;
    const { targetKey, target, type } = (0, index_js_namespaceObject.useRecord)();
    const { getCollection } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    const [options, setOptions] = (0, external_react_namespaceObject.useState)([]);
    const [initialValue, setInitialValue] = (0, external_react_namespaceObject.useState)(value || targetKey);
    const form = (0, schema_namespaceObject.useForm)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const field = (0, schema_namespaceObject.useField)();
    field.required = true;
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (target) {
            var _getCollection_fields;
            setOptions(null === (_getCollection_fields = getCollection(target).fields) || void 0 === _getCollection_fields ? void 0 : _getCollection_fields.filter((v)=>{
                if ('hasMany' !== type) return v.primaryKey || v.unique;
                return supportTypes.includes(v.type);
            }).map((k)=>{
                var _k_uiSchema;
                return {
                    value: k.name,
                    label: compile((null == k ? void 0 : null === (_k_uiSchema = k.uiSchema) || void 0 === _k_uiSchema ? void 0 : _k_uiSchema.title) || k.title || k.name)
                };
            }));
        }
    }, []);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
            showSearch: true,
            options: options,
            onDropdownVisibleChange: async (open)=>{
                const { target, type } = form.values;
                if (target && open) {
                    var _getCollection_fields;
                    setOptions(null === (_getCollection_fields = getCollection(target).fields) || void 0 === _getCollection_fields ? void 0 : _getCollection_fields.filter((v)=>{
                        if ('hasMany' !== type) return v.primaryKey || v.unique;
                        return supportTypes.includes(v.type);
                    }).map((k)=>{
                        var _k_uiSchema;
                        return {
                            value: k.name,
                            label: compile((null == k ? void 0 : null === (_k_uiSchema = k.uiSchema) || void 0 === _k_uiSchema ? void 0 : _k_uiSchema.title) || k.title || k.name)
                        };
                    }));
                }
            },
            onChange: (value)=>{
                var _props_onChange;
                null == props || null === (_props_onChange = props.onChange) || void 0 === _props_onChange || _props_onChange.call(props, value);
                setInitialValue(value);
            },
            value: initialValue,
            disabled: disabled
        })
    });
}, {
    displayName: 'TargetKey'
});
const ForeignKey = (0, schema_namespaceObject.observer)((props)=>{
    const { disabled } = props;
    const [options, setOptions] = (0, external_react_namespaceObject.useState)([]);
    const { getCollection } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    const record = (0, index_js_namespaceObject.useRecord)();
    const field = (0, schema_namespaceObject.useField)();
    const { collectionName, target, type, through, name, template } = record;
    const value = record[field.props.name];
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const form = (0, schema_namespaceObject.useForm)();
    const [initialValue, setInitialValue] = (0, external_react_namespaceObject.useState)(value || ('view' === template ? null : field.initialValue));
    (0, external_react_namespaceObject.useEffect)(()=>{
        var _getCollection;
        const effectField = [
            'belongsTo'
        ].includes(type) ? collectionName : [
            'belongsToMany'
        ].includes(type) ? through : target;
        const fields = null === (_getCollection = getCollection(effectField)) || void 0 === _getCollection ? void 0 : _getCollection.fields;
        if (fields) {
            const sourceOptions = null == fields ? void 0 : fields.filter((v)=>supportTypes.includes(v.type)).map((k)=>{
                var _k_uiSchema;
                return {
                    value: k.name,
                    label: compile((null === (_k_uiSchema = k.uiSchema) || void 0 === _k_uiSchema ? void 0 : _k_uiSchema.title) || k.name)
                };
            });
            setOptions(sourceOptions);
            if (value) {
                const option = sourceOptions.find((v)=>v.value === value);
                setInitialValue((null == option ? void 0 : option.label) || value);
            }
        }
    }, [
        type
    ]);
    const Compoent = 'view' === template ? external_antd_namespaceObject.Select : external_antd_namespaceObject.AutoComplete;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Compoent, {
            disabled: disabled,
            value: initialValue,
            options: options,
            showSearch: true,
            onDropdownVisibleChange: async (open)=>{
                const { target, type, through } = form.values;
                const effectField = [
                    'belongsTo'
                ].includes(type) ? collectionName || name : [
                    'belongsToMany'
                ].includes(type) ? through : target;
                if (effectField && open) {
                    var _getCollection;
                    const fields = (null === (_getCollection = getCollection(effectField)) || void 0 === _getCollection ? void 0 : _getCollection.fields) || [];
                    setOptions(null == fields ? void 0 : fields.filter((v)=>supportTypes.includes(v.type)).map((k)=>{
                        var _k_uiSchema;
                        return {
                            value: k.name,
                            label: compile((null === (_k_uiSchema = k.uiSchema) || void 0 === _k_uiSchema ? void 0 : _k_uiSchema.title) || k.name)
                        };
                    }));
                }
            },
            onChange: (value, option)=>{
                var _props_onChange;
                null == props || null === (_props_onChange = props.onChange) || void 0 === _props_onChange || _props_onChange.call(props, value);
                setInitialValue(option.label || value);
            }
        })
    });
}, {
    displayName: 'ForeignKey'
});
const ThroughCollection = (0, schema_namespaceObject.observer)((props)=>{
    const { disabled } = props;
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const { getCollections } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    const [options, setOptions] = (0, external_react_namespaceObject.useState)([]);
    const field = (0, schema_namespaceObject.useField)();
    const record = (0, index_js_namespaceObject.useRecord)();
    const value = record[field.props.name];
    const [initialValue, setInitialValue] = (0, external_react_namespaceObject.useState)(value || field.initialValue);
    const [defOptions, setDefOptions] = (0, external_react_namespaceObject.useState)([]);
    const loadCollections = ()=>{
        const filteredItems = getCollections().filter((item)=>{
            const isAutoCreateAndThrough = item.autoCreate && item.isThrough;
            if (isAutoCreateAndThrough) return false;
            return true;
        });
        return filteredItems.map((item)=>({
                label: compile(item.title),
                value: item.name
            }));
    };
    (0, external_react_namespaceObject.useEffect)(()=>{
        const data = loadCollections();
        setOptions(data);
        setDefOptions(data);
        if (value) {
            const option = data.find((v)=>v.value === value);
            setInitialValue((null == option ? void 0 : option.label) || value);
        }
    }, []);
    const handleSearch = (value)=>{
        if (value) {
            const filteredOptions = defOptions.filter((option)=>option.label.toLowerCase().includes(value.toLowerCase()));
            setOptions(filteredOptions);
        } else setOptions(defOptions);
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.AutoComplete, {
            disabled: disabled,
            showSearch: true,
            popupMatchSelectWidth: false,
            value: initialValue,
            options: options,
            onSearch: handleSearch,
            onChange: (value, option)=>{
                var _props_onChange;
                null == props || null === (_props_onChange = props.onChange) || void 0 === _props_onChange || _props_onChange.call(props, value);
                setInitialValue(option.label || value);
            }
        })
    });
}, {
    displayName: 'ThroughCollection'
});
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
});
