"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__, {
    PreviewFields: ()=>PreviewFields
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_lodash_namespaceObject = require("lodash");
const external_react_i18next_namespaceObject = require("react-i18next");
const external_index_js_namespaceObject = require("../../../index.js");
const index_js_namespaceObject = require("../../../api-client/index.js");
const interfaces_js_namespaceObject = require("../../Configuration/interfaces.js");
const useCollectionManager_deprecated_js_namespaceObject = require("../../hooks/useCollectionManager_deprecated.js");
const getInterfaceOptions = (data, type)=>{
    const interfaceOptions = [];
    data.forEach((item)=>{
        const options = item.children.filter((h)=>{
            var _h_availableTypes;
            return null == h ? void 0 : null === (_h_availableTypes = h.availableTypes) || void 0 === _h_availableTypes ? void 0 : _h_availableTypes.includes(type);
        });
        interfaceOptions.push({
            label: item.label,
            key: item.key,
            children: options
        });
    });
    return interfaceOptions.filter((v)=>v.children.length > 0);
};
const PreviewCom = (props)=>{
    const { databaseView, viewName, sources, schema } = props;
    const { data: fields } = (0, external_react_namespaceObject.useContext)(external_index_js_namespaceObject.ResourceActionContext);
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const [loading, setLoading] = (0, external_react_namespaceObject.useState)(false);
    const [dataSource, setDataSource] = (0, external_react_namespaceObject.useState)([]);
    const [sourceFields, setSourceFields] = (0, external_react_namespaceObject.useState)([]);
    const [sourceCollections, setSourceCollections] = (0, external_react_namespaceObject.useState)(sources);
    const field = (0, schema_namespaceObject.useField)();
    const form = (0, schema_namespaceObject.useForm)();
    const { getCollection, getInterface, getCollectionFields, getInheritCollections, getParentCollectionFields } = (0, useCollectionManager_deprecated_js_namespaceObject.useCollectionManager_deprecated)();
    const compile = (0, external_index_js_namespaceObject.useCompile)();
    const options = (0, interfaces_js_namespaceObject.useFieldInterfaceOptions)();
    const initOptions = options.filter((v)=>![
            'relation',
            'systemInfo'
        ].includes(v.key));
    (0, external_react_namespaceObject.useEffect)(()=>{
        const data = [];
        sourceCollections.forEach((item)=>{
            var _collection_fields_filter;
            const collection = getCollection(item);
            const inherits = getInheritCollections(item);
            const result = inherits.map((v)=>{
                const fields = getParentCollectionFields(v, item);
                return {
                    type: 'group',
                    key: v,
                    label: t("Parent collection fields") + t("(".concat(getCollection(v).title, ")")),
                    children: fields.filter((v)=>![
                            'hasOne',
                            'hasMany',
                            'belongsToMany'
                        ].includes(null == v ? void 0 : v.type)).map((k)=>{
                        var _k_uiSchema;
                        return {
                            value: k.name,
                            label: t(null === (_k_uiSchema = k.uiSchema) || void 0 === _k_uiSchema ? void 0 : _k_uiSchema.title)
                        };
                    })
                };
            });
            const children = null === (_collection_fields_filter = collection.fields.filter((v)=>![
                    'hasOne',
                    'hasMany',
                    'belongsToMany'
                ].includes(null == v ? void 0 : v.type))) || void 0 === _collection_fields_filter ? void 0 : _collection_fields_filter.map((v)=>{
                var _v_uiSchema;
                return {
                    value: v.name,
                    key: v.name,
                    label: t((null === (_v_uiSchema = v.uiSchema) || void 0 === _v_uiSchema ? void 0 : _v_uiSchema.title) || v.name)
                };
            }).concat(result);
            data.push({
                value: item,
                label: t(collection.title || collection.name),
                children
            });
        });
        setSourceFields(data);
    }, [
        sourceCollections,
        databaseView
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (databaseView) {
            setLoading(true);
            api.resource("dbViews").get({
                filterByTk: viewName,
                schema
            }).then((param)=>{
                let { data } = param;
                if (data) {
                    var _Object_values, _data_data;
                    setLoading(false);
                    setDataSource([]);
                    const fieldsData = null === (_Object_values = Object.values(null == data ? void 0 : null === (_data_data = data.data) || void 0 === _data_data ? void 0 : _data_data.fields)) || void 0 === _Object_values ? void 0 : _Object_values.map((v)=>{
                        if (v.source) {
                            var _option_uiSchema;
                            const option = (null == fields ? void 0 : fields.data.find((h)=>h.name === v.name)) || v;
                            return {
                                ...v,
                                uiSchema: {
                                    ...(0, external_lodash_namespaceObject.omit)(option.uiSchema, 'rawTitle'),
                                    title: (null === (_option_uiSchema = option.uiSchema) || void 0 === _option_uiSchema ? void 0 : _option_uiSchema.title) || option.name
                                }
                            };
                        }
                        {
                            var _option_uiSchema1;
                            const option = (null == fields ? void 0 : fields.data.find((h)=>h.name === v.name)) || v;
                            return {
                                ...option,
                                uiSchema: {
                                    ...(0, external_lodash_namespaceObject.omit)(option.uiSchema, 'rawTitle'),
                                    title: (null === (_option_uiSchema1 = option.uiSchema) || void 0 === _option_uiSchema1 ? void 0 : _option_uiSchema1.title) || option.name
                                }
                            };
                        }
                    });
                    field.value = fieldsData;
                    setTimeout(()=>{
                        var _data_data, _data_data1;
                        setDataSource(fieldsData);
                        form.setValuesIn('sources', null === (_data_data = data.data) || void 0 === _data_data ? void 0 : _data_data.sources);
                        setSourceCollections(null === (_data_data1 = data.data) || void 0 === _data_data1 ? void 0 : _data_data1.sources);
                    });
                }
            }).catch;
        }
    }, [
        databaseView
    ]);
    const handleFieldChange = (record, index)=>{
        dataSource.splice(index, 1, record);
        setDataSource(dataSource);
        field.value = dataSource.map((v)=>{
            var _v_source_filter, _v_source_filter1, _v_source;
            const source = 'string' == typeof v.source ? v.source : null === (_v_source = v.source) || void 0 === _v_source ? void 0 : null === (_v_source_filter1 = _v_source.filter) || void 0 === _v_source_filter1 ? void 0 : null === (_v_source_filter = _v_source_filter1.call(_v_source, Boolean)) || void 0 === _v_source_filter ? void 0 : _v_source_filter.join('.');
            return {
                ...v,
                source
            };
        });
    };
    const columns = [
        {
            title: t('Field name'),
            dataIndex: 'name',
            key: 'name',
            width: 130
        },
        {
            title: t('Field source'),
            dataIndex: 'source',
            key: 'source',
            width: 200,
            render: (text, record, index)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Cascader, {
                    defaultValue: 'string' == typeof text ? null == text ? void 0 : text.split('.') : text,
                    allowClear: true,
                    style: {
                        width: '100%'
                    },
                    options: compile(sourceFields),
                    onChange: (value, selectedOptions)=>{
                        var _getCollectionFields;
                        const sourceField = null === (_getCollectionFields = getCollectionFields(null == value ? void 0 : value[0])) || void 0 === _getCollectionFields ? void 0 : _getCollectionFields.find((v)=>v.name === (0, external_lodash_namespaceObject.last)(value));
                        handleFieldChange({
                            ...record,
                            source: value,
                            uiSchema: null == sourceField ? void 0 : sourceField.uiSchema
                        }, index);
                    },
                    placeholder: t('Select field source')
                })
        },
        {
            title: t('Field type'),
            dataIndex: 'type',
            width: 140,
            key: 'type',
            render: (text, _, index)=>{
                const item = dataSource[index];
                return (null == item ? void 0 : item.source) || !(null == item ? void 0 : item.possibleTypes) ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                    children: text
                }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
                    defaultValue: text,
                    popupMatchSelectWidth: false,
                    style: {
                        width: '100%'
                    },
                    options: (null == item ? void 0 : item.possibleTypes.map((v)=>({
                            label: v,
                            value: v
                        }))) || [],
                    onChange: (value)=>handleFieldChange({
                            ...item,
                            type: value
                        }, index)
                });
            }
        },
        {
            title: t('Field interface'),
            dataIndex: 'interface',
            key: 'interface',
            width: 150,
            render: (text, _, index)=>{
                const item = dataSource[index];
                const data = getInterfaceOptions(initOptions, item.type);
                return item.source ? text : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
                    defaultValue: text,
                    style: {
                        width: '100%'
                    },
                    popupMatchSelectWidth: false,
                    onChange: (value)=>{
                        var _interfaceConfig_default;
                        const interfaceConfig = getInterface(value);
                        handleFieldChange({
                            ...item,
                            interface: value,
                            uiSchema: null == interfaceConfig ? void 0 : null === (_interfaceConfig_default = interfaceConfig.default) || void 0 === _interfaceConfig_default ? void 0 : _interfaceConfig_default.uiSchema
                        }, index);
                    },
                    children: data.map((group)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select.OptGroup, {
                            label: compile(group.label),
                            children: group.children.map((item)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select.Option, {
                                    value: item.name,
                                    children: compile(item.label)
                                }, item.value))
                        }, group.key))
                });
            }
        },
        {
            title: t('Field display name'),
            dataIndex: 'title',
            key: 'title',
            width: 180,
            render: (text, record, index)=>{
                var _item_uiSchema;
                const item = dataSource[index];
                return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Input, {
                    defaultValue: (null == item ? void 0 : null === (_item_uiSchema = item.uiSchema) || void 0 === _item_uiSchema ? void 0 : _item_uiSchema.title) || text,
                    onChange: (e)=>handleFieldChange({
                            ...item,
                            uiSchema: {
                                ...(0, external_lodash_namespaceObject.omit)(null == item ? void 0 : item.uiSchema, 'rawTitle'),
                                title: e.target.value
                            }
                        }, index)
                });
            }
        }
    ];
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Spin, {
        spinning: loading,
        children: dataSource.length > 0 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                    className: "ant-formily-item-label",
                    style: {
                        display: 'flex',
                        padding: '0 0 8px'
                    },
                    children: [
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                            className: "ant-formily-item-label-content",
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("label", {
                                    children: t('Fields')
                                })
                            })
                        }),
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                            className: "ant-formily-item-colon",
                            children: ":"
                        })
                    ]
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Table, {
                    bordered: true,
                    size: 'middle',
                    columns: columns,
                    dataSource: dataSource,
                    scroll: {
                        y: 300
                    },
                    pagination: false,
                    rowClassName: "editable-row"
                }, viewName)
            ]
        })
    });
};
function areEqual(prevProps, nextProps) {
    return nextProps.name === prevProps.name && nextProps.sources === prevProps.sources;
}
const PreviewFields = /*#__PURE__*/ external_react_default().memo(PreviewCom, areEqual);
PreviewFields.displayName = 'PreviewFields';
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
});
