"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 formik_1 = require("formik");
const use_translation_wrapper_1 = require("../../../../common/hooks/use-translation-wrapper");
const utils_1 = require("../../../../common/utils");
const utils_2 = require("../utils");
const NodePoolForm_1 = tslib_1.__importDefault(require("./NodePoolForm"));
const AddNodePoolModal = ({ onSubmit, onClose, agents, hostedCluster, agentsNamespace, }) => {
    var _a;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const [error, setError] = React.useState();
    const namespaceAgents = agents.filter((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.namespace) === agentsNamespace; });
    const handleSubmit = (values) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        var _a, _b;
        setError(undefined);
        const nodePool = {
            apiVersion: 'hypershift.openshift.io/v1beta1',
            kind: 'NodePool',
            metadata: {
                name: values.nodePoolName,
                namespace: (_a = hostedCluster.metadata) === null || _a === void 0 ? void 0 : _a.namespace,
            },
            spec: {
                clusterName: ((_b = hostedCluster.metadata) === null || _b === void 0 ? void 0 : _b.name) || '',
                replicas: values.count,
                management: {
                    autoRepair: false,
                    upgradeType: 'InPlace',
                },
                platform: {
                    type: 'Agent',
                    agent: {
                        agentLabelSelector: {
                            matchLabels: (0, utils_2.formikLabelsToLabels)(values.agentLabels),
                        },
                    },
                },
                release: {
                    image: hostedCluster.spec.release.image,
                },
            },
        };
        try {
            yield onSubmit(nodePool);
            onClose();
        }
        catch (err) {
            setError((0, utils_1.getErrorMessage)(err));
        }
    });
    return (React.createElement(react_core_1.Modal, { "aria-label": "add node pool dialog", isOpen: true, onClose: onClose, variant: react_core_1.ModalVariant.medium },
        React.createElement(react_core_1.ModalHeader, { title: t('ai:Add nodepool') }),
        React.createElement(formik_1.Formik, { initialValues: {
                nodePoolName: `nodepool-${((_a = hostedCluster.metadata) === null || _a === void 0 ? void 0 : _a.name) || ''}-${Math.floor(Math.random() * 100)}`,
                agentLabels: [],
                count: 1,
                useAutoscaling: false,
                autoscaling: {
                    minReplicas: 1,
                    maxReplicas: 1,
                },
            }, 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, hostedCluster: hostedCluster })),
                    error && (React.createElement(react_core_1.StackItem, null,
                        React.createElement(react_core_1.Alert, { variant: "danger", isInline: true, title: error }))))),
            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:Create')),
                React.createElement(react_core_1.Button, { variant: "link", onClick: onClose }, t('ai:Cancel'))))))));
};
exports.default = AddNodePoolModal;
//# sourceMappingURL=AddNodePoolModal.js.map