"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const Yup = tslib_1.__importStar(require("yup"));
const react_core_1 = require("@patternfly/react-core");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const ProxyFields_1 = require("../../../common/components/clusterConfiguration/ProxyFields");
const formik_1 = require("formik");
const common_1 = require("../../../common");
const utils_1 = require("../../../common/utils");
const utils_2 = require("./utils");
const validationSchema = (t) => Yup.lazy((values) => Yup.object().shape({
    httpProxy: (0, common_1.httpProxyValidationSchema)({
        values,
        pairValueName: 'httpsProxy',
        allowEmpty: true,
        t,
    }),
    httpsProxy: (0, common_1.httpProxyValidationSchema)({
        values,
        pairValueName: 'httpProxy',
        allowEmpty: true,
        t,
    }),
    noProxy: (0, common_1.noProxyValidationSchema)(t),
}));
const Footer = ({ onClose }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const { isSubmitting, submitForm, isValid } = (0, formik_1.useFormikContext)();
    return (React.createElement(react_core_1.ModalFooter, null,
        React.createElement(react_core_1.Button, { onClick: () => void submitForm(), isDisabled: isSubmitting || !isValid, isLoading: isSubmitting }, t('ai:Save')),
        React.createElement(react_core_1.Button, { onClick: onClose, variant: react_core_1.ButtonVariant.secondary }, t('ai:Cancel'))));
};
const EditProxyModal = ({ onClose, infraEnv, onSubmit, hasAgents, hasBMHs, }) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const [error, setError] = React.useState();
    const warningMsg = (0, utils_2.getWarningMessage)(hasAgents, hasBMHs, t);
    const enableProxy = !!((_b = (_a = infraEnv.spec) === null || _a === void 0 ? void 0 : _a.proxy) === null || _b === void 0 ? void 0 : _b.httpProxy) ||
        !!((_d = (_c = infraEnv.spec) === null || _c === void 0 ? void 0 : _c.proxy) === null || _d === void 0 ? void 0 : _d.httpsProxy) ||
        !!((_f = (_e = infraEnv.spec) === null || _e === void 0 ? void 0 : _e.proxy) === null || _f === void 0 ? void 0 : _f.noProxy);
    return (React.createElement(react_core_1.Modal, { "aria-label": t('ai:Edit proxy settings'), isOpen: true, onClose: onClose, variant: react_core_1.ModalVariant.medium },
        React.createElement(react_core_1.ModalHeader, { title: t('ai:Edit proxy settings') }),
        React.createElement(formik_1.Formik, { initialValues: {
                httpProxy: (_h = (_g = infraEnv.spec) === null || _g === void 0 ? void 0 : _g.proxy) === null || _h === void 0 ? void 0 : _h.httpProxy,
                httpsProxy: (_k = (_j = infraEnv.spec) === null || _j === void 0 ? void 0 : _j.proxy) === null || _k === void 0 ? void 0 : _k.httpsProxy,
                noProxy: (_m = (_l = infraEnv.spec) === null || _l === void 0 ? void 0 : _l.proxy) === null || _m === void 0 ? void 0 : _m.noProxy,
                enableProxy,
            }, validationSchema: validationSchema(t), onSubmit: (values) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
                setError(undefined);
                try {
                    yield onSubmit(values, infraEnv);
                    onClose();
                }
                catch (e) {
                    setError((0, utils_1.getErrorMessage)(e));
                }
            }) },
            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(react_core_1.Alert, { isInline: true, variant: "warning", title: warningMsg })),
                        React.createElement(react_core_1.StackItem, null,
                            React.createElement(react_core_1.Form, null,
                                React.createElement(ProxyFields_1.ProxyInputFields, null),
                                error && (React.createElement(react_core_1.Alert, { variant: "danger", title: t('ai:An error occurred'), isInline: true }, error)))))),
                React.createElement(Footer, { onClose: onClose })))));
};
exports.default = EditProxyModal;
//# sourceMappingURL=EditProxyModal.js.map