"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddHosts = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const api_1 = require("../../../common/api");
const DiscoveryImageModal_1 = require("../clusterConfiguration/DiscoveryImageModal");
const ModalDialogsContext_1 = require("../hosts/ModalDialogsContext");
const common_1 = require("../../../common");
const InventoryAddHost_1 = tslib_1.__importDefault(require("./InventoryAddHost"));
const fetchEvents_1 = require("../fetching/fetchEvents");
const services_1 = require("../../services");
const ClusterDetailStatusVarieties_1 = tslib_1.__importStar(require("../clusterDetail/ClusterDetailStatusVarieties"));
const ViewClusterEventsButton_1 = tslib_1.__importDefault(require("../../../common/components/ui/ViewClusterEventsButton"));
const { addAlert } = common_1.alertsSlice.actions;
const AddHosts = () => {
    const { cluster, resetCluster, canEdit } = react_1.default.useContext(common_1.AddHostsContext);
    const [isSubmitting, setSubmitting] = react_1.default.useState(false);
    const clusterVarieties = (0, ClusterDetailStatusVarieties_1.useClusterStatusVarieties)(cluster);
    const handleHostsInstall = react_1.default.useCallback(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        setSubmitting(true);
        try {
            if (!cluster || !resetCluster || !cluster.hosts) {
                return;
            }
            const hostsToBeInstalled = cluster.hosts.filter((host) => (0, common_1.canInstallHost)(cluster, host.status));
            yield services_1.HostsService.installAll(hostsToBeInstalled);
            yield resetCluster();
        }
        catch (e) {
            (0, api_1.handleApiError)(e, () => addAlert({
                title: 'Failed to start hosts installation.',
                message: (0, api_1.getApiErrorMessage)(e),
            }));
        }
        finally {
            setTimeout(() => {
                setSubmitting(false);
            }, 10000);
        }
    }), [cluster, resetCluster]);
    if (!cluster || !resetCluster) {
        return null;
    }
    return (react_1.default.createElement(ModalDialogsContext_1.ModalDialogsContextProvider, null,
        react_1.default.createElement(react_core_1.Card, null,
            react_1.default.createElement(react_core_1.CardTitle, null,
                react_1.default.createElement(react_core_1.Title, { headingLevel: "h2", size: "lg", className: "card-title" }, "Host Discovery")),
            react_1.default.createElement(react_core_1.CardBody, null,
                react_1.default.createElement(react_core_1.Stack, { hasGutter: true },
                    react_1.default.createElement(react_core_1.StackItem, null,
                        react_1.default.createElement(react_core_1.Grid, { hasGutter: true },
                            react_1.default.createElement(ClusterDetailStatusVarieties_1.default, { cluster: cluster, clusterVarieties: clusterVarieties }))),
                    react_1.default.createElement(react_core_1.StackItem, null,
                        react_1.default.createElement(InventoryAddHost_1.default, { cluster: cluster })))),
            react_1.default.createElement(react_core_1.CardFooter, null,
                react_1.default.createElement(common_1.Alerts, null),
                react_1.default.createElement(react_core_1.Toolbar, { id: "cluster-toolbar" },
                    react_1.default.createElement(react_core_1.ToolbarContent, null,
                        react_1.default.createElement(common_1.ToolbarButton, { variant: react_core_1.ButtonVariant.primary, name: "install", onClick: () => void handleHostsInstall(), isDisabled: !canEdit || isSubmitting || (0, common_1.getReadyHostCount)(cluster) === 0 }, "Install ready hosts"),
                        react_1.default.createElement(ViewClusterEventsButton_1.default, { cluster: cluster, onFetchEvents: fetchEvents_1.onFetchEvents }))))),
        react_1.default.createElement(DiscoveryImageModal_1.DiscoveryImageModal, null)));
};
exports.AddHosts = AddHosts;
//# sourceMappingURL=AddHosts.js.map