"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RuleTypeInfoButton = void 0;
var react_1 = __importStar(require("react"));
var ramda_1 = require("ramda");
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var IconButton_1 = __importDefault(require("@mui/material/IconButton"));
var List_1 = __importDefault(require("@mui/material/List"));
var ListItem_1 = __importDefault(require("@mui/material/ListItem"));
var ListItemText_1 = __importDefault(require("@mui/material/ListItemText"));
var Popover_1 = __importDefault(require("@mui/material/Popover"));
var Typography_1 = __importDefault(require("@mui/material/Typography"));
var Close_1 = __importDefault(require("@mui/icons-material/Close"));
var Info_1 = __importDefault(require("@mui/icons-material/Info"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var SmallIconButton_1 = require("../../../../../SmallIconButton");
var SourceIcon_1 = require("../../../../../SourceIcon");
var MdmModuleContext_1 = require("../../../../../contexts/MdmModuleContext");
var styles_1 = require("./styles");
var SourcesRenderer = function (_a) {
    var sourcesUriOrder = _a.sourcesUriOrder, metadata = _a.metadata;
    var styles = (0, styles_1.useStyles)();
    return (react_1.default.createElement(react_1.default.Fragment, null, sourcesUriOrder.map(function (sourceUri) { return (react_1.default.createElement(ListItem_1.default, { key: sourceUri, className: styles.popupItem },
        react_1.default.createElement(SourceIcon_1.SourceIcon, { className: styles.sourceIcon, sourceType: sourceUri }),
        react_1.default.createElement(ListItemText_1.default, { primary: (0, mdm_sdk_1.getSourceSystemLabel)(metadata, sourceUri), primaryTypographyProps: { variant: 'body2' } }))); })));
};
var AttributeRenderer = function (_a) {
    var attributeUri = _a.attributeUri, metadata = _a.metadata;
    var styles = (0, styles_1.useStyles)();
    var attributeType = (0, mdm_sdk_1.findAttributeTypeByUri)(metadata, attributeUri);
    return (react_1.default.createElement(ListItem_1.default, { key: attributeType.uri, className: styles.popupItem },
        react_1.default.createElement(ListItemText_1.default, { primary: (0, mdm_sdk_1.getLabel)(attributeType.label), primaryTypographyProps: { variant: 'body2' } })));
};
var getInfoTitle = (0, ramda_1.cond)([
    [(0, ramda_1.prop)('sourcesUriOrder'), function () { return ui_i18n_1.default.text('Source sequence'); }],
    [(0, ramda_1.prop)('primaryAttributeUri'), function () { return ui_i18n_1.default.text('Attribute winner'); }],
    [(0, ramda_1.prop)('comparisonAttributeUri'), function () { return ui_i18n_1.default.text('Comparison attribute'); }]
]);
var getInfoContent = (0, ramda_1.cond)([
    [
        (0, ramda_1.prop)('sourcesUriOrder'),
        function (_a, metadata) {
            var sourcesUriOrder = _a.sourcesUriOrder;
            return react_1.default.createElement(SourcesRenderer, { metadata: metadata, sourcesUriOrder: sourcesUriOrder });
        }
    ],
    [
        (0, ramda_1.prop)('primaryAttributeUri'),
        function (_a, metadata) {
            var primaryAttributeUri = _a.primaryAttributeUri;
            return (react_1.default.createElement(AttributeRenderer, { metadata: metadata, attributeUri: primaryAttributeUri }));
        }
    ],
    [
        (0, ramda_1.prop)('comparisonAttributeUri'),
        function (_a, metadata) {
            var comparisonAttributeUri = _a.comparisonAttributeUri;
            return (react_1.default.createElement(AttributeRenderer, { metadata: metadata, attributeUri: comparisonAttributeUri }));
        }
    ]
]);
var RuleTypeInfoButton = function (_a) {
    var info = _a.info;
    var styles = (0, styles_1.useStyles)();
    var _b = (0, react_1.useState)(null), anchorEl = _b[0], setAnchorEl = _b[1];
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var openPopup = function (event) { return setAnchorEl(event.currentTarget); };
    var closePopup = function () { return setAnchorEl(null); };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(SmallIconButton_1.SmallIconButtonWithTooltip, { icon: Info_1.default, tooltipTitle: ui_i18n_1.default.text('View details'), size: 'XXS', onClick: openPopup }),
        react_1.default.createElement(Popover_1.default, { classes: { paper: styles.rulePopup }, open: !!anchorEl, anchorEl: anchorEl, onClose: closePopup, anchorOrigin: {
                vertical: 'bottom',
                horizontal: 'right'
            }, transformOrigin: {
                vertical: 'top',
                horizontal: 'right'
            } },
            react_1.default.createElement("div", { className: styles.popupTitleWrapper },
                react_1.default.createElement(IconButton_1.default, { className: styles.closeButton, onClick: closePopup, size: "large" },
                    react_1.default.createElement(Close_1.default, null)),
                react_1.default.createElement(Typography_1.default, { variant: 'subtitle1', className: styles.popupTitle }, getInfoTitle(info))),
            react_1.default.createElement(List_1.default, { component: "div", className: styles.popupItemsWrapper }, getInfoContent(info, metadata)))));
};
exports.RuleTypeInfoButton = RuleTypeInfoButton;
