"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 __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
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 mdm_sdk_1 = require("@reltio/mdm-sdk");
var classnames_1 = __importDefault(require("classnames"));
var ramda_1 = require("ramda");
var Box_1 = __importDefault(require("@mui/material/Box"));
var AttributeTitle_1 = require("../../../AttributeTitle");
var CardinalityMessage_1 = require("../../../CardinalityMessage");
var ErrorMessage_1 = require("../../../ErrorMessage");
var EditModeAttributesFactory_1 = require("../../../EditModeAttributesFactory");
var RolesEditor_1 = require("../../../RolesEditor");
var TagsEditor_1 = require("../../../TagsEditor");
var HasDeletionsContext_1 = require("../../../contexts/HasDeletionsContext");
var withContext_1 = require("../../../HOCs/withContext");
var useScrollToAttribute_1 = require("../../../hooks/useScrollToAttribute");
var styles_1 = require("../../styles");
var SpecialRenderer = function (_a) {
    var values = _a.values, attributeType = _a.attributeType, parentUri = _a.parentUri, mode = _a.mode, errorMessage = _a.errorMessage, onDeleteAttribute = _a.onDeleteAttribute, onChangeAttribute = _a.onChangeAttribute, showEmptyEditors = _a.showEmptyEditors, hasDeletionsProp = _a.hasDeletions, setHasDeletions = _a.setHasDeletions, highlightedAttribute = _a.highlightedAttribute, _b = _a.showTitle, showTitle = _b === void 0 ? true : _b, otherProps = __rest(_a, ["values", "attributeType", "parentUri", "mode", "errorMessage", "onDeleteAttribute", "onChangeAttribute", "showEmptyEditors", "hasDeletions", "setHasDeletions", "highlightedAttribute", "showTitle"]);
    var styles = (0, styles_1.useStyles)();
    var label = attributeType.label, isRequired = attributeType.required, cardinality = attributeType.cardinality;
    var isEditableMode = (0, mdm_sdk_1.isEditableMode)(mode);
    var _c = (0, react_1.useState)(false), hasDeletionsState = _c[0], setHasDeletionsState = _c[1];
    var updateHasDeletions = (0, react_1.useCallback)(function (value) {
        setHasDeletionsState(value);
        setHasDeletions === null || setHasDeletions === void 0 ? void 0 : setHasDeletions(attributeType.uri, value);
    }, [attributeType.uri, setHasDeletions]);
    var hasDeletions = hasDeletionsProp !== undefined ? hasDeletionsProp : hasDeletionsState;
    // eslint-disable-next-line react-hooks/exhaustive-deps
    var onDelete = (0, react_1.useCallback)((0, ramda_1.pipe)(onDeleteAttribute, ramda_1.T, updateHasDeletions), [
        onDeleteAttribute,
        updateHasDeletions
    ]);
    var isEmptyValues = (0, mdm_sdk_1.isEmptyValue)(values);
    var showEmpty = !hasDeletions && showEmptyEditors && isEmptyValues;
    var emptyEditorValue = (0, react_1.useRef)((0, mdm_sdk_1.createNewAttribute)({
        parentUri: parentUri,
        attributeType: attributeType
    }));
    var _d = (0, useScrollToAttribute_1.useScrollToAttribute)(highlightedAttribute), ref = _d.ref, highlightedClassName = _d.highlightedClassName;
    var renderValue = function () {
        switch (attributeType.uri) {
            case mdm_sdk_1.EntityAttrTypes.tags.uri:
                return (react_1.default.createElement(TagsEditor_1.TagsEditor, __assign({}, otherProps, { values: values, onDelete: onDelete, onChange: onChangeAttribute })));
            case mdm_sdk_1.EntityAttrTypes.roles.uri:
                return (react_1.default.createElement(RolesEditor_1.RolesEditor, __assign({}, otherProps, { values: values, onDelete: onDelete, onChange: onChangeAttribute })));
            default: {
                //startDate, endDate
                var value = __assign(__assign({}, emptyEditorValue.current), { value: values[0] || '' });
                return (react_1.default.createElement(EditModeAttributesFactory_1.EditModeAttribute, __assign({}, otherProps, { attributeValue: value, attributeType: attributeType, mode: mode, onDeleteAttribute: onDelete, onChangeAttribute: onChangeAttribute })));
            }
        }
    };
    if ((0, mdm_sdk_1.isActivenessAttrType)(attributeType) && isEmptyValues && !showEmpty) {
        return null;
    }
    return (react_1.default.createElement("div", { ref: ref, className: (0, classnames_1.default)(highlightedClassName, styles.wrapper) },
        showTitle && (react_1.default.createElement(AttributeTitle_1.AttributeTitle, { label: label, isRequired: isRequired, className: (0, classnames_1.default)(styles.title, styles.titleWrapper), "data-reltio-id": "reltio-attribute-label" })),
        isEditableMode && react_1.default.createElement(CardinalityMessage_1.CardinalityMessage, { cardinality: cardinality }),
        isEditableMode && react_1.default.createElement(ErrorMessage_1.ErrorMessage, { message: errorMessage }),
        react_1.default.createElement(Box_1.default, { className: styles.attributesWrapper }, renderValue())));
};
exports.default = (0, withContext_1.withContext)(HasDeletionsContext_1.HasDeletionsContext, function (contextValue, _a) {
    var attributeType = _a.attributeType;
    var _b = contextValue || {}, hasDeletionsMap = _b.hasDeletionsMap, setHasDeletions = _b.setHasDeletions;
    return {
        hasDeletions: hasDeletionsMap ? Boolean(hasDeletionsMap[attributeType === null || attributeType === void 0 ? void 0 : attributeType.uri]) : undefined,
        setHasDeletions: setHasDeletions
    };
}, SpecialRenderer);
