"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_core_1 = require("@patternfly/react-core");
const common_1 = require("../../../common");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const EnvironmentErrors_1 = require("../InfraEnv/EnvironmentErrors");
const DownloadIpxeScript_1 = tslib_1.__importDefault(require("../../../common/components/clusterConfiguration/DownloadIpxeScript"));
const hooks_1 = require("../../hooks");
const AddHostModal = ({ isOpen, onClose, infraEnv, agentClusterInstall, onSaveISOParams, docVersion, isIPXE, }) => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const hasDHCP = ((_b = (_a = infraEnv.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b.networkType) !== 'static';
    const sshPublicKey = ((_c = infraEnv.spec) === null || _c === void 0 ? void 0 : _c.sshAuthorizedKey) || ((_d = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _d === void 0 ? void 0 : _d.sshPublicKey);
    const { httpProxy, httpsProxy, noProxy } = ((_e = infraEnv.spec) === null || _e === void 0 ? void 0 : _e.proxy) || {};
    const imageType = ((_f = infraEnv.spec) === null || _f === void 0 ? void 0 : _f.imageType) || 'minimal-iso';
    const [dialogType, setDialogType] = React.useState('config');
    const [agentServiceConfig, loaded, error] = (0, hooks_1.useAgentServiceConfig)({ name: 'agent' });
    const ciscoUrl = loaded && !error
        ? (_h = (_g = agentServiceConfig === null || agentServiceConfig === void 0 ? void 0 : agentServiceConfig.metadata) === null || _g === void 0 ? void 0 : _g.annotations) === null || _h === void 0 ? void 0 : _h['ciscoIntersightURL']
        : undefined;
    const handleIsoConfigSubmit = (values, formikActions) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        try {
            yield onSaveISOParams(values);
            setDialogType('download');
        }
        catch (error) {
            formikActions.setStatus({
                error: {
                    title: t('ai:Failed to download the discovery Image'),
                    message: error, // TODO(mlibra): parse it better!!
                },
            });
        }
    });
    return (React.createElement(react_core_1.Modal, { "aria-label": t('ai:Add host dialog'), isOpen: isOpen, onClose: onClose, variant: react_core_1.ModalVariant.small, id: "add-host-modal" },
        React.createElement(react_core_1.ModalHeader, { title: t('ai:Add hosts') }),
        React.createElement(EnvironmentErrors_1.EnvironmentErrors, { infraEnv: infraEnv, docVersion: docVersion },
            dialogType === 'config' && (React.createElement(common_1.DiscoveryImageConfigForm, { onCancel: onClose, handleSubmit: handleIsoConfigSubmit, hideDiscoveryImageType: isIPXE, imageType: imageType, sshPublicKey: sshPublicKey, httpProxy: httpProxy, httpsProxy: httpsProxy, noProxy: noProxy, hasDHCP: hasDHCP, isIPXE: isIPXE, allowEmpty: true, docVersion: docVersion })),
            dialogType === 'download' && (React.createElement(React.Fragment, null, isIPXE ? (React.createElement(GeneratingIPXEDownload, { onClose: onClose, infraEnv: infraEnv, onReset: agentClusterInstall ? () => setDialogType('config') : undefined })) : (React.createElement(GeneratingIsoDownload, { onClose: onClose, infraEnv: infraEnv, onReset: agentClusterInstall ? () => setDialogType('config') : undefined, hasDHCP: hasDHCP, docVersion: docVersion, ciscoUrl: ciscoUrl })))))));
};
const GeneratingIsoDownload = ({ infraEnv, onClose, onReset, hasDHCP, docVersion, ciscoUrl, }) => {
    var _a;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    return ((_a = infraEnv.status) === null || _a === void 0 ? void 0 : _a.isoDownloadURL) ? (React.createElement(common_1.DownloadIso, { onClose: onClose, downloadUrl: infraEnv.status.isoDownloadURL, onReset: onReset, hasDHCP: hasDHCP, docVersion: docVersion, ciscoUrl: ciscoUrl })) : (React.createElement(react_core_1.ModalBody, null,
        React.createElement(react_core_1.EmptyState, { icon: react_core_1.Spinner },
            React.createElement(react_core_1.EmptyStateBody, null, t('ai:Generating discovery ISO')))));
};
const GeneratingIPXEDownload = ({ infraEnv, onClose, onReset, }) => {
    var _a, _b;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    return ((_b = (_a = infraEnv.status) === null || _a === void 0 ? void 0 : _a.bootArtifacts) === null || _b === void 0 ? void 0 : _b.ipxeScript) ? (React.createElement(DownloadIpxeScript_1.default, { onClose: onClose, downloadUrl: infraEnv.status.bootArtifacts.ipxeScript, onReset: onReset })) : (React.createElement(react_core_1.ModalBody, null,
        React.createElement(react_core_1.EmptyState, { icon: react_core_1.Spinner },
            React.createElement(react_core_1.EmptyStateBody, null, t('ai:Generating iPXE script')))));
};
exports.default = AddHostModal;
//# sourceMappingURL=AddHostModal.js.map