import ModelAPI from "../../Utils/ModelAPI/ModelAPI";
import { FormType } from "../Forms/ModelForm";
import ModelFormModal from "../ModelFormModal/ModelFormModal";
import BaseModelTable, { ModalType } from "./BaseModelTable";
import BaseModel from "../../../Models/DatabaseModels/DatabaseBaseModel/DatabaseBaseModel";
import ObjectID from "../../../Types/ObjectID";
import React from "react";
const ModelTable = (props) => {
    const modelAPI = props.modelAPI || ModelAPI;
    const model = new props.modelType();
    return (React.createElement(BaseModelTable, Object.assign({}, props, { callbacks: {
            getJSONFromModel: (item) => {
                return BaseModel.toJSONObject(item, props.modelType);
            },
            updateById: async (args) => {
                const { id, data } = args;
                await modelAPI.updateById({
                    modelType: props.modelType,
                    id: new ObjectID(id),
                    data: data,
                });
            },
            toJSONArray: (items) => {
                return BaseModel.toJSONObjectArray(items, props.modelType);
            },
            getList: async (data) => {
                return await modelAPI.getList({
                    modelType: data.modelType,
                    query: data.query,
                    limit: data.limit,
                    groupBy: data.groupBy,
                    skip: data.skip,
                    sort: data.sort,
                    select: data.select,
                    requestOptions: data.requestOptions,
                });
            },
            addSlugToSelect: (select) => {
                const slugifyColumn = model.getSlugifyColumn();
                if (slugifyColumn) {
                    select[slugifyColumn] = true;
                }
                return select;
            },
            showCreateEditModal: (data) => {
                var _a;
                const { modalType, modelIdToEdit, onBeforeCreate, onSuccess, onClose, } = data;
                return (React.createElement(ModelFormModal, { modelAPI: props.modelAPI, title: modalType === ModalType.Create
                        ? `${props.createVerb || "Create"} New ${props.singularName || model.singularName}`
                        : `Edit ${props.singularName || model.singularName}`, formRef: props.createEditFromRef, modalWidth: props.createEditModalWidth, name: modalType === ModalType.Create
                        ? `${props.name} > ${props.createVerb || "Create"} New ${props.singularName || model.singularName}`
                        : `${props.name} > Edit ${props.singularName || model.singularName}`, initialValues: modalType === ModalType.Create
                        ? props.createInitialValues
                        : undefined, onClose: onClose, submitButtonText: modalType === ModalType.Create
                        ? `${props.createVerb || "Create"} ${props.singularName || model.singularName}`
                        : `Save Changes`, onSuccess: onSuccess, onBeforeCreate: onBeforeCreate, modelType: props.modelType, formProps: {
                        summary: props.formSummary,
                        name: `create-${props.modelType.name}-from`,
                        modelType: props.modelType,
                        id: `create-${props.modelType.name}-from`,
                        fields: ((_a = props.formFields) === null || _a === void 0 ? void 0 : _a.filter((field) => {
                            // If the field has doNotShowWhenEditing set to true, then don't show it when editing
                            if (modelIdToEdit) {
                                return !field.doNotShowWhenEditing;
                            }
                            // If the field has doNotShowWhenCreating set to true, then don't show it when creating
                            return !field.doNotShowWhenCreating;
                        })) || [],
                        steps: props.formSteps || [],
                        formType: modalType === ModalType.Create
                            ? FormType.Create
                            : FormType.Update,
                    }, modelIdToEdit: modelIdToEdit }));
            },
            getModelFromJSON: (item) => {
                return BaseModel.fromJSON(item, props.modelType);
            },
            deleteItem: async (item) => {
                await modelAPI.deleteItem({
                    modelType: props.modelType,
                    id: item.id,
                    requestOptions: props.deleteRequestOptions,
                });
            },
        } })));
};
export default ModelTable;
//# sourceMappingURL=ModelTable.js.map