import { jsx } from "react/jsx-runtime";
import "react";
import { Schema, observer, useForm } from "@tachybase/schema";
import { Table } from "antd";
import { useTranslation } from "react-i18next";
import { useAsyncData } from "../../../../async-data-provider/index.mjs";
const PreviewTable = observer(()=>{
    const { data: res, loading, error } = useAsyncData();
    const { data } = res || {};
    const { t } = useTranslation();
    const form = useForm();
    const fields = form.values.fields || [];
    const titleMp = fields.reduce((mp, field)=>{
        var _field_uiSchema;
        mp[field.name] = null == field ? void 0 : null == (_field_uiSchema = field.uiSchema) ? void 0 : _field_uiSchema.title;
        return mp;
    }, {});
    const columns = error ? [] : Object.keys((null == data ? void 0 : data[0]) || {}).map((col)=>{
        const title = titleMp[col];
        return {
            title: Schema.compile(title || col, {
                t
            }),
            dataIndex: col,
            key: col
        };
    });
    const dataSource = error ? [] : null == data ? void 0 : data.map((record, index)=>{
        const compiledRecord = Object.entries(record).reduce((mp, param)=>{
            let [key, val] = param;
            if ('string' != typeof val) {
                mp[key] = val;
                return mp;
            }
            const compiled = Schema.compile(val, {
                t
            });
            mp[key] = t(compiled);
            return mp;
        }, {});
        return {
            ...compiledRecord,
            key: index
        };
    });
    return /*#__PURE__*/ jsx(Table, {
        bordered: true,
        dataSource: dataSource,
        columns: columns,
        scroll: {
            x: 150 * columns.length,
            y: 300
        },
        loading: loading,
        rowKey: "key"
    });
}, {
    displayName: 'PreviewTable'
});
export { PreviewTable };
