"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigMapDetail = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const common_1 = require("../../../../common");
const minus_circle_icon_1 = require("@patternfly/react-icons/dist/js/icons/minus-circle-icon");
const info_circle_icon_1 = tslib_1.__importDefault(require("@patternfly/react-icons/dist/js/icons/info-circle-icon"));
const formik_1 = require("formik");
const ExpandedConfigMap = ({ index, configMap, }) => {
    var _a, _b, _c, _d;
    const { t } = (0, common_1.useTranslation)();
    const [, { error: isValidError }] = (0, formik_1.useField)(`configMaps.${index}.valid`);
    const [, { error: manifestNamesError }] = (0, formik_1.useField)(`configMaps.${index}.manifestNames`);
    return isValidError ? (react_1.default.createElement(react_core_1.GridItem, null,
        react_1.default.createElement(react_core_1.Label, { variant: "outline", icon: react_1.default.createElement(info_circle_icon_1.default, null), color: "red" }, isValidError))) : (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(react_core_1.GridItem, null,
            react_1.default.createElement(react_core_1.DescriptionList, null,
                react_1.default.createElement(react_core_1.DescriptionListGroup, null,
                    react_1.default.createElement(react_core_1.DescriptionListTerm, null, t('ai:Name')),
                    react_1.default.createElement(react_core_1.DescriptionListDescription, null, (_a = configMap === null || configMap === void 0 ? void 0 : configMap.metadata) === null || _a === void 0 ? void 0 : _a.name)),
                react_1.default.createElement(react_core_1.DescriptionListGroup, null,
                    react_1.default.createElement(react_core_1.DescriptionListTerm, null, t('ai:Namespace')),
                    react_1.default.createElement(react_core_1.DescriptionListDescription, null, (_b = configMap === null || configMap === void 0 ? void 0 : configMap.metadata) === null || _b === void 0 ? void 0 : _b.namespace)),
                react_1.default.createElement(react_core_1.DescriptionListGroup, null,
                    react_1.default.createElement(react_core_1.DescriptionListTerm, null, t('ai:Data')),
                    react_1.default.createElement(react_core_1.DescriptionListDescription, null,
                        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.ExternalLink, { href: `/k8s/ns/${((_c = configMap === null || configMap === void 0 ? void 0 : configMap.metadata) === null || _c === void 0 ? void 0 : _c.namespace) || ''}/configmaps/${((_d = configMap === null || configMap === void 0 ? void 0 : configMap.metadata) === null || _d === void 0 ? void 0 : _d.name) || ''}/form` }, t('ai:Edit config map data'))),
                            Object.entries((configMap === null || configMap === void 0 ? void 0 : configMap.data) || {}).map(([key, val]) => (react_1.default.createElement(react_core_1.GridItem, { key: key },
                                react_1.default.createElement(react_core_1.Content, { component: react_core_1.ContentVariants.h4 }, key),
                                react_1.default.createElement(react_core_1.CodeBlock, null,
                                    react_1.default.createElement(react_core_1.CodeBlockCode, null, val)))))))))),
        manifestNamesError && (react_1.default.createElement(react_core_1.GridItem, null,
            react_1.default.createElement(react_core_1.Alert, { title: manifestNamesError, variant: "danger", isInline: true })))));
};
const CollapsedConfigMap = ({ index, configMap, }) => {
    const [, { error: isValidError }] = (0, formik_1.useField)(`configMaps.${index}.valid`);
    const [, { error: manifestNamesError }] = (0, formik_1.useField)(`configMaps.${index}.manifestNames`);
    const { t } = (0, common_1.useTranslation)();
    return (react_1.default.createElement(react_core_1.GridItem, null, isValidError || manifestNamesError ? (react_1.default.createElement(react_core_1.Label, { variant: "outline", icon: react_1.default.createElement(info_circle_icon_1.default, null), color: "red" }, isValidError || manifestNamesError)) : (react_1.default.createElement(react_core_1.Label, { variant: "outline", icon: react_1.default.createElement(info_circle_icon_1.default, null) }, t('ai:{{count}} entry', { count: Object.entries((configMap === null || configMap === void 0 ? void 0 : configMap.data) || {}).length })))));
};
const ConfigMapDetail = ({ index, configMapName, configMap, onRemove, isLoading, }) => {
    const [isExpanded, setIsExpanded] = react_1.default.useState(false);
    const [showRemoveButton, setShowRemoveButton] = react_1.default.useState(false);
    return (react_1.default.createElement(react_core_1.GridItem, null,
        react_1.default.createElement(react_core_1.Grid, { onMouseEnter: () => setShowRemoveButton(true), onMouseLeave: () => setShowRemoveButton(false), hasGutter: true },
            react_1.default.createElement(react_core_1.GridItem, null,
                react_1.default.createElement(react_core_1.Flex, null,
                    react_1.default.createElement(react_core_1.FlexItem, null,
                        react_1.default.createElement(react_core_1.ExpandableSectionToggle, { isExpanded: isExpanded, onToggle: () => setIsExpanded(!isExpanded), direction: "down" }, configMapName)),
                    react_1.default.createElement(react_core_1.FlexItem, { align: { default: 'alignRight' } },
                        react_1.default.createElement(react_core_1.Button, { "aria-label": "remove config map", style: { visibility: showRemoveButton ? 'visible' : 'hidden' }, variant: "plain", onClick: onRemove },
                            react_1.default.createElement(minus_circle_icon_1.MinusCircleIcon, null))))),
            isLoading ? (react_1.default.createElement(react_core_1.GridItem, null,
                react_1.default.createElement(react_core_1.Bullseye, null,
                    react_1.default.createElement(react_core_1.Spinner, { size: "lg" })))) : isExpanded ? (react_1.default.createElement(ExpandedConfigMap, { configMap: configMap, index: index })) : (react_1.default.createElement(CollapsedConfigMap, { configMap: configMap, index: index })),
            react_1.default.createElement(react_core_1.GridItem, null,
                react_1.default.createElement(react_core_1.Divider, null)))));
};
exports.ConfigMapDetail = ConfigMapDetail;
//# sourceMappingURL=ConfigMapDetail.js.map