var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React, { memo, useContext, useMemo, useState } from 'react';
import { path } from 'ramda';
import { getAttributesListForReadMode, getBaseUri } from '@reltio/mdm-sdk';
import { BranchDecorator } from '../BranchDecorator';
import { ReadOnlyAttributesPager } from '../ReadOnlyAttributesPager';
import { ShowMore } from '../ShowMore';
import { ShowLess } from '../ShowLess';
import { FIXED_TYPE_URIS } from '../constants';
import { splitPagersData } from '../helpers/attributesView';
import { HiddenAttributesContext } from '../contexts/HiddenAttributesContext';
import { AlwaysVisibleAttributesContext } from '../contexts/AlwaysVisibleAttributesContext';
var ReadOnlyAttributesList = function (_a) {
    var _b = _a.attrTypes, attrTypes = _b === void 0 ? [] : _b, entity = _a.entity, parentUri = _a.parentUri, drawLines = _a.drawLines, children = _a.children, className = _a.className, max = _a.max, _c = _a.fixedTypeUris, fixedTypeUris = _c === void 0 ? FIXED_TYPE_URIS : _c, showNonOv = _a.showNonOv, classes = _a.classes, FallbackSlot = _a.FallbackSlot;
    var hiddenAttributes = useContext(HiddenAttributesContext);
    var alwaysVisibleAttributes = useContext(AlwaysVisibleAttributesContext);
    var entityUri = parentUri && getBaseUri(parentUri);
    var currentEntityHiddenAttributes = useMemo(function () { return hiddenAttributes[entityUri] || []; }, [hiddenAttributes, entityUri]);
    var filteredAttrTypes = useMemo(function () { return (attrTypes || []).filter(function (attrType) { return !currentEntityHiddenAttributes.includes(attrType.uri); }); }, [attrTypes, currentEntityHiddenAttributes]);
    var _d = useState(max || Infinity), visibleValuesCount = _d[0], setVisibleValuesCount = _d[1];
    var pagersData = useMemo(function () { return getAttributesListForReadMode(filteredAttrTypes, entity, showNonOv, alwaysVisibleAttributes); }, [filteredAttrTypes, entity, showNonOv, alwaysVisibleAttributes]);
    var _e = useMemo(function () { return splitPagersData(fixedTypeUris, pagersData); }, [pagersData, fixedTypeUris]), fixedPagersData = _e[0], regularPagersData = _e[1];
    var visibleAttributePagersData = regularPagersData.slice(0, visibleValuesCount);
    var showMore = max && visibleAttributePagersData.length < regularPagersData.length;
    var showLess = max && visibleAttributePagersData.length >= regularPagersData.length && max < visibleAttributePagersData.length;
    var decoratorProps = {
        enabled: drawLines
    };
    var onShowMore = function () {
        setVisibleValuesCount(Infinity);
    };
    var onShowLess = function () {
        setVisibleValuesCount(max);
    };
    return (React.createElement("div", { className: className },
        children && React.createElement(BranchDecorator, __assign({}, decoratorProps), children),
        visibleAttributePagersData.concat(fixedPagersData).map(function (_a) {
            var attrType = _a.attrType, values = _a.values;
            return (React.createElement(ReadOnlyAttributesPager, { key: attrType.uri, attributeType: attrType, drawLines: drawLines, values: values, paging: path(['paging', attrType.uri], entity.attributes), parentUri: parentUri, showNonOv: showNonOv, classes: classes, FallbackSlot: FallbackSlot }));
        }),
        showMore && React.createElement(ShowMore, { onClick: onShowMore }),
        showLess && React.createElement(ShowLess, { onClick: onShowLess })));
};
ReadOnlyAttributesList.displayName = 'ReadOnlyAttributesList';
export default memo(ReadOnlyAttributesList);
