import { jsx } from "react/jsx-runtime";
import { useEffect, useState } from "react";
import { observer, useForm } from "@tachybase/schema";
import { Table, Tag } from "antd";
import { useTranslation } from "react-i18next";
import { useCollectionManager_deprecated } from "../../index.mjs";
import { useCompile } from "../../../index.mjs";
const getDefaultCollectionFields = (presetFields, values)=>{
    if ((null == values ? void 0 : values.template) === 'view' || (null == values ? void 0 : values.template) === 'sql') return values.fields;
    const defaults = values.fields ? [
        ...values.fields
    ].filter((v)=>![
            'id',
            'createdBy',
            'updatedAt',
            'createdAt',
            'updatedBy'
        ].includes(v.name)) : [];
    if (presetFields.find((v)=>'id' === v.name)) defaults.push({
        name: 'id',
        type: 'bigInt',
        autoIncrement: true,
        primaryKey: true,
        allowNull: false,
        uiSchema: {
            type: 'number',
            title: '{{t("ID")}}',
            'x-component': 'InputNumber',
            'x-read-pretty': true
        },
        interface: 'integer'
    });
    if (presetFields.find((v)=>'createdAt' === v.name)) defaults.push({
        name: 'createdAt',
        interface: 'createdAt',
        type: 'date',
        field: 'createdAt',
        uiSchema: {
            type: 'datetime',
            title: '{{t("Created at")}}',
            'x-component': 'DatePicker',
            'x-component-props': {},
            'x-read-pretty': true
        }
    });
    if (presetFields.find((v)=>'createdBy' === v.name)) defaults.push({
        name: 'createdBy',
        interface: 'createdBy',
        type: 'belongsTo',
        target: 'users',
        foreignKey: 'createdById',
        uiSchema: {
            type: 'object',
            title: '{{t("Created by")}}',
            'x-component': 'AssociationField',
            'x-component-props': {
                fieldNames: {
                    value: 'id',
                    label: 'nickname'
                }
            },
            'x-read-pretty': true
        }
    });
    if (presetFields.find((v)=>'updatedAt' === v.name)) defaults.push({
        type: 'date',
        field: 'updatedAt',
        name: 'updatedAt',
        interface: 'updatedAt',
        uiSchema: {
            type: 'string',
            title: '{{t("Last updated at")}}',
            'x-component': 'DatePicker',
            'x-component-props': {},
            'x-read-pretty': true
        }
    });
    if (presetFields.find((v)=>'updatedBy' === v.name)) defaults.push({
        type: 'belongsTo',
        target: 'users',
        foreignKey: 'updatedById',
        name: 'updatedBy',
        interface: 'updatedBy',
        uiSchema: {
            type: 'object',
            title: '{{t("Last updated by")}}',
            'x-component': 'AssociationField',
            'x-component-props': {
                fieldNames: {
                    value: 'id',
                    label: 'nickname'
                }
            },
            'x-read-pretty': true
        }
    });
    return defaults;
};
const PresetFields = observer((props)=>{
    const { getInterface } = useCollectionManager_deprecated();
    const form = useForm();
    const compile = useCompile();
    const [selectedRowKeys, setSelectedRowKeys] = useState([]);
    const { t } = useTranslation();
    const column = [
        {
            title: t('Field'),
            dataIndex: 'field',
            key: 'field'
        },
        {
            title: t('Interface'),
            dataIndex: 'interface',
            key: 'interface',
            render: (value)=>{
                var _getInterface;
                return /*#__PURE__*/ jsx(Tag, {
                    children: compile(null == (_getInterface = getInterface(value)) ? void 0 : _getInterface.title)
                });
            }
        },
        {
            title: t("Description"),
            dataIndex: "description",
            key: "description"
        }
    ];
    const dataSource = [
        {
            field: t('ID'),
            interface: 'integer',
            description: t('Primary key, unique identifier, self growth'),
            name: 'id'
        },
        {
            field: t('Created at'),
            interface: 'createdAt',
            description: t('Store the creation time of each record'),
            name: 'createdAt'
        },
        {
            field: t('Last updated at'),
            interface: 'updatedAt',
            description: t('Store the last update time of each record'),
            name: 'updatedAt'
        },
        {
            field: t('Created by'),
            interface: 'createdBy',
            description: t('Store the creation user of each record'),
            name: 'createdBy'
        },
        {
            field: t('Last updated by'),
            interface: 'updatedBy',
            description: t('Store the last update user of each record'),
            name: 'updatedBy'
        }
    ];
    useEffect(()=>{
        const config = {
            autoGenId: true,
            createdAt: true,
            createdBy: true,
            updatedAt: true,
            updatedBy: true
        };
        const initialValue = [
            'id',
            'createdAt',
            'createdBy',
            'updatedAt',
            'updatedBy'
        ];
        setSelectedRowKeys(initialValue);
        form.setValues({
            ...form.values,
            ...config
        });
    }, []);
    useEffect(()=>{
        const fields = getDefaultCollectionFields(selectedRowKeys.map((v)=>({
                name: v
            })), form.values);
        form.setValuesIn('fields', fields);
    }, [
        selectedRowKeys
    ]);
    return /*#__PURE__*/ jsx(Table, {
        size: "small",
        pagination: false,
        rowKey: "name",
        bordered: true,
        scroll: {
            x: 600
        },
        dataSource: dataSource,
        columns: column,
        rowSelection: {
            type: 'checkbox',
            selectedRowKeys,
            getCheckboxProps: (record)=>({
                    disabled: 'file' === form.values.template,
                    name: record.name
                }),
            onChange: (_, selectedRows)=>{
                var _fields_map;
                const fields = getDefaultCollectionFields(selectedRows, form.values);
                const config = {
                    autoGenId: !!fields.find((v)=>'id' === v.name),
                    createdAt: !!fields.find((v)=>'createdAt' === v.name),
                    createdBy: !!fields.find((v)=>'createdBy' === v.name),
                    updatedAt: !!fields.find((v)=>'updatedAt' === v.name),
                    updatedBy: !!fields.find((v)=>'updatedBy' === v.name)
                };
                setSelectedRowKeys(null == fields ? void 0 : null == (_fields_map = fields.map) ? void 0 : _fields_map.call(fields, (v)=>v.name));
                form.setValues({
                    ...form.values,
                    fields,
                    ...config
                });
            }
        }
    });
}, {
    displayName: 'PresetFields'
});
export { PresetFields };
