"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiscoveryImageModal = exports.DiscoveryImageModalButton = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const humanize_plus_1 = require("humanize-plus");
const common_1 = require("../../../common");
const DiscoveryImageForm_1 = tslib_1.__importDefault(require("./DiscoveryImageForm"));
const ModalDialogsContext_1 = require("../hosts/ModalDialogsContext");
const useInfraEnvImageUrl_1 = tslib_1.__importDefault(require("../../hooks/useInfraEnvImageUrl"));
const useInfraEnvIpxeImageUrl_1 = tslib_1.__importDefault(require("../../hooks/useInfraEnvIpxeImageUrl"));
const DownloadIpxeScript_1 = tslib_1.__importDefault(require("../../../common/components/clusterConfiguration/DownloadIpxeScript"));
require("./DiscoveryImageModal.css");
const services_1 = require("../../services");
const react_redux_1 = require("react-redux");
const slice_1 = require("../../store/slices/current-cluster/slice");
const DiscoveryImageModalButton = ({ ButtonComponent = react_core_1.Button, cluster, idPrefix, }) => {
    const { discoveryImageDialog } = (0, ModalDialogsContext_1.useModalDialogsContext)();
    const { open } = discoveryImageDialog;
    const isSNOCluster = (0, common_1.isSNO)(cluster);
    return (react_1.default.createElement(ButtonComponent, { variant: react_core_1.ButtonVariant.secondary, onClick: () => open({ cluster }), id: `${idPrefix}-button-download-discovery-iso` },
        "Add ",
        (0, humanize_plus_1.pluralize)(+isSNOCluster, 'host')));
};
exports.DiscoveryImageModalButton = DiscoveryImageModalButton;
const DiscoveryImageModal = () => {
    const [isoDownloadUrl, setIsoDownloadUrl] = react_1.default.useState('');
    const [isoDownloadError, setIsoDownloadError] = react_1.default.useState('');
    const [ipxeDownloadUrl, setIpxeDownloadUrl] = react_1.default.useState('');
    const [ipxeDownloadError, setIpxeDownloadError] = react_1.default.useState('');
    const [isIpxeSelected, setIpxeSelected] = react_1.default.useState(false);
    const { discoveryImageDialog } = (0, ModalDialogsContext_1.useModalDialogsContext)();
    const { data, isOpen, close } = discoveryImageDialog;
    const cluster = data === null || data === void 0 ? void 0 : data.cluster;
    const { getIsoImageUrl } = (0, useInfraEnvImageUrl_1.default)();
    const { getIpxeImageUrl } = (0, useInfraEnvIpxeImageUrl_1.default)();
    const dispatch = (0, react_redux_1.useDispatch)();
    const onImageReady = react_1.default.useCallback(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        // We need to retrieve the Iso for the only infraEnv on Day1, hence we don't specify the architecture
        const { url, error } = yield getIsoImageUrl(cluster.id, common_1.CpuArchitecture.USE_DAY1_ARCHITECTURE);
        setIsoDownloadUrl(url);
        setIsoDownloadError(error);
    }), [getIsoImageUrl, cluster === null || cluster === void 0 ? void 0 : cluster.id]);
    const onImageIpxeReady = react_1.default.useCallback(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        // We need to retrieve the Iso for the only infraEnv on Day1, hence we don't specify the architecture
        const { url, error } = yield getIpxeImageUrl(cluster.id, common_1.CpuArchitecture.USE_DAY1_ARCHITECTURE);
        setIpxeDownloadUrl(url);
        setIpxeDownloadError(error);
    }), [getIpxeImageUrl, cluster === null || cluster === void 0 ? void 0 : cluster.id]);
    const onReset = react_1.default.useCallback(() => {
        setIsoDownloadUrl('');
        setIpxeSelected(false);
    }, []);
    const onResetIpxe = react_1.default.useCallback(() => {
        setIpxeDownloadUrl('');
        setIpxeSelected(true);
    }, []);
    const updateTagsForCiscoIntersight = (cluster) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        try {
            const { data: updatedCluster } = yield services_1.ClustersService.update(cluster.id, cluster.tags, {
                tags: common_1.AI_CISCO_INTERSIGHT_TAG,
            });
            dispatch((0, slice_1.updateCluster)(updatedCluster));
        }
        catch (e) { }
    });
    if (!cluster) {
        return null;
    }
    const isSNOCluster = (0, common_1.isSNO)(cluster);
    return (react_1.default.createElement(react_core_1.Modal, { "aria-label": "Add hosts dialog", isOpen: isOpen, onClose: close, variant: react_core_1.ModalVariant.small, id: "generate-discovery-iso-modal", ouiaId: "discovery-image-modal", tabIndex: 0 },
        react_1.default.createElement(react_core_1.ModalHeader, { "data-testid": "discovery-modal-title", title: `Add ${(0, humanize_plus_1.pluralize)(+isSNOCluster, 'host')}` }),
        (isoDownloadError || ipxeDownloadError) && react_1.default.createElement(common_1.ErrorState, null),
        isoDownloadUrl ? (react_1.default.createElement(common_1.DownloadIso, { fileName: `discovery_image_${cluster.name || ''}.iso`, downloadUrl: isoDownloadUrl, isSNO: isSNOCluster, onReset: onReset, onClose: close, updateTagsForCiscoIntersight: () => void updateTagsForCiscoIntersight(cluster) })) : ipxeDownloadUrl ? (react_1.default.createElement(DownloadIpxeScript_1.default, { fileName: `discovery_ipxe_script_${cluster.name || ''}.txt`, downloadUrl: ipxeDownloadUrl, isSNO: isSNOCluster, onReset: onResetIpxe, onClose: close })) : (react_1.default.createElement(DiscoveryImageForm_1.default, { cluster: cluster, onCancel: close, onSuccess: onImageReady, cpuArchitecture: common_1.CpuArchitecture.USE_DAY1_ARCHITECTURE, onSuccessIpxe: onImageIpxeReady, isIpxeSelected: isIpxeSelected }))));
};
exports.DiscoveryImageModal = DiscoveryImageModal;
//# sourceMappingURL=DiscoveryImageModal.js.map