"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 axios_1 = tslib_1.__importDefault(require("axios"));
const api_1 = require("../../../common/api");
const common_1 = require("../../../common");
const slice_1 = require("../../store/slices/current-cluster/slice");
const useInfraEnv_1 = tslib_1.__importDefault(require("../../hooks/useInfraEnv"));
const services_1 = require("../../services");
const OcmDiscoveryImageConfigForm_1 = require("./OcmDiscoveryImageConfigForm");
const CpuArchitectureService_1 = require("../../services/CpuArchitectureService");
const hooks_1 = require("../../hooks");
const utils_1 = require("../utils");
const DiscoveryImageForm = ({ cluster, onCancel, onSuccess, cpuArchitecture, onSuccessIpxe, isIpxeSelected, }) => {
    var _a, _b, _c;
    const pullSecret = (0, hooks_1.usePullSecret)();
    const { infraEnv, error: infraEnvError } = (0, useInfraEnv_1.default)(cluster.id, cpuArchitecture, cluster.name, pullSecret, cluster.openshiftVersion);
    const cancelSourceRef = react_1.default.useRef();
    const dispatch = (0, react_redux_1.useDispatch)();
    react_1.default.useEffect(() => {
        cancelSourceRef.current = axios_1.default.CancelToken.source();
        return () => { var _a; return (_a = cancelSourceRef.current) === null || _a === void 0 ? void 0 : _a.cancel('Image generation cancelled by user.'); };
    }, []);
    const handleCancel = () => {
        dispatch((0, slice_1.forceReload)());
        onCancel();
    };
    const handleSubmit = (formValues, formikActions) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if (cluster.id && (infraEnv === null || infraEnv === void 0 ? void 0 : infraEnv.id)) {
            const isIPXE = formValues['imageType'] === 'discovery-image-ipxe';
            try {
                const { updatedCluster } = yield services_1.DiscoveryImageFormService.update(cluster.id, cluster.tags, infraEnv.id, formValues, undefined, isIPXE);
                isIPXE ? yield onSuccessIpxe() : yield onSuccess();
                dispatch((0, slice_1.updateCluster)(updatedCluster));
            }
            catch (error) {
                (0, api_1.handleApiError)(error, () => {
                    formikActions.setStatus({
                        error: {
                            title: isIPXE ? 'Failed to create ipxe scripts' : 'Failed to create discovery image',
                            message: (0, api_1.getApiErrorMessage)(error),
                        },
                    });
                });
                if ((0, api_1.isUnknownServerError)(error)) {
                    dispatch((0, slice_1.setServerUpdateError)());
                    onCancel();
                }
            }
        }
    });
    if (infraEnvError) {
        return react_1.default.createElement(common_1.ErrorState, null);
    }
    if (!infraEnv) {
        return react_1.default.createElement(common_1.LoadingState, null);
    }
    return (react_1.default.createElement(OcmDiscoveryImageConfigForm_1.OcmDiscoveryImageConfigForm, { onCancel: handleCancel, handleSubmit: handleSubmit, sshPublicKey: infraEnv.sshAuthorizedKey, httpProxy: (_a = infraEnv.proxy) === null || _a === void 0 ? void 0 : _a.httpProxy, httpsProxy: (_b = infraEnv.proxy) === null || _b === void 0 ? void 0 : _b.httpsProxy, noProxy: (_c = infraEnv.proxy) === null || _c === void 0 ? void 0 : _c.noProxy, imageType: infraEnv.type, enableCertificate: infraEnv.additionalTrustBundle !== undefined, trustBundle: infraEnv.additionalTrustBundle, isIpxeSelected: isIpxeSelected, selectedCpuArchitecture: cpuArchitecture === common_1.CpuArchitecture.USE_DAY1_ARCHITECTURE
            ? (0, CpuArchitectureService_1.mapClusterCpuArchToInfraEnvCpuArch)(infraEnv.cpuArchitecture)
            : (0, CpuArchitectureService_1.mapClusterCpuArchToInfraEnvCpuArch)(cpuArchitecture), isOracleCloudInfrastructure: (0, utils_1.isOciPlatformType)(cluster) }));
};
exports.default = DiscoveryImageForm;
//# sourceMappingURL=DiscoveryImageForm.js.map