"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__, {
    PreviewTable: ()=>PreviewTable
});
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 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 useCollectionManager_deprecated_js_namespaceObject = require("../../hooks/useCollectionManager_deprecated.js");
const mapFields = [
    'lineString',
    'point',
    'circle',
    'polygon'
];
const PreviewTable = (props)=>{
    const { databaseView, schema, viewName, fields } = props;
    const [previewColumns, setPreviewColumns] = (0, external_react_namespaceObject.useState)([]);
    const [previewData, setPreviewData] = (0, external_react_namespaceObject.useState)([]);
    const compile = (0, external_index_js_namespaceObject.useCompile)();
    const [loading, setLoading] = (0, external_react_namespaceObject.useState)(false);
    const { getInterface, getCollectionFields } = (0, useCollectionManager_deprecated_js_namespaceObject.useCollectionManager_deprecated)();
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const form = (0, schema_namespaceObject.useForm)();
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (databaseView) getPreviewData();
    }, [
        viewName,
        schema
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        const pColumns = formatPreviewColumns(fields);
        setPreviewColumns(pColumns);
    }, [
        form.values.fields
    ]);
    const getPreviewData = ()=>{
        const fieldTypes = {};
        form.values.fields.map((v)=>{
            if (mapFields.includes(v.type)) fieldTypes[v.name] = v.type;
        });
        setLoading(true);
        api.resource("dbViews").query({
            filterByTk: viewName,
            schema,
            fieldTypes
        }).then((param)=>{
            let { data } = param;
            if (data) {
                setLoading(false);
                setPreviewData((null == data ? void 0 : data.data) || []);
            }
        }).catch;
    };
    const formatPreviewColumns = (data)=>{
        var _data_filter;
        return null === (_data_filter = data.filter((k)=>k.source || k.interface)) || void 0 === _data_filter ? void 0 : _data_filter.map((item)=>{
            var _item_source, _getCollectionFields_find, _getCollectionFields, _item_uiSchema, _getInterface_default, _getInterface;
            const fieldSource = 'string' == typeof (null == item ? void 0 : item.source) ? null == item ? void 0 : null === (_item_source = item.source) || void 0 === _item_source ? void 0 : _item_source.split('.') : null == item ? void 0 : item.source;
            const sourceField = null === (_getCollectionFields = getCollectionFields(null == fieldSource ? void 0 : fieldSource[0])) || void 0 === _getCollectionFields ? void 0 : null === (_getCollectionFields_find = _getCollectionFields.find((v)=>v.name === (null == fieldSource ? void 0 : fieldSource[1]))) || void 0 === _getCollectionFields_find ? void 0 : _getCollectionFields_find.uiSchema;
            const target = (null == item ? void 0 : null === (_item_uiSchema = item.uiSchema) || void 0 === _item_uiSchema ? void 0 : _item_uiSchema.title) || (null == sourceField ? void 0 : sourceField.title) || item.name;
            const schema = item.source ? sourceField : null === (_getInterface = getInterface(item.interface)) || void 0 === _getInterface ? void 0 : null === (_getInterface_default = _getInterface.default) || void 0 === _getInterface_default ? void 0 : _getInterface_default.uiSchema;
            return {
                title: compile(target),
                dataIndex: item.name,
                key: item.name,
                width: 200,
                render: (v, record, index)=>{
                    const content = record[item.name];
                    const objSchema = {
                        type: 'object',
                        properties: {
                            [item.name]: {
                                name: "".concat(item.name),
                                'x-component': schema && fieldSource ? 'CollectionField' : 'Input',
                                'x-read-pretty': true,
                                'x-collection-field': null == fieldSource ? void 0 : fieldSource.join('.'),
                                default: 'json' === item.interface ? JSON.stringify(content) : content
                            }
                        }
                    };
                    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.EllipsisWithTooltip, {
                        ellipsis: true,
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.RecursionField, {
                            schema: objSchema,
                            name: index,
                            onlyRenderProperties: true
                        })
                    });
                }
            };
        });
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Spin, {
        spinning: loading,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
            style: {
                marginBottom: 22
            },
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                    className: "ant-formily-item-label",
                    style: {
                        marginTop: 24,
                        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('Preview')
                                })
                            })
                        }),
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                            className: "ant-formily-item-colon",
                            children: ":"
                        })
                    ]
                }, viewName),
                (null == previewColumns ? void 0 : previewColumns.length) > 0 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(jsx_runtime_namespaceObject.Fragment, {
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Table, {
                        size: 'middle',
                        pagination: false,
                        bordered: true,
                        columns: previewColumns,
                        dataSource: previewData,
                        scroll: {
                            x: 1000,
                            y: 300
                        }
                    }, "".concat(viewName, "-preview"))
                })
            ]
        })
    }, "preview");
};
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
});
