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