import API from "../../Utils/API/API";
import ModelAPI from "../../Utils/ModelAPI/ModelAPI";
import { ButtonStyleType } from "../Button/Button";
import Card from "../Card/Card";
import ConfirmModal from "../Modal/ConfirmModal";
import IconProp from "../../../Types/Icon/IconProp";
import React, { useState } from "react";
const ModelDelete = (props) => {
    var _a, _b;
    const model = new props.modelType();
    const [showModal, setShowModal] = useState(false);
    const [isLoading, setIsLoading] = useState(false);
    const [error, setError] = useState("");
    const [showErrorModal, setShowErrorModal] = useState(false);
    const deleteItem = async () => {
        var _a;
        setIsLoading(true);
        try {
            const modelAPI = props.modelAPI || ModelAPI;
            await modelAPI.deleteItem({
                modelType: props.modelType,
                id: props.modelId,
            });
            (_a = props.onDeleteSuccess) === null || _a === void 0 ? void 0 : _a.call(props);
        }
        catch (err) {
            setError(API.getFriendlyMessage(err));
            setShowErrorModal(true);
        }
        setIsLoading(false);
    };
    return (React.createElement(React.Fragment, null,
        React.createElement(Card, { title: `Delete ${model.singularName}`, description: `Are you sure you want to delete this ${(_a = model.singularName) === null || _a === void 0 ? void 0 : _a.toLowerCase()}?`, buttons: [
                {
                    title: `Delete ${model.singularName}`,
                    buttonStyle: ButtonStyleType.DANGER,
                    onClick: () => {
                        setShowModal(true);
                    },
                    isLoading: isLoading,
                    icon: IconProp.Trash,
                },
            ] }),
        showModal ? (React.createElement(ConfirmModal, { description: `Are you sure you want to delete this ${(_b = model.singularName) === null || _b === void 0 ? void 0 : _b.toLowerCase()}?`, title: `Delete ${model.singularName}`, onSubmit: async () => {
                setShowModal(false);
                await deleteItem();
            }, onClose: () => {
                setShowModal(false);
            }, submitButtonText: `Delete ${model.singularName}`, submitButtonType: ButtonStyleType.DANGER })) : (React.createElement(React.Fragment, null)),
        showErrorModal ? (React.createElement(ConfirmModal, { description: error, title: `Delete Error`, onSubmit: () => {
                setShowErrorModal(false);
                setError("");
            }, submitButtonText: `Close`, submitButtonType: ButtonStyleType.NORMAL })) : (React.createElement(React.Fragment, null))));
};
export default ModelDelete;
//# sourceMappingURL=ModelDelete.js.map