"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OcmDiscoveryImageConfigForm = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const Yup = tslib_1.__importStar(require("yup"));
const react_core_1 = require("@patternfly/react-core");
const formik_1 = require("formik");
const ui_1 = require("../../../common/components/ui");
const ProxyFields_1 = tslib_1.__importDefault(require("../../../common/components/clusterConfiguration/ProxyFields"));
const UploadSSH_1 = tslib_1.__importDefault(require("../../../common/components/clusterConfiguration/UploadSSH"));
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const DiscoveryImageTypeDropdown_1 = tslib_1.__importStar(require("./DiscoveryImageTypeDropdown"));
const CertificateFields_1 = tslib_1.__importDefault(require("../../../common/components/clusterConfiguration/CertificateFields"));
const common_1 = require("../../../common");
const validationSchema = (t) => Yup.lazy((values) => Yup.object().shape({
    sshPublicKey: (0, common_1.sshPublicKeyValidationSchema)(t),
    httpProxy: (0, common_1.httpProxyValidationSchema)({ values, pairValueName: 'httpsProxy', t }),
    httpsProxy: (0, common_1.httpProxyValidationSchema)({ values, pairValueName: 'httpProxy', t }), // share the schema, httpS is currently not supported
    noProxy: (0, common_1.noProxyValidationSchema)(t),
}));
const OcmDiscoveryImageConfigForm = ({ handleSubmit, onCancel, sshPublicKey, httpProxy, httpsProxy, noProxy, imageType, isIpxeSelected, enableCertificate, trustBundle, selectedCpuArchitecture, isOracleCloudInfrastructure = false, }) => {
    const imageTypeValue = isIpxeSelected
        ? 'discovery-image-ipxe'
        : imageType
            ? imageType
            : 'full-iso';
    const initialValues = {
        sshPublicKey: sshPublicKey || '',
        httpProxy: httpProxy || '',
        httpsProxy: httpsProxy || '',
        noProxy: noProxy || '',
        enableProxy: !!(httpProxy || httpsProxy || noProxy),
        imageType: imageTypeValue,
        enableCertificate: enableCertificate || false,
        trustBundle: trustBundle || '',
    };
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const [buttonText, setButtonText] = react_1.default.useState(t('ai:Generate Discovery ISO'));
    const updateDiscoveryButtonAndAlertText = react_1.default.useCallback((isIpxeSelected) => {
        if (!isIpxeSelected) {
            setButtonText(t('ai:Generate Discovery ISO'));
            setAlertDiscoveryText(t('ai:To add hosts to the cluster, generate a Discovery ISO.'));
        }
        else {
            setButtonText(t('ai:Generate script file'));
            setAlertDiscoveryText(t('ai:To add hosts to the cluster, generate iPXE script.'));
        }
    }, [t]);
    react_1.default.useEffect(() => {
        updateDiscoveryButtonAndAlertText(isIpxeSelected);
    }, [updateDiscoveryButtonAndAlertText, isIpxeSelected]);
    const [alertDiscoveryText, setAlertDiscoveryText] = react_1.default.useState(t('ai:To add hosts to the cluster, generate a Discovery ISO.'));
    return (react_1.default.createElement(formik_1.Formik, { initialValues: initialValues, initialStatus: { error: null }, validationSchema: validationSchema(t), onSubmit: handleSubmit }, ({ submitForm, isSubmitting, status, setStatus }) => {
        return (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(react_core_1.ModalBody, 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.Alert, { variant: react_core_1.AlertVariant.info, isInline: true, title: alertDiscoveryText })),
                    react_1.default.createElement(react_core_1.StackItem, null,
                        react_1.default.createElement(react_core_1.Form, null,
                            react_1.default.createElement(DiscoveryImageTypeDropdown_1.default, { name: "imageType", defaultValue: DiscoveryImageTypeDropdown_1.discoveryImageTypes[imageTypeValue], onChange: updateDiscoveryButtonAndAlertText, selectedCpuArchitecture: selectedCpuArchitecture, isDisabled: isOracleCloudInfrastructure }),
                            react_1.default.createElement(UploadSSH_1.default, { labelText: t('ai:Provide an SSH key to be able to connect to the hosts for debugging purposes during the discovery process') }),
                            react_1.default.createElement(ProxyFields_1.default, null),
                            react_1.default.createElement(CertificateFields_1.default, null))))),
            react_1.default.createElement(react_core_1.ModalFooter, null,
                react_1.default.createElement(react_core_1.Stack, { hasGutter: true, style: { width: '100%' } },
                    (status === null || status === void 0 ? void 0 : status.error) && (react_1.default.createElement(react_core_1.StackItem, null,
                        react_1.default.createElement(ui_1.AlertFormikError, { status: status, onClose: () => setStatus(undefined) }))),
                    react_1.default.createElement(react_core_1.StackItem, null,
                        react_1.default.createElement(react_core_1.Flex, null,
                            react_1.default.createElement(react_core_1.FlexItem, null,
                                react_1.default.createElement(react_core_1.Button, { onClick: () => {
                                        void submitForm();
                                    }, isDisabled: isSubmitting, isLoading: isSubmitting, "data-testid": "generate-discovery-iso-button" }, isSubmitting ? t('ai:Generating') : buttonText)),
                            react_1.default.createElement(react_core_1.FlexItem, null,
                                react_1.default.createElement(react_core_1.Button, { key: "cancel", variant: "link", onClick: onCancel }, t('ai:Cancel')))))))));
    }));
};
exports.OcmDiscoveryImageConfigForm = OcmDiscoveryImageConfigForm;
//# sourceMappingURL=OcmDiscoveryImageConfigForm.js.map