"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 formik_1 = require("formik");
const common_1 = require("../../../common");
const HostInventory_1 = tslib_1.__importDefault(require("../clusterConfiguration/HostInventory"));
const ClusterWizardContext_1 = require("./ClusterWizardContext");
const wizardTransition_1 = require("./wizardTransition");
const api_1 = require("../../../common/api");
const slice_1 = require("../../store/slices/current-cluster/slice");
const ClusterWizardFooter_1 = tslib_1.__importDefault(require("./ClusterWizardFooter"));
const ClusterWizardNavigation_1 = tslib_1.__importDefault(require("./ClusterWizardNavigation"));
const services_1 = require("../../services");
const selectors_1 = require("../../store/slices/current-cluster/selectors");
const useLateBinding_1 = tslib_1.__importDefault(require("../../hooks/useLateBinding"));
const HostDiscoveryForm = ({ cluster }) => {
    var _a, _b;
    const { alerts } = (0, common_1.useAlerts)();
    const { errors, touched, isSubmitting, isValid } = (0, formik_1.useFormikContext)();
    const clusterWizardContext = (0, ClusterWizardContext_1.useClusterWizardContext)();
    const isAutoSaveRunning = (0, common_1.useFormikAutoSave)();
    const errorFields = (0, common_1.getFormikErrorFields)(errors, touched);
    const isBinding = (0, useLateBinding_1.default)(cluster);
    const hasHosts = ((_b = (_a = cluster.hosts) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0;
    const isNextDisabled = !isValid ||
        !!alerts.length ||
        isAutoSaveRunning ||
        isSubmitting ||
        isBinding ||
        !(0, wizardTransition_1.canNextHostDiscovery)({ cluster });
    const isWaitingForHosts = hasHosts && (isBinding || !(0, wizardTransition_1.canNextHostDiscovery)({ cluster }));
    const footer = (react_1.default.createElement(ClusterWizardFooter_1.default, { cluster: cluster, errorFields: errorFields, isSubmitting: isSubmitting, isNextDisabled: isNextDisabled, isWaitingForHosts: isWaitingForHosts, onNext: () => clusterWizardContext.moveNext(), onBack: () => clusterWizardContext.moveBack(), isBackDisabled: isSubmitting || isAutoSaveRunning }));
    return (react_1.default.createElement(common_1.ClusterWizardStep, { navigation: react_1.default.createElement(ClusterWizardNavigation_1.default, { cluster: cluster }), footer: footer },
        react_1.default.createElement(HostInventory_1.default, { cluster: cluster })));
};
const HostDiscovery = ({ cluster }) => {
    const dispatch = (0, react_redux_1.useDispatch)();
    const { addAlert, clearAlerts } = (0, common_1.useAlerts)();
    const { isViewerMode } = (0, react_redux_1.useSelector)(selectors_1.selectCurrentClusterPermissionsState);
    const initialValues = react_1.default.useMemo(() => (0, common_1.getHostDiscoveryInitialValues)(cluster), 
    // eslint-disable-next-line react-hooks/exhaustive-deps
    []);
    const onSubmit = (values) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        clearAlerts();
        const params = {};
        services_1.HostDiscoveryService.setSchedulableMasters(params, values, cluster);
        try {
            const { data } = yield services_1.ClustersService.update(cluster.id, cluster.tags, params);
            dispatch((0, slice_1.updateCluster)(data));
        }
        catch (e) {
            (0, api_1.handleApiError)(e, () => addAlert({ title: 'Failed to update the cluster', message: (0, api_1.getApiErrorMessage)(e) }));
            if ((0, api_1.isUnknownServerError)(e)) {
                dispatch((0, slice_1.setServerUpdateError)());
            }
        }
    });
    const handleSubmit = isViewerMode ? () => Promise.resolve() : onSubmit;
    return (react_1.default.createElement(formik_1.Formik, { initialValues: initialValues, onSubmit: handleSubmit },
        react_1.default.createElement(HostDiscoveryForm, { cluster: cluster })));
};
exports.default = HostDiscovery;
//# sourceMappingURL=HostDiscovery.js.map