"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomManifestsPage = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const CustomManifests_1 = require("./components/CustomManifests");
const common_1 = require("../../../../common");
const formik_1 = require("../../../../common/components/ui/formik");
const utils_1 = require("../../utils");
const DeleteCustomManifestModal_1 = tslib_1.__importDefault(require("./DeleteCustomManifestModal"));
const services_1 = require("../../../services");
const ClusterWizardContext_1 = require("../../clusterWizard/ClusterWizardContext");
const CustomManifestsPage = ({ cluster, onFormStateChange, }) => {
    const clusterWizardContext = (0, ClusterWizardContext_1.useClusterWizardContext)();
    const isRequired = (0, common_1.isThirdPartyCNI)(cluster.networkType) || (0, utils_1.isOciPlatformType)(cluster);
    const [useCustomManifests, setUseCustomManifests] = react_1.default.useState(isRequired);
    const [isDeleteModalOpen, setDeleteModalOpen] = react_1.default.useState(false);
    react_1.default.useEffect(() => {
        var _a;
        if (isRequired || ((_a = clusterWizardContext.uiSettings) === null || _a === void 0 ? void 0 : _a.customManifestsAdded)) {
            setUseCustomManifests(true);
        }
    }, [isRequired, clusterWizardContext]);
    // The form unmounts when the switch is off, but its last state persists in the parent.
    react_1.default.useEffect(() => {
        if (!useCustomManifests) {
            onFormStateChange({
                isValid: true,
                isSubmitting: false,
                isAutoSaveRunning: false,
                errors: {},
                touched: {},
                isEmpty: true,
            });
        }
    }, [useCustomManifests, onFormStateChange]);
    const handleSwitchChange = react_1.default.useCallback((_event, on) => {
        var _a;
        if (!on && ((_a = clusterWizardContext.uiSettings) === null || _a === void 0 ? void 0 : _a.customManifestsAdded)) {
            setDeleteModalOpen(true);
        }
        else {
            setUseCustomManifests(on);
            if (!on) {
                void clusterWizardContext.updateUISettings({ customManifestsAdded: false });
            }
        }
    }, [clusterWizardContext]);
    const handleCloseDeleteModal = react_1.default.useCallback(() => {
        setDeleteModalOpen(false);
    }, []);
    const cleanCustomManifests = react_1.default.useCallback(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        var _a;
        if (!((_a = clusterWizardContext.uiSettings) === null || _a === void 0 ? void 0 : _a.customManifestsAdded) || !cluster.id)
            return;
        const { data: manifests } = yield common_1.ClustersAPI.getManifests(cluster.id);
        yield services_1.ClustersService.removeClusterManifests(manifests, cluster.id);
        setUseCustomManifests(false);
        setDeleteModalOpen(false);
        yield clusterWizardContext.updateUISettings({
            customManifestsAdded: false,
            customManifestsUpdated: true,
        });
    }), [cluster.id, clusterWizardContext]);
    const fieldId = (0, formik_1.getFieldId)('use-custom-manifests', 'switch');
    const tooltipWhenDisabled = isRequired
        ? 'Custom manifests are required when using a third-party CNI or Oracle Cloud Infrastructure.'
        : '';
    return (react_1.default.createElement(react_core_1.Grid, { hasGutter: true },
        react_1.default.createElement(react_core_1.Content, null,
            react_1.default.createElement(common_1.ClusterWizardStepHeader, null, "Custom manifests"),
            react_1.default.createElement(react_core_1.Content, { component: react_core_1.ContentVariants.small }, "Upload additional manifests that will be applied at the install time for advanced configuration of the cluster.")),
        react_1.default.createElement(react_core_1.FormGroup, { fieldId: fieldId, label: "" },
            react_1.default.createElement(react_core_1.Tooltip, { content: tooltipWhenDisabled, hidden: !isRequired },
                react_1.default.createElement("span", { style: { display: 'inline-block', cursor: isRequired ? 'not-allowed' : undefined } },
                    react_1.default.createElement(react_core_1.Switch, { id: fieldId, "data-testid": fieldId, "data-checked": useCustomManifests, isChecked: useCustomManifests, isDisabled: isRequired, onChange: handleSwitchChange, label: "Use custom manifests" })))),
        !useCustomManifests && react_1.default.createElement("div", { style: { minHeight: '250px' }, "aria-hidden": "true" }),
        useCustomManifests && (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(common_1.CustomManifestsAlert, { usesExternalPlatform: (0, utils_1.isOciPlatformType)(cluster), usesThirdPartyCNI: (0, common_1.isThirdPartyCNI)(cluster.networkType) }),
            react_1.default.createElement(react_core_1.Alert, { isInline: true, variant: "warning", title: "No validation is performed for the custom manifest contents. Only include resources that are necessary for initial setup to reduce the chance of installation failures." }),
            react_1.default.createElement(CustomManifests_1.CustomManifests, { cluster: cluster, onFormStateChange: onFormStateChange }))),
        react_1.default.createElement(DeleteCustomManifestModal_1.default, { isOpen: isDeleteModalOpen, onClose: handleCloseDeleteModal, onDelete: () => void cleanCustomManifests() })));
};
exports.CustomManifestsPage = CustomManifestsPage;
//# sourceMappingURL=CustomManifestsPage.js.map