"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OcmBaseDomainField = exports.BaseDnsHelperText = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const formik_1 = require("formik");
const common_1 = require("../../../common");
const OcmFormFields_1 = require("../ui/OcmFormFields");
const featureStateUtils_1 = require("../featureSupportLevels/featureStateUtils");
const INPUT_NAME = 'baseDnsDomain';
const INPUT_LABEL = 'Base domain';
const fieldId = (0, common_1.getFieldId)(INPUT_NAME, 'input');
const getManagedDomainLabel = (domain) => `${(domain === null || domain === void 0 ? void 0 : domain.domain) || ''} (${(domain === null || domain === void 0 ? void 0 : domain.provider) || ''})`;
const BaseDnsHelperText = ({ name, baseDnsDomain, }) => (react_1.default.createElement(react_core_1.FormHelperText, null,
    react_1.default.createElement(react_core_1.HelperText, { id: fieldId },
        react_1.default.createElement(react_core_1.HelperTextItem, { "data-testid": `input-basednshelpertext-${fieldId}-helper-text` },
            "Enter the name of your domain [domainname] or [domainname.com]. This cannot be changed after cluster installed. All DNS records must include the cluster name and be subdomains of the base you enter. The full cluster address will be: ",
            react_1.default.createElement("br", null),
            react_1.default.createElement("span", { className: "pf-v6-u-font-weight-bold" },
                name || '[Cluster Name]',
                ".",
                baseDnsDomain || '[domainname.com]')))));
exports.BaseDnsHelperText = BaseDnsHelperText;
const OcmBaseDomainField = ({ managedDomains, clusterExists, }) => {
    const [, { value }, { setValue }] = (0, formik_1.useField)(INPUT_NAME);
    const { values } = (0, formik_1.useFormikContext)();
    const { name, baseDnsDomain, useRedHatDnsService } = values;
    const [isOpen, setOpen] = react_1.default.useState(false);
    const dropdownItems = react_1.default.useMemo(() => {
        return managedDomains === null || managedDomains === void 0 ? void 0 : managedDomains.map((d) => (react_1.default.createElement(react_core_1.DropdownItem, { key: d.domain, id: d.domain }, getManagedDomainLabel(d))));
    }, [managedDomains]);
    const dropdownToggle = (toggleRef) => {
        const selectedDomain = managedDomains.find((d) => d.domain === value);
        return (react_1.default.createElement(react_core_1.MenuToggle, { ref: toggleRef, onClick: () => setOpen(!isOpen), isExpanded: isOpen, className: "pf-v6-u-w-100", "data-testid": "base-domain-dropdown-toggle" }, selectedDomain ? getManagedDomainLabel(selectedDomain) : 'Base domain'));
    };
    const toggleRedHatDnsService = (checked) => setValue((checked && managedDomains.map((d) => d.domain)[0]) || '');
    const onSelect = react_1.default.useCallback((event) => {
        const selectedDomain = (event === null || event === void 0 ? void 0 : event.currentTarget.id) || '';
        setValue(selectedDomain);
        setOpen(false);
    }, [setValue]);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        !!managedDomains.length && toggleRedHatDnsService && (react_1.default.createElement(react_core_1.Tooltip, { content: featureStateUtils_1.clusterExistsReason, hidden: !clusterExists },
            react_1.default.createElement(OcmFormFields_1.OcmCheckboxField, { name: "useRedHatDnsService", label: "Use a temporary 60-day domain", helperText: "A base domain will be provided for temporary, non-production clusters.", onChange: toggleRedHatDnsService, isDisabled: clusterExists }))),
        react_1.default.createElement(react_core_1.FormGroup, { id: `form-control__${fieldId}`, fieldId: fieldId, name: INPUT_NAME, label: INPUT_LABEL, isRequired: true },
            useRedHatDnsService ? (react_1.default.createElement(react_core_1.Dropdown, { toggle: dropdownToggle, isOpen: isOpen, onSelect: onSelect, onOpenChange: () => setOpen(!isOpen) }, dropdownItems)) : (react_1.default.createElement(OcmFormFields_1.OcmInputField, { name: INPUT_NAME, placeholder: "localhost.com", isDisabled: useRedHatDnsService, isRequired: true })),
            react_1.default.createElement(exports.BaseDnsHelperText, { name: name, baseDnsDomain: baseDnsDomain }))));
};
exports.OcmBaseDomainField = OcmBaseDomainField;
//# sourceMappingURL=OcmBaseDomainField.js.map