import API from "../../Utils/API/API";
import ModelAPI from "../../Utils/ModelAPI/ModelAPI";
import Navigation from "../../Utils/Navigation";
import { ButtonStyleType } from "../Button/Button";
import Card from "../Card/Card";
import BasicFormModal from "../FormModal/BasicFormModal";
import ConfirmModal from "../Modal/ConfirmModal";
import BaseModel from "../../../Models/DatabaseModels/DatabaseBaseModel/DatabaseBaseModel";
import Route from "../../../Types/API/Route";
import IconProp from "../../../Types/Icon/IconProp";
import React, { useState } from "react";
const DuplicateModel = (props) => {
    var _a, _b, _c;
    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 duplicateItem = async (partialModel) => {
        setIsLoading(true);
        try {
            const item = await ModelAPI.getItem({
                modelType: props.modelType,
                id: props.modelId,
                select: props.fieldsToDuplicate,
            });
            if (!item) {
                throw new Error(`Could not find ${model.singularName} with id ${props.modelId}`);
            }
            for (const field of props.fieldsToChange) {
                const key = Object.keys(field.field || {})[0];
                if (!key) {
                    continue;
                }
                const value = partialModel.getValue(key);
                item.setValue(key, value);
            }
            item.removeValue("_id");
            // now we have the item, we need to remove the id and then save it
            const newItem = (await ModelAPI.create({
                model: item,
                modelType: props.modelType,
            }));
            if (!newItem) {
                throw new Error(`Could not create ${model.singularName}`);
            }
            if (props.onDuplicateSuccess) {
                await props.onDuplicateSuccess(newItem.data);
            }
            if (props.navigateToOnSuccess) {
                Navigation.navigate(new Route(props.navigateToOnSuccess.toString()).addRoute(`/${newItem.data.id.toString()}`), {
                    forceNavigate: true,
                });
            }
        }
        catch (err) {
            setError(API.getFriendlyMessage(err));
            setShowErrorModal(true);
        }
        setIsLoading(false);
    };
    return (React.createElement(React.Fragment, null,
        React.createElement(Card, { title: `Duplicate ${model.singularName}`, description: `Duplicating this ${(_a = model.singularName) === null || _a === void 0 ? void 0 : _a.toLowerCase()} will create another ${(_b = model.singularName) === null || _b === void 0 ? void 0 : _b.toLowerCase()} exactly like this one.`, buttons: [
                {
                    title: `Duplicate ${model.singularName}`,
                    buttonStyle: ButtonStyleType.NORMAL,
                    onClick: () => {
                        setShowModal(true);
                    },
                    isLoading: isLoading,
                    icon: IconProp.Copy,
                },
            ] }),
        showModal ? (React.createElement(BasicFormModal, { description: `Are you sure you want to duplicate this ${(_c = model.singularName) === null || _c === void 0 ? void 0 : _c.toLowerCase()}?`, title: `Duplicate ${model.singularName}`, onSubmit: (item) => {
                setShowModal(false);
                duplicateItem(BaseModel.fromJSONObject(item, props.modelType));
            }, onClose: () => {
                setShowModal(false);
            }, submitButtonText: `Duplicate ${model.singularName}`, formProps: {
                fields: props.fieldsToChange,
            } })) : (React.createElement(React.Fragment, null)),
        showErrorModal ? (React.createElement(ConfirmModal, { description: error, title: `Duplicate Error`, onSubmit: () => {
                setShowErrorModal(false);
                setError("");
            }, submitButtonText: `Close`, submitButtonType: ButtonStyleType.NORMAL })) : (React.createElement(React.Fragment, null))));
};
export default DuplicateModel;
//# sourceMappingURL=DuplicateModel.js.map