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, { useCallback, useContext, useMemo, useState, useEffect } from 'react';
import i18n from 'ui-i18n';
import { getCreatableAttributeTypes, getAttributesListForEditMode, getParentUri, checkCanCreateAttribute, getBaseUri, Mode, isTempUri } from '@reltio/mdm-sdk';
import { last, path } from 'ramda';
import { EditModeAttributesPager } from '../EditModeAttributesPager';
import { BranchDecorator } from '../BranchDecorator';
import { MoreAttributesButton } from '../MoreAttributesButton';
import { FIXED_TYPE_URIS } from '../constants';
import { splitPagersData } from '../helpers/attributesView';
import { getMoreAttrTypes } from './helpers';
import { PinnedAttributesContext } from '../contexts/PinnedAttributesContext';
import { HasDeletionsContext } from '../contexts/HasDeletionsContext';
import { withContext } from '../HOCs/withContext';
import { ConfigPermissionsContext } from '../contexts/ConfigPermissionsContext';
import { ScrollType, ScrollToElementContext } from '../contexts/ScrollToElementContext';
import { HiddenAttributesContext } from '../contexts/HiddenAttributesContext';
import { AlwaysVisibleAttributesContext } from '../contexts/AlwaysVisibleAttributesContext';
var EditModeAttributesList = function (_a) {
    var parentAttributeType = _a.parentAttributeType, attrTypes = _a.attrTypes, entity = _a.entity, parentUri = _a.parentUri, showEmptyEditors = _a.showEmptyEditors, mode = _a.mode, crosswalks = _a.crosswalks, drawLines = _a.drawLines, children = _a.children, className = _a.className, _b = _a.fixedTypeUris, fixedTypeUris = _b === void 0 ? FIXED_TYPE_URIS : _b, onAddAttributes = _a.onAddAttributes, onChangeAttribute = _a.onChangeAttribute, onDeleteAttribute = _a.onDeleteAttribute, additionalControlsRenderer = _a.additionalControlsRenderer, showNonOv = _a.showNonOv, highlightAttribute = _a.highlightAttribute;
    var hiddenAttributes = useContext(HiddenAttributesContext);
    var entityUri = parentUri && getBaseUri(parentUri);
    var currentEntityHiddenAttributes = useMemo(function () { return hiddenAttributes[entityUri] || []; }, [hiddenAttributes, entityUri]);
    var alwaysVisibleAttributes = useContext(AlwaysVisibleAttributesContext);
    var filteredAttrTypes = useMemo(function () { return attrTypes.filter(function (attrType) { return !currentEntityHiddenAttributes.includes(attrType.uri); }); }, [attrTypes, currentEntityHiddenAttributes]);
    var _c = entity.attributes, attributes = _c === void 0 ? {} : _c;
    var _d = useState({}), hasDeletionsMap = _d[0], setHasDeletionsMap = _d[1];
    var setHasDeletions = useCallback(function (attrTypeUri, value) {
        setHasDeletionsMap(function (prevMap) {
            var _a;
            return (__assign(__assign({}, prevMap), (_a = {}, _a[attrTypeUri] = value, _a)));
        });
    }, []);
    useEffect(function () {
        setHasDeletionsMap({});
    }, [parentUri]);
    var canCreate = useContext(ConfigPermissionsContext).canCreate;
    var canCreateParentAttrType = useMemo(function () { return canCreate && parentAttributeType && checkCanCreateAttribute({ attributeType: parentAttributeType, mode: mode }); }, [parentAttributeType, mode, canCreate]);
    var creatableAttrTypes = useMemo(function () { return (canCreate ? getCreatableAttributeTypes(mode, filteredAttrTypes) : []); }, [filteredAttrTypes, mode, canCreate]);
    var moreAttrTypes = getMoreAttrTypes(creatableAttrTypes, entity, showEmptyEditors, hasDeletionsMap);
    var pinnedAttributes = useContext(PinnedAttributesContext);
    var pagersData = useMemo(function () {
        return getAttributesListForEditMode({
            attrTypes: filteredAttrTypes,
            mode: mode,
            entity: entity,
            showEmptyEditors: showEmptyEditors,
            showNonOv: showNonOv,
            pinnedAttributes: pinnedAttributes,
            configCanCreate: canCreate,
            alwaysVisible: alwaysVisibleAttributes,
            isTemporary: isTempUri(parentUri)
        });
    }, [
        filteredAttrTypes,
        mode,
        entity,
        showEmptyEditors,
        showNonOv,
        pinnedAttributes,
        canCreate,
        alwaysVisibleAttributes,
        parentUri
    ]);
    var _e = useMemo(function () { return splitPagersData(fixedTypeUris, pagersData); }, [pagersData, fixedTypeUris]), fixedPagersData = _e[0], regularPagersData = _e[1];
    var onAddMoreAttributes = useCallback(function (attributeTypes) {
        onAddAttributes(attributeTypes.map(function (attributeType) { return ({
            attributeType: attributeType,
            parentUri: attributeType === parentAttributeType ? getParentUri(parentUri) : parentUri
        }); }));
        highlightAttribute(last(attributeTypes), ScrollType.NewAttribute);
    }, [onAddAttributes, parentUri, parentAttributeType]);
    var decoratorProps = {
        enabled: drawLines
    };
    var hasDeletionsContextValue = useMemo(function () { return ({
        hasDeletionsMap: hasDeletionsMap,
        setHasDeletions: setHasDeletions
    }); }, [hasDeletionsMap, setHasDeletions]);
    return (React.createElement("div", { className: className },
        children && React.createElement(BranchDecorator, __assign({}, decoratorProps), children),
        React.createElement(BranchDecorator, __assign({}, decoratorProps, { plain: true }), moreAttrTypes.length > 0 && mode !== Mode.Viewing && (React.createElement(MoreAttributesButton, { label: i18n.text('More attributes'), onApply: onAddMoreAttributes, dense: drawLines, data: moreAttrTypes, parent: canCreateParentAttrType && parentAttributeType }))),
        React.createElement(HasDeletionsContext.Provider, { value: hasDeletionsContextValue }, regularPagersData.concat(fixedPagersData).map(function (_a) {
            var attrType = _a.attrType, values = _a.values;
            return (React.createElement(EditModeAttributesPager, { key: "".concat(attrType.uri, "_").concat(parentUri), attributeType: attrType, drawLines: drawLines, values: values, paging: path(['paging', attrType.uri], attributes), parentUri: parentUri, showEmptyEditors: showEmptyEditors, mode: mode, crosswalks: crosswalks, onAddAttributes: onAddAttributes, onChangeAttribute: onChangeAttribute, onDeleteAttribute: onDeleteAttribute, additionalControlsRenderer: additionalControlsRenderer, showNonOv: showNonOv }));
        }))));
};
export default withContext(ScrollToElementContext, function (contextValue) {
    var highlightAttribute = (contextValue || {}).highlightAttribute;
    return { highlightAttribute: highlightAttribute };
}, EditModeAttributesList);
