"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__, {
    PresetFields: ()=>PresetFields
});
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 external_index_js_namespaceObject_1 = require("../../../index.js");
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 = (0, schema_namespaceObject.observer)((props)=>{
    const { getInterface } = (0, external_index_js_namespaceObject.useCollectionManager_deprecated)();
    const form = (0, schema_namespaceObject.useForm)();
    const compile = (0, external_index_js_namespaceObject_1.useCompile)();
    const [selectedRowKeys, setSelectedRowKeys] = (0, external_react_namespaceObject.useState)([]);
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const column = [
        {
            title: t('Field'),
            dataIndex: 'field',
            key: 'field'
        },
        {
            title: t('Interface'),
            dataIndex: 'interface',
            key: 'interface',
            render: (value)=>{
                var _getInterface;
                return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tag, {
                    children: compile(null === (_getInterface = getInterface(value)) || void 0 === _getInterface ? 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'
        }
    ];
    (0, external_react_namespaceObject.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
        });
    }, []);
    (0, external_react_namespaceObject.useEffect)(()=>{
        const fields = getDefaultCollectionFields(selectedRowKeys.map((v)=>({
                name: v
            })), form.values);
        form.setValuesIn('fields', fields);
    }, [
        selectedRowKeys
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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 ? void 0 : _fields_map.call(fields, (v)=>v.name));
                form.setValues({
                    ...form.values,
                    fields,
                    ...config
                });
            }
        }
    });
}, {
    displayName: 'PresetFields'
});
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
});
