"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ramda_1 = require("ramda");
var EditModeAttributesPager_1 = require("../EditModeAttributesPager");
var BranchDecorator_1 = require("../BranchDecorator");
var MoreAttributesButton_1 = require("../MoreAttributesButton");
var constants_1 = require("../constants");
var attributesView_1 = require("../helpers/attributesView");
var helpers_1 = require("./helpers");
var PinnedAttributesContext_1 = require("../contexts/PinnedAttributesContext");
var HasDeletionsContext_1 = require("../contexts/HasDeletionsContext");
var withContext_1 = require("../HOCs/withContext");
var ConfigPermissionsContext_1 = require("../contexts/ConfigPermissionsContext");
var ScrollToElementContext_1 = require("../contexts/ScrollToElementContext");
var HiddenAttributesContext_1 = require("../contexts/HiddenAttributesContext");
var AlwaysVisibleAttributesContext_1 = require("../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 ? constants_1.FIXED_TYPE_URIS : _b, onAddAttributes = _a.onAddAttributes, onChangeAttribute = _a.onChangeAttribute, onDeleteAttribute = _a.onDeleteAttribute, additionalControlsRenderer = _a.additionalControlsRenderer, showNonOv = _a.showNonOv, highlightAttribute = _a.highlightAttribute;
    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 alwaysVisibleAttributes = (0, react_1.useContext)(AlwaysVisibleAttributesContext_1.AlwaysVisibleAttributesContext);
    var filteredAttrTypes = (0, react_1.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 = (0, react_1.useState)({}), hasDeletionsMap = _d[0], setHasDeletionsMap = _d[1];
    var setHasDeletions = (0, react_1.useCallback)(function (attrTypeUri, value) {
        setHasDeletionsMap(function (prevMap) {
            var _a;
            return (__assign(__assign({}, prevMap), (_a = {}, _a[attrTypeUri] = value, _a)));
        });
    }, []);
    (0, react_1.useEffect)(function () {
        setHasDeletionsMap({});
    }, [parentUri]);
    var canCreate = (0, react_1.useContext)(ConfigPermissionsContext_1.ConfigPermissionsContext).canCreate;
    var canCreateParentAttrType = (0, react_1.useMemo)(function () { return canCreate && parentAttributeType && (0, mdm_sdk_1.checkCanCreateAttribute)({ attributeType: parentAttributeType, mode: mode }); }, [parentAttributeType, mode, canCreate]);
    var creatableAttrTypes = (0, react_1.useMemo)(function () { return (canCreate ? (0, mdm_sdk_1.getCreatableAttributeTypes)(mode, filteredAttrTypes) : []); }, [filteredAttrTypes, mode, canCreate]);
    var moreAttrTypes = (0, helpers_1.getMoreAttrTypes)(creatableAttrTypes, entity, showEmptyEditors, hasDeletionsMap);
    var pinnedAttributes = (0, react_1.useContext)(PinnedAttributesContext_1.PinnedAttributesContext);
    var pagersData = (0, react_1.useMemo)(function () {
        return (0, mdm_sdk_1.getAttributesListForEditMode)({
            attrTypes: filteredAttrTypes,
            mode: mode,
            entity: entity,
            showEmptyEditors: showEmptyEditors,
            showNonOv: showNonOv,
            pinnedAttributes: pinnedAttributes,
            configCanCreate: canCreate,
            alwaysVisible: alwaysVisibleAttributes,
            isTemporary: (0, mdm_sdk_1.isTempUri)(parentUri)
        });
    }, [
        filteredAttrTypes,
        mode,
        entity,
        showEmptyEditors,
        showNonOv,
        pinnedAttributes,
        canCreate,
        alwaysVisibleAttributes,
        parentUri
    ]);
    var _e = (0, react_1.useMemo)(function () { return (0, attributesView_1.splitPagersData)(fixedTypeUris, pagersData); }, [pagersData, fixedTypeUris]), fixedPagersData = _e[0], regularPagersData = _e[1];
    var onAddMoreAttributes = (0, react_1.useCallback)(function (attributeTypes) {
        onAddAttributes(attributeTypes.map(function (attributeType) { return ({
            attributeType: attributeType,
            parentUri: attributeType === parentAttributeType ? (0, mdm_sdk_1.getParentUri)(parentUri) : parentUri
        }); }));
        highlightAttribute((0, ramda_1.last)(attributeTypes), ScrollToElementContext_1.ScrollType.NewAttribute);
    }, [onAddAttributes, parentUri, parentAttributeType]);
    var decoratorProps = {
        enabled: drawLines
    };
    var hasDeletionsContextValue = (0, react_1.useMemo)(function () { return ({
        hasDeletionsMap: hasDeletionsMap,
        setHasDeletions: setHasDeletions
    }); }, [hasDeletionsMap, setHasDeletions]);
    return (react_1.default.createElement("div", { className: className },
        children && react_1.default.createElement(BranchDecorator_1.BranchDecorator, __assign({}, decoratorProps), children),
        react_1.default.createElement(BranchDecorator_1.BranchDecorator, __assign({}, decoratorProps, { plain: true }), moreAttrTypes.length > 0 && mode !== mdm_sdk_1.Mode.Viewing && (react_1.default.createElement(MoreAttributesButton_1.MoreAttributesButton, { label: ui_i18n_1.default.text('More attributes'), onApply: onAddMoreAttributes, dense: drawLines, data: moreAttrTypes, parent: canCreateParentAttrType && parentAttributeType }))),
        react_1.default.createElement(HasDeletionsContext_1.HasDeletionsContext.Provider, { value: hasDeletionsContextValue }, regularPagersData.concat(fixedPagersData).map(function (_a) {
            var attrType = _a.attrType, values = _a.values;
            return (react_1.default.createElement(EditModeAttributesPager_1.EditModeAttributesPager, { key: "".concat(attrType.uri, "_").concat(parentUri), attributeType: attrType, drawLines: drawLines, values: values, paging: (0, ramda_1.path)(['paging', attrType.uri], attributes), parentUri: parentUri, showEmptyEditors: showEmptyEditors, mode: mode, crosswalks: crosswalks, onAddAttributes: onAddAttributes, onChangeAttribute: onChangeAttribute, onDeleteAttribute: onDeleteAttribute, additionalControlsRenderer: additionalControlsRenderer, showNonOv: showNonOv }));
        }))));
};
exports.default = (0, withContext_1.withContext)(ScrollToElementContext_1.ScrollToElementContext, function (contextValue) {
    var highlightAttribute = (contextValue || {}).highlightAttribute;
    return { highlightAttribute: highlightAttribute };
}, EditModeAttributesList);
