"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const exclamation_triangle_icon_1 = require("@patternfly/react-icons/dist/js/icons/exclamation-triangle-icon");
const utils_1 = require("./utils");
const constants_1 = require("../constants");
const use_translation_wrapper_1 = require("../../hooks/use-translation-wrapper");
const utils_2 = require("../../utils");
const Hostname = ({ host, inventory = {}, onEditHostname, title, className, hosts, readonly = false, }) => {
    const hostname = title || (0, utils_1.getHostname)(host, inventory) || constants_1.DASH;
    const isNotLocalhost = hostname !== 'localhost';
    const isUnique = hosts
        ? !hosts.find((h) => {
            const hostInventory = (0, utils_2.stringToJSON)(h.inventory || '') || {};
            return h.id !== host.id && (0, utils_1.getHostname)(h, hostInventory) === hostname;
        })
        : true;
    const isValid = isUnique && isNotLocalhost;
    const body = (react_1.default.createElement(react_core_1.Flex, { alignItems: { default: 'alignItemsCenter' } },
        onEditHostname && !isValid && (react_1.default.createElement(react_core_1.FlexItem, { className: 'pf-v6-u-mr-xs' },
            react_1.default.createElement(react_core_1.Icon, { status: "warning" },
                react_1.default.createElement(exclamation_triangle_icon_1.ExclamationTriangleIcon, null)))),
        react_1.default.createElement(react_core_1.FlexItem, null, hostname)));
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    return !readonly && onEditHostname ? (isValid ? (react_1.default.createElement(react_core_1.Button, { variant: react_core_1.ButtonVariant.link, isInline: true, onClick: onEditHostname, className: className }, body)) : (react_1.default.createElement(react_core_1.Popover, { headerContent: react_1.default.createElement(react_core_1.Flex, { alignItems: { default: 'alignItemsCenter' } },
            react_1.default.createElement(react_core_1.FlexItem, { className: 'pf-v6-u-mr-xs' },
                react_1.default.createElement(react_core_1.Icon, { status: "warning" },
                    react_1.default.createElement(exclamation_triangle_icon_1.ExclamationTriangleIcon, null))),
            react_1.default.createElement(react_core_1.FlexItem, null, t('ai:Hostname is not valid'))), bodyContent: !isNotLocalhost ? t('ai:Hostname can not be localhost') : t('ai:Hostname is not unique'), footerContent: react_1.default.createElement(react_core_1.Button, { variant: react_core_1.ButtonVariant.link, isInline: true, className: className, onClick: onEditHostname }, t('ai:Change hostname')), minWidth: "30rem", maxWidth: "50rem", hideOnOutsideClick: true, zIndex: 300 },
        react_1.default.createElement(react_core_1.Button, { variant: react_core_1.ButtonVariant.link, isInline: true, className: className }, body)))) : (body);
};
exports.default = Hostname;
//# sourceMappingURL=Hostname.js.map