import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
const supportTypes = [
    'string',
    'bigInt',
    'integer',
    'uuid',
    'uid'
];
const SourceForeignKey = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)(()=>{
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const { getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const collection = (null == record ? void 0 : record.collectionName) ? getCollection(record.collectionName) : record;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { getCollectionFields } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)(()=>{
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { getCollectionFields } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)(()=>{
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { getCollectionFields } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)(()=>{
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const { getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const collection = (null == record ? void 0 : record.collectionName) ? getCollection(record.collectionName) : record;
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Select, {
            disabled: true,
            popupMatchSelectWidth: false,
            value: collection.name,
            options: [
                {
                    value: collection.name,
                    label: compile(collection.title)
                }
            ]
        })
    });
}, {
    displayName: 'SourceCollection'
});
const SourceKey = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    var _getCollection_fields, _getCollection, _options_;
    const { sourceKey, collectionName, name } = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const { getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        var _options_;
        field.initialValue = (null == options ? void 0 : null === (_options_ = options[0]) || void 0 === _options_ ? void 0 : _options_.value) || sourceKey;
    }, []);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { value, disabled } = props;
    const { targetKey, target, type } = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const { getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const [options, setOptions] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const [initialValue, setInitialValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(value || targetKey);
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    field.required = true;
    (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { disabled } = props;
    const [options, setOptions] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const { getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { collectionName, target, type, through, name, template } = record;
    const value = record[field.props.name];
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const [initialValue, setInitialValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(value || ('view' === template ? null : field.initialValue));
    (0, __WEBPACK_EXTERNAL_MODULE_react__.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 ? __WEBPACK_EXTERNAL_MODULE_antd__.Select : __WEBPACK_EXTERNAL_MODULE_antd__.AutoComplete;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { disabled } = props;
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const { getCollections } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const [options, setOptions] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const value = record[field.props.name];
    const [initialValue, setInitialValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(value || field.initialValue);
    const [defOptions, setDefOptions] = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.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'
});
export { ForeignKey, SourceCollection, SourceForeignKey, SourceKey, TargetForeignKey, TargetKey, ThroughCollection, ThroughForeignKey };
