"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.MultiLineRenderer = void 0;
var react_1 = __importStar(require("react"));
var ramda_1 = require("ramda");
var classnames_1 = __importDefault(require("classnames"));
var react_context_selector_1 = require("@fluentui/react-context-selector");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ReadOnlyAttributesFactory_1 = require("../../../ReadOnlyAttributesFactory");
var AttributeTitle_1 = require("../../../AttributeTitle");
var ShowMore_1 = require("../../../ShowMore");
var ShowLess_1 = require("../../../ShowLess");
var OvIcon_1 = require("../../../OvIcon");
var DescriptionIcon_1 = require("../../../DescriptionIcon");
var ControlAttributeIcon_1 = require("../../../ControlAttributeIcon");
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var AttributesVerificationContext_1 = require("../../../contexts/AttributesVerificationContext");
var VerificationButton_1 = require("../../../VerificationButton/VerificationButton");
var SyncedValueHeight_1 = require("../../../SyncedValueHeight");
var styles_1 = require("../../styles");
var MultiLineRenderer = function (_a) {
    var max = _a.max, values = _a.values, nonVisibleValues = _a.nonVisibleValues, totalVisibleValues = _a.totalVisibleValues, attributeType = _a.attributeType, parentUri = _a.parentUri, requestNextPageOfAttributeValues = _a.requestNextPageOfAttributeValues, showNonOv = _a.showNonOv, totalNonVisibleValues = _a.totalNonVisibleValues, classes = _a.classes, FallbackSlot = _a.FallbackSlot;
    var styles = (0, styles_1.useMultiLineStyles)();
    var _b = (0, react_1.useState)(max), visibleValuesCount = _b[0], setVisibleValuesCount = _b[1];
    var isVerificationAttribute = (0, react_context_selector_1.useContextSelector)(AttributesVerificationContext_1.AttributesVerificationContext, function (context) { return (context === null || context === void 0 ? void 0 : context.isVerificationAttribute) || ramda_1.F; });
    var controlAttributes = (0, MdmModuleContext_1.useMdmControlAttributes)();
    var isControlAttribute = (0, react_1.useMemo)(function () { return controlAttributes.includes(attributeType.uri); }, [controlAttributes, attributeType.uri]);
    var hasPaging = max < totalVisibleValues;
    var showMore = hasPaging && visibleValuesCount < totalVisibleValues;
    var showLess = hasPaging && visibleValuesCount >= totalVisibleValues;
    var label = attributeType.label, description = attributeType.description;
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var hasMasking = (0, react_1.useMemo)(function () { return (0, mdm_sdk_1.checkHasMasking)(metadata, attributeType); }, [metadata, attributeType]);
    var showMasking = (0, MdmModuleContext_1.useMdmShowMasking)();
    var onShowMore = function () {
        if (values.length < totalVisibleValues) {
            requestNextPageOfAttributeValues({
                parentUri: parentUri,
                attributeTypeUri: attributeType.uri,
                attributeTypeName: attributeType.name,
                values: values,
                defaultMaxValues: max,
                options: {
                    sendMasking: showMasking && hasMasking,
                    ovOnly: !showNonOv
                }
            });
        }
        setVisibleValuesCount(visibleValuesCount + max);
    };
    var onShowLess = function () {
        setVisibleValuesCount(max);
    };
    var shownValues = values.slice(0, visibleValuesCount);
    var hiddenValuesCount = totalVisibleValues - visibleValuesCount;
    var showVerificationButton = isVerificationAttribute(attributeType);
    var blockUri = parentUri ? "".concat(parentUri, "/").concat(attributeType.uri) : attributeType.uri;
    return (react_1.default.createElement(SyncedValueHeight_1.ValueHeightReporter, { uri: blockUri },
        react_1.default.createElement("div", { "data-reltio-id": "multi-line-renderer", className: styles.wrapper },
            react_1.default.createElement("div", { className: styles.titleRow },
                react_1.default.createElement(AttributeTitle_1.AttributeTitle, { label: label, className: (0, classnames_1.default)(styles.title, classes === null || classes === void 0 ? void 0 : classes.title), "data-reltio-id": "reltio-attribute-label" }),
                react_1.default.createElement(DescriptionIcon_1.DescriptionIcon, { description: description }),
                isControlAttribute && react_1.default.createElement(ControlAttributeIcon_1.ControlAttributeIcon, null),
                react_1.default.createElement(OvIcon_1.OvIcon, { nonOvValues: nonVisibleValues, attributeType: attributeType, className: styles.ovIcon, nonOvTotal: totalNonVisibleValues }),
                showVerificationButton && react_1.default.createElement(VerificationButton_1.VerificationButton, { attributeType: attributeType })),
            react_1.default.createElement("div", { className: styles.attributes }, shownValues.map(function (value) { return (react_1.default.createElement(SyncedValueHeight_1.ValueHeightReporter, { key: value.uri, uri: value.uri },
                react_1.default.createElement(ReadOnlyAttributesFactory_1.ReadOnlyAttribute, { className: (0, classnames_1.default)(styles.attribute, classes === null || classes === void 0 ? void 0 : classes.content), attributeValue: value, attributeType: attributeType, showNonOv: showNonOv, classes: classes, FallbackSlot: FallbackSlot }))); })),
            showMore && (react_1.default.createElement(ShowMore_1.ShowMore, { moreNumber: (0, ramda_1.min)(max, hiddenValuesCount), valueNumber: hiddenValuesCount, onClick: onShowMore })),
            showLess && react_1.default.createElement(ShowLess_1.ShowLess, { onClick: onShowLess }))));
};
exports.MultiLineRenderer = MultiLineRenderer;
