"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useInfraEnvProxies = exports.useNetworkingFormik = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const Yup = tslib_1.__importStar(require("yup"));
const common_1 = require("../common");
const utils_1 = require("../../../common/components/clusterConfiguration/utils");
const networkConfigurationValidation_1 = require("./networkConfigurationValidation");
const helpers_1 = require("../helpers");
const hooks_1 = require("../../../common/hooks");
const common_2 = require("../../../common");
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const getInfraEnvProxy = (infraEnvs) => {
    const infraEnvWithProxy = infraEnvs.find((ie) => { var _a, _b, _c, _d, _e, _f; return ((_b = (_a = ie.spec) === null || _a === void 0 ? void 0 : _a.proxy) === null || _b === void 0 ? void 0 : _b.httpProxy) || ((_d = (_c = ie.spec) === null || _c === void 0 ? void 0 : _c.proxy) === null || _d === void 0 ? void 0 : _d.httpsProxy) || ((_f = (_e = ie.spec) === null || _e === void 0 ? void 0 : _e.proxy) === null || _f === void 0 ? void 0 : _f.noProxy); });
    return {
        infraEnvWithProxy,
        sameProxies: infraEnvs.every((ie) => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
            return ((_b = (_a = ie.spec) === null || _a === void 0 ? void 0 : _a.proxy) === null || _b === void 0 ? void 0 : _b.httpProxy) === ((_d = (_c = infraEnvWithProxy === null || infraEnvWithProxy === void 0 ? void 0 : infraEnvWithProxy.spec) === null || _c === void 0 ? void 0 : _c.proxy) === null || _d === void 0 ? void 0 : _d.httpProxy) &&
                ((_f = (_e = ie.spec) === null || _e === void 0 ? void 0 : _e.proxy) === null || _f === void 0 ? void 0 : _f.httpsProxy) === ((_h = (_g = infraEnvWithProxy === null || infraEnvWithProxy === void 0 ? void 0 : infraEnvWithProxy.spec) === null || _g === void 0 ? void 0 : _g.proxy) === null || _h === void 0 ? void 0 : _h.httpsProxy) &&
                ((_k = (_j = ie.spec) === null || _j === void 0 ? void 0 : _j.proxy) === null || _k === void 0 ? void 0 : _k.noProxy) === ((_m = (_l = infraEnvWithProxy === null || infraEnvWithProxy === void 0 ? void 0 : infraEnvWithProxy.spec) === null || _l === void 0 ? void 0 : _l.proxy) === null || _m === void 0 ? void 0 : _m.noProxy);
        }),
    };
};
const getNetworkConfigurationValidationSchema = (initialValues, hostSubnets, t, openshiftVersion) => Yup.lazy((values) => {
    var _a, _b, _c, _d, _e, _f;
    return Yup.object().shape({
        clusterNetworks: (0, common_2.clusterNetworksSchema)(t, (_a = values.stackType) !== null && _a !== void 0 ? _a : '', (_c = (_b = values.machineNetworks) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.cidr, openshiftVersion),
        serviceNetworks: (0, common_2.serviceNetworksSchema)(t, (_d = values.stackType) !== null && _d !== void 0 ? _d : '', (_f = (_e = values.machineNetworks) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.cidr, openshiftVersion),
        apiVips: (0, common_2.vipArrayValidationSchema)(hostSubnets, values, t),
        ingressVips: (0, common_2.vipArrayValidationSchema)(hostSubnets, values, t),
        sshPublicKey: (0, common_2.sshPublicKeyListValidationSchema)(t),
        httpProxy: (0, common_2.httpProxyValidationSchema)({ values, pairValueName: 'httpsProxy', t }),
        httpsProxy: (0, common_2.httpProxyValidationSchema)({ values, pairValueName: 'httpProxy', t }), // share the schema, httpS is currently not supported
        noProxy: (0, common_2.noProxyValidationSchema)(t),
    });
});
const useNetworkingFormik = ({ clusterDeployment, agentClusterInstall, agents, }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const initialValues = react_1.default.useMemo(() => {
        const cluster = (0, helpers_1.getAICluster)({
            clusterDeployment,
            agentClusterInstall,
            agents,
        });
        return Object.assign(Object.assign({}, (0, networkConfigurationValidation_1.getNetworkInitialValues)(cluster, common_2.CLUSTER_DEFAULT_NETWORK_SETTINGS)), { enableProxy: false, editProxy: false });
    }, []);
    const validationSchema = react_1.default.useMemo(() => {
        const cluster = (0, helpers_1.getAICluster)({
            clusterDeployment,
            agentClusterInstall,
            agents,
        });
        const hostSubnets = (0, utils_1.getHostSubnets)(cluster);
        return getNetworkConfigurationValidationSchema(initialValues, hostSubnets, t, cluster.openshiftVersion);
    }, [clusterDeployment, agentClusterInstall, agents, t, initialValues]);
    return {
        initialValues,
        validationSchema,
    };
};
exports.useNetworkingFormik = useNetworkingFormik;
const useInfraEnvProxies = ({ agents, fetchInfraEnv }) => {
    const [infraEnvs, setInfraEnvs] = react_1.default.useState();
    const [infraEnvsError, setInfraEnvsError] = react_1.default.useState();
    const { infraEnvWithProxy, sameProxies } = getInfraEnvProxy(infraEnvs || []);
    const infraEnvsMetadata = agents
        .map((a) => {
        var _a, _b, _c;
        return ({
            name: (_b = (_a = a.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b[common_1.INFRAENV_AGENTINSTALL_LABEL_KEY],
            namespace: (_c = a.metadata) === null || _c === void 0 ? void 0 : _c.namespace,
        });
    })
        .filter((ie) => ie.name && ie.namespace)
        .sort();
    const memoInfraEnvs = (0, hooks_1.useDeepCompareMemoize)(infraEnvsMetadata);
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    react_1.default.useEffect(() => {
        const fetch = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            setInfraEnvs(undefined);
            setInfraEnvsError(undefined);
            const infraEnvRequests = memoInfraEnvs.map((ie) => fetchInfraEnv(ie.name, ie.namespace));
            try {
                const infraEnvResults = yield Promise.all(infraEnvRequests);
                setInfraEnvs(infraEnvResults);
            }
            catch (e) {
                const error = e;
                setInfraEnvsError(error.message || t('ai:Could not fetch infra environments'));
            }
        });
        void fetch();
    }, [memoInfraEnvs, fetchInfraEnv, setInfraEnvs, setInfraEnvsError, t]);
    return {
        infraEnvWithProxy,
        sameProxies,
        infraEnvsError,
        infraEnvsLoading: !infraEnvs,
    };
};
exports.useInfraEnvProxies = useInfraEnvProxies;
//# sourceMappingURL=use-networking-formik.js.map