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 classnames from 'classnames';
import React, { useContext, memo, useMemo } from 'react';
import { always, cond, has, ifElse, partition, prop, T } from 'ramda';
import { isComplexAttribute, isImage, isSpecialAttribute, isOv } from '@reltio/mdm-sdk';
import { BranchDecorator } from '../BranchDecorator';
import { OneLineRenderer } from './components/OneLineRenderer';
import { MultiLineRenderer } from './components/MultiLineRenderer';
import { SpecialRenderer } from './components/SpecialRenderer';
import { ImageLineRenderer } from './components/ImageLineRenderer';
import { EmptyRenderer } from './components/EmptyRenderer';
import { useMdmAction, useMdmMaxValuesInResponse } from '../contexts/MdmModuleContext';
import { AlwaysVisibleAttributesContext } from '../contexts/AlwaysVisibleAttributesContext';
import { useStyles } from './styles';
var RENDERER_TYPES = {
    oneLine: 'oneLine',
    multiLine: 'multiLine',
    imageLine: 'imageLine',
    special: 'special',
    empty: 'empty'
};
var ReadOnlyAttributesPager = function (_a) {
    var _b, _c, _d, _e;
    var _f;
    var values = _a.values, attributeType = _a.attributeType, drawLines = _a.drawLines, _g = _a.paging, paging = _g === void 0 ? {} : _g, parentUri = _a.parentUri, maxProp = _a.max, showNonOv = _a.showNonOv, classes = _a.classes, FallbackSlot = _a.FallbackSlot;
    var styles = useStyles();
    var requestNextPageOfAttributeValues = useMdmAction('requestNextPageOfAttributeValues');
    var maxValuesInResponse = useMdmMaxValuesInResponse();
    var max = (_f = maxProp !== null && maxProp !== void 0 ? maxProp : maxValuesInResponse) !== null && _f !== void 0 ? _f : Infinity;
    var partitionByOv = partition(isOv);
    var alwaysVisibleAttributes = useContext(AlwaysVisibleAttributesContext);
    var isAlwaysVisible = useMemo(function () { return alwaysVisibleAttributes === null || alwaysVisibleAttributes === void 0 ? void 0 : alwaysVisibleAttributes.includes(attributeType.uri); }, [attributeType.uri, alwaysVisibleAttributes]);
    if (!values || (!values.length && !isAlwaysVisible)) {
        return null;
    }
    var _h = partitionByOv(values), ovValues = _h[0], nonOvValues = _h[1];
    var visibleValues = showNonOv ? values : ovValues;
    var nonVisibleValues = showNonOv ? [] : nonOvValues;
    var totalProp = showNonOv ? 'totalValues' : 'totalOvValues';
    var totalVisibleValues = ifElse(has(totalProp), prop(totalProp), always(visibleValues.length))(paging);
    var totalNonVisibleValues = (paging === null || paging === void 0 ? void 0 : paging.totalValues)
        ? paging.totalValues - totalVisibleValues
        : nonVisibleValues.length;
    var isOneLineRender = totalVisibleValues === 1 && !isComplexAttribute(attributeType);
    var isEmptyRender = values.length === 0 && isAlwaysVisible;
    var render = function () {
        var type = cond([
            [isSpecialAttribute, always(RENDERER_TYPES.special)],
            [isImage, always(RENDERER_TYPES.imageLine)],
            [always(isOneLineRender), always(RENDERER_TYPES.oneLine)],
            [always(isEmptyRender), always(RENDERER_TYPES.empty)],
            [T, always(RENDERER_TYPES.multiLine)]
        ])(attributeType);
        switch (type) {
            case RENDERER_TYPES.oneLine:
                return (React.createElement(OneLineRenderer, { value: visibleValues[0], nonVisibleValues: nonVisibleValues, totalNonVisibleValues: totalNonVisibleValues, attributeType: attributeType, showNonOv: showNonOv, classes: classes, FallbackSlot: FallbackSlot }));
            case RENDERER_TYPES.multiLine:
                return (React.createElement(MultiLineRenderer, { values: visibleValues, nonVisibleValues: nonVisibleValues, totalVisibleValues: totalVisibleValues, totalNonVisibleValues: totalNonVisibleValues, attributeType: attributeType, parentUri: parentUri, max: max, requestNextPageOfAttributeValues: requestNextPageOfAttributeValues, showNonOv: showNonOv, classes: classes, FallbackSlot: FallbackSlot }));
            case RENDERER_TYPES.imageLine:
                return (React.createElement(ImageLineRenderer, { attributeValues: visibleValues, attributeType: attributeType, paging: paging, parentUri: parentUri, requestNextPageOfAttributeValues: requestNextPageOfAttributeValues, showNonOv: showNonOv }));
            case RENDERER_TYPES.special:
                return React.createElement(SpecialRenderer, { values: visibleValues, attributeType: attributeType, classes: classes });
            case RENDERER_TYPES.empty:
                return React.createElement(EmptyRenderer, { attributeType: attributeType, classes: classes });
        }
    };
    return (React.createElement(BranchDecorator, { enabled: drawLines, className: classnames(__assign((_b = {}, _b[styles.paddingWrapperWithLines] = drawLines, _b), ((classes === null || classes === void 0 ? void 0 : classes.wrapperWithLines) ? (_c = {}, _c[classes === null || classes === void 0 ? void 0 : classes.wrapperWithLines] = drawLines, _c) : {}))) },
        React.createElement("div", { className: classnames(__assign((_d = {}, _d[styles.paddingWrapper] = !drawLines, _d), ((classes === null || classes === void 0 ? void 0 : classes.wrapper) ? (_e = {}, _e[classes === null || classes === void 0 ? void 0 : classes.wrapper] = !drawLines, _e) : {}))) }, render())));
};
export default memo(ReadOnlyAttributesPager);
