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 ObjectID from "../../../Types/ObjectID";
import React, { useState } from "react";
const ResetObjectID = (props) => {
    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 [showResultModal, setShowResultModal] = useState(false);
    const [newId, setNewId] = useState(null);
    const resetKey = async () => {
        setIsLoading(true);
        try {
            const resetIdTo = ObjectID.generate();
            setNewId(resetIdTo);
            await ModelAPI.updateById({
                modelType: props.modelType,
                id: props.modelId,
                data: {
                    [props.fieldName]: resetIdTo.toString(),
                },
            });
            setNewId(resetIdTo);
            setShowModal(false);
            setShowResultModal(true);
        }
        catch (err) {
            setError(API.getFriendlyMessage(err));
            setShowErrorModal(true);
        }
        setIsLoading(false);
    };
    const tableColumn = props.fieldName
        ? model.getTableColumnMetadata(props.fieldName)
        : undefined;
    const tableColumnName = (tableColumn === null || tableColumn === void 0 ? void 0 : tableColumn.title) || props.fieldName;
    return (React.createElement(React.Fragment, null,
        React.createElement(Card, { title: `${props.title}`, description: props.description, buttons: [
                {
                    title: `${props.title}`,
                    buttonStyle: ButtonStyleType.NORMAL,
                    onClick: () => {
                        setShowModal(true);
                    },
                    isLoading: isLoading,
                    icon: IconProp.Reload,
                },
            ] }),
        showModal ? (React.createElement(ConfirmModal, { description: `Are you sure you want to reset ${tableColumnName}?`, title: `Reset ${tableColumnName}`, onSubmit: async () => {
                await resetKey();
            }, isLoading: isLoading, onClose: () => {
                setShowModal(false);
            }, submitButtonText: `Reset`, submitButtonType: ButtonStyleType.DANGER })) : (React.createElement(React.Fragment, null)),
        showErrorModal ? (React.createElement(ConfirmModal, { description: error, title: `Reset Error`, onSubmit: () => {
                setShowErrorModal(false);
                setError("");
            }, submitButtonText: `Close`, submitButtonType: ButtonStyleType.NORMAL })) : (React.createElement(React.Fragment, null)),
        showResultModal ? (React.createElement(ConfirmModal, { description: `Your new ${tableColumnName} is ${(newId === null || newId === void 0 ? void 0 : newId.toString()) || ""}`, title: `New ${tableColumnName}`, onSubmit: () => {
                if (props.onUpdateComplete && newId) {
                    props.onUpdateComplete(newId);
                }
                setShowResultModal(false);
                setError("");
            }, submitButtonText: `Close`, submitButtonType: ButtonStyleType.NORMAL })) : (React.createElement(React.Fragment, null))));
};
export default ResetObjectID;
//# sourceMappingURL=ResetObjectID.js.map