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 __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;
};
import React, { useCallback, useRef, useState } from 'react';
import { createNewAttribute, EntityAttrTypes, isEditableMode as checkIsEditableMode, isEmptyValue, isActivenessAttrType } from '@reltio/mdm-sdk';
import classnames from 'classnames';
import { pipe, T } from 'ramda';
import Box from '@mui/material/Box';
import { AttributeTitle } from '../../../AttributeTitle';
import { CardinalityMessage } from '../../../CardinalityMessage';
import { ErrorMessage } from '../../../ErrorMessage';
import { EditModeAttribute } from '../../../EditModeAttributesFactory';
import { RolesEditor } from '../../../RolesEditor';
import { TagsEditor } from '../../../TagsEditor';
import { HasDeletionsContext } from '../../../contexts/HasDeletionsContext';
import { withContext } from '../../../HOCs/withContext';
import { useScrollToAttribute } from '../../../hooks/useScrollToAttribute';
import { useStyles } from '../../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 = useStyles();
    var label = attributeType.label, isRequired = attributeType.required, cardinality = attributeType.cardinality;
    var isEditableMode = checkIsEditableMode(mode);
    var _c = useState(false), hasDeletionsState = _c[0], setHasDeletionsState = _c[1];
    var updateHasDeletions = 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 = useCallback(pipe(onDeleteAttribute, T, updateHasDeletions), [
        onDeleteAttribute,
        updateHasDeletions
    ]);
    var isEmptyValues = isEmptyValue(values);
    var showEmpty = !hasDeletions && showEmptyEditors && isEmptyValues;
    var emptyEditorValue = useRef(createNewAttribute({
        parentUri: parentUri,
        attributeType: attributeType
    }));
    var _d = useScrollToAttribute(highlightedAttribute), ref = _d.ref, highlightedClassName = _d.highlightedClassName;
    var renderValue = function () {
        switch (attributeType.uri) {
            case EntityAttrTypes.tags.uri:
                return (React.createElement(TagsEditor, __assign({}, otherProps, { values: values, onDelete: onDelete, onChange: onChangeAttribute })));
            case EntityAttrTypes.roles.uri:
                return (React.createElement(RolesEditor, __assign({}, otherProps, { values: values, onDelete: onDelete, onChange: onChangeAttribute })));
            default: {
                //startDate, endDate
                var value = __assign(__assign({}, emptyEditorValue.current), { value: values[0] || '' });
                return (React.createElement(EditModeAttribute, __assign({}, otherProps, { attributeValue: value, attributeType: attributeType, mode: mode, onDeleteAttribute: onDelete, onChangeAttribute: onChangeAttribute })));
            }
        }
    };
    if (isActivenessAttrType(attributeType) && isEmptyValues && !showEmpty) {
        return null;
    }
    return (React.createElement("div", { ref: ref, className: classnames(highlightedClassName, styles.wrapper) },
        showTitle && (React.createElement(AttributeTitle, { label: label, isRequired: isRequired, className: classnames(styles.title, styles.titleWrapper), "data-reltio-id": "reltio-attribute-label" })),
        isEditableMode && React.createElement(CardinalityMessage, { cardinality: cardinality }),
        isEditableMode && React.createElement(ErrorMessage, { message: errorMessage }),
        React.createElement(Box, { className: styles.attributesWrapper }, renderValue())));
};
export default withContext(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);
