"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_core_1 = require("@patternfly/react-core");
const formik_1 = require("formik");
const React = tslib_1.__importStar(require("react"));
const NodePoolForm_1 = tslib_1.__importDefault(require("./NodePoolForm"));
const utils_1 = require("../utils");
const use_translation_wrapper_1 = require("../../../../common/hooks/use-translation-wrapper");
const utils_2 = require("../../../../common/utils");
const getPatches = (values, nodePool) => {
    const patches = [];
    if (values.useAutoscaling) {
        if (!!nodePool.spec.autoScaling) {
            patches.push({
                op: 'replace',
                value: {
                    min: values.autoscaling.minReplicas,
                    max: values.autoscaling.maxReplicas,
                },
                path: '/spec/autoScaling',
            });
        }
        else {
            patches.push({ op: 'remove', path: '/spec/replicas' }, {
                op: 'add',
                value: {
                    min: values.autoscaling.minReplicas,
                    max: values.autoscaling.maxReplicas,
                },
                path: '/spec/autoScaling',
            });
        }
    }
    else {
        if (nodePool.spec.replicas !== undefined) {
            patches.push({
                op: 'replace',
                value: values.count,
                path: '/spec/replicas',
            });
        }
        else {
            patches.push({ op: 'remove', path: '/spec/autoScaling' }, {
                op: 'add',
                value: values.count,
                path: '/spec/replicas',
            });
        }
    }
    return patches;
};
const ManageHostsModal = ({ onClose, nodePool, hostedCluster, agents, onSubmit, agentMachines, }) => {
    var _a, _b, _c, _d, _e, _f;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const [error, setError] = React.useState();
    const namespaceAgents = agents.filter((a) => { var _a, _b; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.namespace) === ((_b = hostedCluster.spec.platform.agent) === null || _b === void 0 ? void 0 : _b.agentNamespace); });
    const handleSubmit = (values) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        setError(undefined);
        const patches = getPatches(values, nodePool);
        try {
            yield onSubmit(nodePool, patches);
            onClose();
        }
        catch (err) {
            setError((0, utils_2.getErrorMessage)(err));
        }
    });
    return (React.createElement(react_core_1.Modal, { "aria-label": "Manage hosts dialog", isOpen: true, onClose: onClose, variant: react_core_1.ModalVariant.medium },
        React.createElement(react_core_1.ModalHeader, { title: t('ai:Manage hosts') }),
        React.createElement(formik_1.Formik, { initialValues: {
                nodePoolName: ((_a = nodePool.metadata) === null || _a === void 0 ? void 0 : _a.name) || '',
                agentLabels: (0, utils_1.labelsToFormikValue)(((_d = (_c = (_b = nodePool.spec.platform) === null || _b === void 0 ? void 0 : _b.agent) === null || _c === void 0 ? void 0 : _c.agentLabelSelector) === null || _d === void 0 ? void 0 : _d.matchLabels) || {}),
                count: nodePool.spec.replicas || 0,
                useAutoscaling: !!nodePool.spec.autoScaling,
                autoscaling: {
                    minReplicas: ((_e = nodePool.spec.autoScaling) === null || _e === void 0 ? void 0 : _e.min) || 1,
                    maxReplicas: ((_f = nodePool.spec.autoScaling) === null || _f === void 0 ? void 0 : _f.max) || 1,
                },
            }, isInitialValid: false, onSubmit: handleSubmit }, ({ isSubmitting, isValid, submitForm }) => (React.createElement(React.Fragment, null,
            React.createElement(react_core_1.ModalBody, null,
                React.createElement(react_core_1.Stack, { hasGutter: true },
                    React.createElement(react_core_1.StackItem, null,
                        React.createElement(NodePoolForm_1.default, { agents: namespaceAgents, agentMachines: agentMachines, nodePool: nodePool, hostedCluster: hostedCluster })),
                    error && (React.createElement(react_core_1.StackItem, null,
                        React.createElement(react_core_1.Alert, { variant: "danger", title: error, isInline: true }))))),
            React.createElement(react_core_1.ModalFooter, null,
                React.createElement(react_core_1.Button
                // eslint-disable-next-line @typescript-eslint/no-misused-promises
                , { 
                    // eslint-disable-next-line @typescript-eslint/no-misused-promises
                    onClick: submitForm, isDisabled: !isValid || isSubmitting, icon: isSubmitting ? React.createElement(react_core_1.Spinner, { size: "md" }) : undefined }, t('ai:Update')),
                React.createElement(react_core_1.Button, { variant: "link", onClick: onClose }, t('ai:Cancel'))))))));
};
exports.default = ManageHostsModal;
//# sourceMappingURL=ManageHostsModal.js.map