"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_redux_1 = require("react-redux");
const react_core_1 = require("@patternfly/react-core");
const exclamation_triangle_icon_1 = require("@patternfly/react-icons/dist/js/icons/exclamation-triangle-icon");
const api_1 = require("../../../common/api");
const slice_1 = require("../../store/slices/current-cluster/slice");
const utils_1 = require("../clusters/utils");
const ModalDialogsContext_1 = require("../hosts/ModalDialogsContext");
const common_1 = require("../../../common");
const apis_1 = require("../../services/apis");
const ResetClusterModal = () => {
    var _a;
    const dispatch = (0, react_redux_1.useDispatch)();
    const [isSubmitting, setIsSubmitting] = react_1.default.useState(false);
    const [error, setError] = react_1.default.useState(null);
    const { resetClusterDialog } = (0, ModalDialogsContext_1.useModalDialogsContext)();
    const { data, isOpen, close: onClose } = resetClusterDialog;
    const cluster = data === null || data === void 0 ? void 0 : data.cluster;
    if (!cluster) {
        return null;
    }
    const handleClose = () => {
        setIsSubmitting(false);
        setError(null);
        onClose();
    };
    const handleReset = () => {
        const doItAsync = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            setIsSubmitting(true);
            try {
                setError(null);
                const { data } = yield apis_1.ClustersAPI.reset(cluster.id);
                dispatch((0, slice_1.updateCluster)(data));
                onClose();
            }
            catch (e) {
                (0, api_1.handleApiError)(e, () => {
                    setError({
                        title: 'Failed to reset cluster installation',
                        message: (0, api_1.getApiErrorMessage)(e),
                    });
                });
            }
            setIsSubmitting(false);
        });
        void doItAsync();
    };
    const collectedLogsPercentage = `${Math.round(((0, utils_1.calculateCollectedLogsCount)(cluster) / ((((_a = cluster.hosts) === null || _a === void 0 ? void 0 : _a.length) || 0) + 1)) * 100)} `;
    const getModalContent = () => {
        if (isSubmitting) {
            return react_1.default.createElement(common_1.LoadingState, { content: "Resetting cluster installation..." });
        }
        if (error) {
            return react_1.default.createElement(common_1.ErrorState, { title: error.title, content: error.message });
        }
        return (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(react_core_1.Content, { component: "p" }, "This will reset the installation and return to the cluster configuration. Some hosts may need to be re-registered by rebooting into the Discovery ISO."),
            react_1.default.createElement(react_core_1.Content, { component: "p" },
                react_1.default.createElement("span", { className: "pf-v6-u-font-weight-bold" }, "Download the installation logs"),
                " to troubleshoot or report a bug.",
                react_1.default.createElement("br", null),
                "Currently, ",
                collectedLogsPercentage,
                "% of the installation logs were collected and are ready for download."),
            react_1.default.createElement(react_core_1.Content, { component: "p" },
                react_1.default.createElement(react_core_1.Icon, { size: "sm", status: "warning" },
                    react_1.default.createElement(exclamation_triangle_icon_1.ExclamationTriangleIcon, null)),
                ' ',
                "Logs won't be available after the installation is reset."),
            react_1.default.createElement(react_core_1.Content, { component: "p" }, "Are you sure you want to reset the cluster?")));
    };
    const actions = [
        react_1.default.createElement(react_core_1.Button, { key: "reset", variant: react_core_1.ButtonVariant.danger, onClick: handleReset, isDisabled: isSubmitting }, "Reset Cluster"),
    ];
    actions.push(react_1.default.createElement(react_core_1.Button, { key: "cancel", variant: react_core_1.ButtonVariant.link, onClick: handleClose, isDisabled: isSubmitting }, "Cancel"));
    return (react_1.default.createElement(react_core_1.Modal, { isOpen: isOpen, variant: react_core_1.ModalVariant.small, onClose: handleClose },
        react_1.default.createElement(react_core_1.ModalHeader, { title: "Reset Cluster Installation" }),
        react_1.default.createElement(react_core_1.ModalBody, null, getModalContent()),
        react_1.default.createElement(react_core_1.ModalFooter, null, actions)));
};
exports.default = ResetClusterModal;
//# sourceMappingURL=ResetClusterModal.js.map