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_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useCollectionManager_deprecated_mjs__ from "../../hooks/useCollectionManager_deprecated.mjs";
const mapFields = [
    'lineString',
    'point',
    'circle',
    'polygon'
];
const PreviewTable = (props)=>{
    const { databaseView, schema, viewName, fields } = props;
    const [previewColumns, setPreviewColumns] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const [previewData, setPreviewData] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCompile)();
    const [loading, setLoading] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const { getInterface, getCollectionFields } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useCollectionManager_deprecated_mjs__.useCollectionManager_deprecated)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (databaseView) getPreviewData();
    }, [
        viewName,
        schema
    ]);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.EllipsisWithTooltip, {
                        ellipsis: true,
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                            schema: objSchema,
                            name: index,
                            onlyRenderProperties: true
                        })
                    });
                }
            };
        });
    };
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Spin, {
        spinning: loading,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
            style: {
                marginBottom: 22
            },
            children: [
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                    className: "ant-formily-item-label",
                    style: {
                        marginTop: 24,
                        display: 'flex',
                        padding: '0 0 8px'
                    },
                    children: [
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                            className: "ant-formily-item-label-content",
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("label", {
                                    children: t('Preview')
                                })
                            })
                        }),
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                            className: "ant-formily-item-colon",
                            children: ":"
                        })
                    ]
                }, viewName),
                (null == previewColumns ? void 0 : previewColumns.length) > 0 && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Table, {
                        size: 'middle',
                        pagination: false,
                        bordered: true,
                        columns: previewColumns,
                        dataSource: previewData,
                        scroll: {
                            x: 1000,
                            y: 300
                        }
                    }, "".concat(viewName, "-preview"))
                })
            ]
        })
    }, "preview");
};
export { PreviewTable };
