"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClusterDeploymentCustomManifestsStep = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const Yup = tslib_1.__importStar(require("yup"));
const formik_1 = require("formik");
const dynamic_plugin_sdk_1 = require("@openshift-console/dynamic-plugin-sdk");
const react_core_1 = require("@patternfly/react-core");
const external_link_alt_icon_1 = tslib_1.__importDefault(require("@patternfly/react-icons/dist/js/icons/external-link-alt-icon"));
const common_1 = require("../../../../common");
const types_1 = require("../../../types");
const ConfigMapForm_1 = require("./ConfigMapForm");
const utils_1 = require("../../../utils");
const ClusterDeploymentCustomManifestsStep = ({ agentClusterInstall, }) => {
    var _a, _b, _c, _d, _e, _f;
    const { t } = (0, common_1.useTranslation)();
    const initialValues = {
        configMaps: ((_b = (_a = agentClusterInstall.spec) === null || _a === void 0 ? void 0 : _a.manifestsConfigMapRefs) === null || _b === void 0 ? void 0 : _b.map(({ name }) => ({
            name,
            valid: false,
        }))) || [],
    };
    const usesExternalPlatform = ((_c = agentClusterInstall.spec) === null || _c === void 0 ? void 0 : _c.platformType) === 'External';
    const usesThirdPartyCNI = (0, common_1.isThirdPartyCNI)((_d = agentClusterInstall.spec) === null || _d === void 0 ? void 0 : _d.networking.networkType);
    const validationSchema = Yup.lazy((values) => Yup.object({
        configMaps: Yup.array()
            .of(Yup.object().shape({
            name: Yup.string().required(t('ai:Config map name is required')),
            valid: Yup.boolean().isTrue(t('ai:Config map not found')),
            manifestNames: Yup.array()
                .of(Yup.string())
                .test('unique-manifests', t('ai:Manifest names in each config map should be unique across all referenced config maps.'), (value) => 
            // check if another config map has any manifests with the same names as this one
            values.configMaps
                .flatMap(({ manifestNames }) => manifestNames)
                .filter((v) => value === null || value === void 0 ? void 0 : value.includes(v)).length === (value === null || value === void 0 ? void 0 : value.length)),
        }))
            .min(usesExternalPlatform || usesThirdPartyCNI ? 1 : 0, t('ai:At least one config map is required')),
    }));
    const onSubmit = (values) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        var _a;
        const patches = [];
        (0, utils_1.appendPatch)(patches, '/spec/manifestsConfigMapRefs', values.configMaps.map(({ name }) => ({ name })), (_a = agentClusterInstall.spec) === null || _a === void 0 ? void 0 : _a.manifestsConfigMapRefs);
        if (patches.length > 0) {
            yield (0, dynamic_plugin_sdk_1.k8sPatch)({
                model: types_1.AgentClusterInstallModel,
                resource: agentClusterInstall,
                data: patches,
            });
        }
    });
    return (react_1.default.createElement(react_core_1.Grid, { hasGutter: true },
        react_1.default.createElement(react_core_1.GridItem, null,
            react_1.default.createElement(common_1.ClusterWizardStepHeader, null, t('ai:Custom manifests'))),
        react_1.default.createElement(react_core_1.GridItem, null,
            react_1.default.createElement(react_core_1.Alert, { variant: "info", isInline: true, title: t('ai:Add custom manifests'), actionLinks: [
                    react_1.default.createElement(react_core_1.AlertActionLink, { key: "define-config-map", component: 'a', href: `/k8s/ns/${(_e = agentClusterInstall.metadata) === null || _e === void 0 ? void 0 : _e.namespace}/core~v1~ConfigMap`, target: "_blank", rel: "noopener noreferrer" },
                        t('ai:Define a config map'),
                        " ",
                        react_1.default.createElement(external_link_alt_icon_1.default, null)),
                ] },
                react_1.default.createElement(react_core_1.Content, { component: react_core_1.ContentVariants.p }, t('ai:Define a config map with your custom manifests and select it in the form below.')),
                react_1.default.createElement(react_core_1.Content, { component: react_core_1.ContentVariants.p }, t('ai:Manifest names in each config map should be unique across all referenced config maps.')))),
        (usesExternalPlatform || usesThirdPartyCNI) && (react_1.default.createElement(react_core_1.GridItem, null,
            react_1.default.createElement(common_1.CustomManifestsAlert, { usesExternalPlatform: usesExternalPlatform, usesThirdPartyCNI: usesThirdPartyCNI }))),
        react_1.default.createElement(react_core_1.GridItem, null,
            react_1.default.createElement(formik_1.Formik, { initialValues: initialValues, onSubmit: onSubmit, validationSchema: validationSchema },
                react_1.default.createElement(ConfigMapForm_1.ConfigMapForm, { namespace: (_f = agentClusterInstall.metadata) === null || _f === void 0 ? void 0 : _f.namespace })))));
};
exports.ClusterDeploymentCustomManifestsStep = ClusterDeploymentCustomManifestsStep;
//# sourceMappingURL=ClusterDeploymentCustomManifestsStep.js.map