import React, { useMemo, useState } from 'react';
import { min, F } from 'ramda';
import classnames from 'classnames';
import { useContextSelector } from '@fluentui/react-context-selector';
import { checkHasMasking } from '@reltio/mdm-sdk';
import { ReadOnlyAttribute } from '../../../ReadOnlyAttributesFactory';
import { AttributeTitle } from '../../../AttributeTitle';
import { ShowMore } from '../../../ShowMore';
import { ShowLess } from '../../../ShowLess';
import { OvIcon } from '../../../OvIcon';
import { DescriptionIcon } from '../../../DescriptionIcon';
import { ControlAttributeIcon } from '../../../ControlAttributeIcon';
import { useMdmControlAttributes, useMdmMetadata, useMdmShowMasking } from '../../../contexts/MdmModuleContext';
import { AttributesVerificationContext } from '../../../contexts/AttributesVerificationContext';
import { VerificationButton } from '../../../VerificationButton/VerificationButton';
import { ValueHeightReporter } from '../../../SyncedValueHeight';
import { useMultiLineStyles } from '../../styles';
export 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 = useMultiLineStyles();
    var _b = useState(max), visibleValuesCount = _b[0], setVisibleValuesCount = _b[1];
    var isVerificationAttribute = useContextSelector(AttributesVerificationContext, function (context) { return (context === null || context === void 0 ? void 0 : context.isVerificationAttribute) || F; });
    var controlAttributes = useMdmControlAttributes();
    var isControlAttribute = 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 = useMdmMetadata();
    var hasMasking = useMemo(function () { return checkHasMasking(metadata, attributeType); }, [metadata, attributeType]);
    var showMasking = 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.createElement(ValueHeightReporter, { uri: blockUri },
        React.createElement("div", { "data-reltio-id": "multi-line-renderer", className: styles.wrapper },
            React.createElement("div", { className: styles.titleRow },
                React.createElement(AttributeTitle, { label: label, className: classnames(styles.title, classes === null || classes === void 0 ? void 0 : classes.title), "data-reltio-id": "reltio-attribute-label" }),
                React.createElement(DescriptionIcon, { description: description }),
                isControlAttribute && React.createElement(ControlAttributeIcon, null),
                React.createElement(OvIcon, { nonOvValues: nonVisibleValues, attributeType: attributeType, className: styles.ovIcon, nonOvTotal: totalNonVisibleValues }),
                showVerificationButton && React.createElement(VerificationButton, { attributeType: attributeType })),
            React.createElement("div", { className: styles.attributes }, shownValues.map(function (value) { return (React.createElement(ValueHeightReporter, { key: value.uri, uri: value.uri },
                React.createElement(ReadOnlyAttribute, { className: classnames(styles.attribute, classes === null || classes === void 0 ? void 0 : classes.content), attributeValue: value, attributeType: attributeType, showNonOv: showNonOv, classes: classes, FallbackSlot: FallbackSlot }))); })),
            showMore && (React.createElement(ShowMore, { moreNumber: min(max, hiddenValuesCount), valueNumber: hiddenValuesCount, onClick: onShowMore })),
            showLess && React.createElement(ShowLess, { onClick: onShowLess }))));
};
