"use strict";
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);
};
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ramda_1 = require("ramda");
var BranchDecorator_1 = require("../BranchDecorator");
var AttributeRenderer_1 = require("./components/AttributeRenderer");
var SpecialRenderer_1 = require("./components/SpecialRenderer");
var ImageLineRenderer_1 = require("./components/ImageLineRenderer");
var withContext_1 = require("../HOCs/withContext");
var types_1 = require("./types");
var ScrollToElementContext_1 = require("../contexts/ScrollToElementContext");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var HiddenAttributesContext_1 = require("../contexts/HiddenAttributesContext");
var EditModeAttributesPager = function (_a) {
    var _b;
    var values = _a.values, attributeType = _a.attributeType, _c = _a.mode, mode = _c === void 0 ? mdm_sdk_1.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 = (0, MdmModuleContext_1.useMdmAction)('errorDeactivated');
    var requestNextPageOfAttributeValues = (0, MdmModuleContext_1.useMdmAction)('requestNextPageOfAttributeValues');
    var hiddenAttributes = (0, react_1.useContext)(HiddenAttributesContext_1.HiddenAttributesContext);
    var entityUri = parentUri && (0, mdm_sdk_1.getBaseUri)(parentUri);
    var currentEntityHiddenAttributes = (0, react_1.useMemo)(function () { return hiddenAttributes[entityUri] || []; }, [hiddenAttributes, entityUri]);
    var mdmActiveErrors = (0, MdmModuleContext_1.useMdmActiveErrorsForAttributesPager)(parentUri, attributeType);
    var activeErrors = (0, react_1.useMemo)(function () { return (0, mdm_sdk_1.filterErrorsForHiddenAttributes)(mdmActiveErrors, currentEntityHiddenAttributes); }, [mdmActiveErrors, currentEntityHiddenAttributes]);
    var profileErrors = (0, MdmModuleContext_1.useMdmProfileErrors)();
    var maxValuesInResponse = (0, MdmModuleContext_1.useMdmMaxValuesInResponse)();
    var errorMessage = (0, mdm_sdk_1.getAttributePagerActiveTypeErrorMessage)(parentUri, attributeType, profileErrors);
    var max = (_b = maxProp !== null && maxProp !== void 0 ? maxProp : maxValuesInResponse) !== null && _b !== void 0 ? _b : Infinity;
    var partitionByOv = (0, ramda_1.partition)(mdm_sdk_1.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 = (0, ramda_1.cond)([
            [mdm_sdk_1.isSpecialAttribute, (0, ramda_1.always)(types_1.RENDERER_TYPES.special)],
            [mdm_sdk_1.isImage, (0, ramda_1.always)(types_1.RENDERER_TYPES.imageLine)],
            [ramda_1.T, (0, ramda_1.always)(types_1.RENDERER_TYPES.default)]
        ])(attributeType);
        switch (type) {
            case types_1.RENDERER_TYPES.special: {
                return (react_1.default.createElement(BranchDecorator_1.BranchDecorator, { enabled: drawLines },
                    react_1.default.createElement(SpecialRenderer_1.SpecialRenderer, __assign({ values: visibleValues, attributeType: attributeType }, commonProps))));
            }
            case types_1.RENDERER_TYPES.imageLine:
                return (react_1.default.createElement(BranchDecorator_1.BranchDecorator, { enabled: drawLines },
                    react_1.default.createElement(ImageLineRenderer_1.ImageLineRenderer, { attributeValues: visibleValues, attributeType: attributeType, paging: paging, onDeleteAttribute: onDeleteAttribute, parentUri: parentUri, onChangeAttribute: onChangeAttribute, requestNextPageOfAttributeValues: requestNextPageOfAttributeValues, showNonOv: showNonOv, highlightedAttribute: highlightedAttribute, error: (0, mdm_sdk_1.getAttributeErrorByTypeUri)(attributeType.uri, activeErrors), highlightedError: highlightedError, onDeactivateError: onDeactivateError })));
            default:
                return (react_1.default.createElement(AttributeRenderer_1.AttributeRenderer, __assign({ values: visibleValues, attributeType: attributeType, max: max, paging: paging, crosswalks: crosswalks, drawLines: drawLines, requestNextPageOfAttributeValues: requestNextPageOfAttributeValues, showNonOv: showNonOv, nonVisibleValues: nonVisibleValues }, commonProps)));
        }
    };
    return renderAttribute();
};
exports.default = (0, withContext_1.withContext)(ScrollToElementContext_1.ScrollToElementContext, function (contextValue, _a) {
    var attributeType = _a.attributeType, parentUri = _a.parentUri;
    var _b = contextValue || {}, element = _b.element, type = _b.type;
    var isHighlightedAttribute = (0, ScrollToElementContext_1.isHighlightedAttributeType)(type) && element.uri === attributeType.uri;
    var isHighlightedError = (0, ScrollToElementContext_1.isHighlightedErrorType)(type) &&
        (0, mdm_sdk_1.isAttributeTypeError)(element, parentUri, attributeType.uri);
    return {
        highlightedAttribute: isHighlightedAttribute ? contextValue : null,
        highlightedError: isHighlightedError ? contextValue : null
    };
}, EditModeAttributesPager);
