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, { useContext, useMemo } from 'react';
import { getAttributeErrorByTypeUri, getAttributePagerActiveTypeErrorMessage, getBaseUri, isAttributeTypeError, isImage, isOv, isSpecialAttribute, Mode, filterErrorsForHiddenAttributes } from '@reltio/mdm-sdk';
import { always, cond, T, partition } from 'ramda';
import { BranchDecorator } from '../BranchDecorator';
import { AttributeRenderer } from './components/AttributeRenderer';
import { SpecialRenderer } from './components/SpecialRenderer';
import { ImageLineRenderer } from './components/ImageLineRenderer';
import { withContext } from '../HOCs/withContext';
import { RENDERER_TYPES } from './types';
import { isHighlightedAttributeType, isHighlightedErrorType, ScrollToElementContext } from '../contexts/ScrollToElementContext';
import { useMdmAction, useMdmActiveErrorsForAttributesPager, useMdmMaxValuesInResponse, useMdmProfileErrors } from '../contexts/MdmModuleContext';
import { HiddenAttributesContext } from '../contexts/HiddenAttributesContext';
var EditModeAttributesPager = function (_a) {
    var _b;
    var values = _a.values, attributeType = _a.attributeType, _c = _a.mode, mode = _c === void 0 ? Mode.Viewing : _c, crosswalks = _a.crosswalks, drawLines = _a.drawLines, _d = _a.paging, paging = _d === void 0 ? {} : _d, parentUri = _a.parentUri, maxProp = _a.max, showEmptyEditors = _a.showEmptyEditors, showNonOv = _a.showNonOv, highlightedError = _a.highlightedError, highlightedAttribute = _a.highlightedAttribute, onAddAttributes = _a.onAddAttributes, onDeleteAttribute = _a.onDeleteAttribute, onChangeAttribute = _a.onChangeAttribute, additionalControlsRenderer = _a.additionalControlsRenderer;
    var onDeactivateError = useMdmAction('errorDeactivated');
    var requestNextPageOfAttributeValues = useMdmAction('requestNextPageOfAttributeValues');
    var hiddenAttributes = useContext(HiddenAttributesContext);
    var entityUri = parentUri && getBaseUri(parentUri);
    var currentEntityHiddenAttributes = useMemo(function () { return hiddenAttributes[entityUri] || []; }, [hiddenAttributes, entityUri]);
    var mdmActiveErrors = useMdmActiveErrorsForAttributesPager(parentUri, attributeType);
    var activeErrors = useMemo(function () { return filterErrorsForHiddenAttributes(mdmActiveErrors, currentEntityHiddenAttributes); }, [mdmActiveErrors, currentEntityHiddenAttributes]);
    var profileErrors = useMdmProfileErrors();
    var maxValuesInResponse = useMdmMaxValuesInResponse();
    var errorMessage = getAttributePagerActiveTypeErrorMessage(parentUri, attributeType, profileErrors);
    var max = (_b = maxProp !== null && maxProp !== void 0 ? maxProp : maxValuesInResponse) !== null && _b !== void 0 ? _b : Infinity;
    var partitionByOv = partition(isOv);
    var _e = partitionByOv(values), ovValues = _e[0], nonOvValues = _e[1];
    var visibleValues = showNonOv ? values : ovValues;
    var nonVisibleValues = showNonOv ? [] : nonOvValues;
    var commonProps = {
        errorMessage: errorMessage,
        errors: activeErrors,
        mode: mode,
        parentUri: parentUri,
        onAddAttributes: onAddAttributes,
        onDeleteAttribute: onDeleteAttribute,
        onChangeAttribute: onChangeAttribute,
        onDeactivateError: onDeactivateError,
        additionalControlsRenderer: additionalControlsRenderer,
        showEmptyEditors: showEmptyEditors,
        highlightedError: highlightedError,
        highlightedAttribute: highlightedAttribute
    };
    var renderAttribute = function () {
        var type = cond([
            [isSpecialAttribute, always(RENDERER_TYPES.special)],
            [isImage, always(RENDERER_TYPES.imageLine)],
            [T, always(RENDERER_TYPES.default)]
        ])(attributeType);
        switch (type) {
            case RENDERER_TYPES.special: {
                return (React.createElement(BranchDecorator, { enabled: drawLines },
                    React.createElement(SpecialRenderer, __assign({ values: visibleValues, attributeType: attributeType }, commonProps))));
            }
            case RENDERER_TYPES.imageLine:
                return (React.createElement(BranchDecorator, { enabled: drawLines },
                    React.createElement(ImageLineRenderer, { attributeValues: visibleValues, attributeType: attributeType, paging: paging, onDeleteAttribute: onDeleteAttribute, parentUri: parentUri, onChangeAttribute: onChangeAttribute, requestNextPageOfAttributeValues: requestNextPageOfAttributeValues, showNonOv: showNonOv, highlightedAttribute: highlightedAttribute, error: getAttributeErrorByTypeUri(attributeType.uri, activeErrors), highlightedError: highlightedError, onDeactivateError: onDeactivateError })));
            default:
                return (React.createElement(AttributeRenderer, __assign({ values: visibleValues, attributeType: attributeType, max: max, paging: paging, crosswalks: crosswalks, drawLines: drawLines, requestNextPageOfAttributeValues: requestNextPageOfAttributeValues, showNonOv: showNonOv, nonVisibleValues: nonVisibleValues }, commonProps)));
        }
    };
    return renderAttribute();
};
export default withContext(ScrollToElementContext, function (contextValue, _a) {
    var attributeType = _a.attributeType, parentUri = _a.parentUri;
    var _b = contextValue || {}, element = _b.element, type = _b.type;
    var isHighlightedAttribute = isHighlightedAttributeType(type) && element.uri === attributeType.uri;
    var isHighlightedError = isHighlightedErrorType(type) &&
        isAttributeTypeError(element, parentUri, attributeType.uri);
    return {
        highlightedAttribute: isHighlightedAttribute ? contextValue : null,
        highlightedError: isHighlightedError ? contextValue : null
    };
}, EditModeAttributesPager);
