var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import React, { useContext, useMemo, useState } from 'react';
import classnames from 'classnames';
import { useDidUpdateEffect } from '../hooks/useDidUpdateEffect';
import { RelatedObjectUrisContext } from '../contexts/RelatedObjectUrisContext';
import { CollaborationContext } from '../contexts/CollaborationContext';
import { CommentButton } from './components/CommentButton';
import { AddCommentButton } from './components/AddCommentButton';
import { CollaborationPopup } from './components/CollaborationPopup';
import { useStyles } from './styles';
export var CommentsContainer = function (_a) {
    var _b;
    var className = _a.className, uri = _a.uri, objectType = _a.objectType, relatedObjectUris = _a.relatedObjectUris, onChangePopupVisibility = _a.onChangePopupVisibility, _c = _a.allowOnlyOneComment, allowOnlyOneComment = _c === void 0 ? false : _c, _d = _a.showAlways, showAlways = _d === void 0 ? false : _d;
    var styles = useStyles();
    var _e = useState(null), anchorEl = _e[0], setAnchorEl = _e[1];
    var _f = useState(false), shouldOpenPopup = _f[0], setShouldOpenPopup = _f[1];
    var isOpenPopup = shouldOpenPopup && Boolean(anchorEl);
    var _g = useContext(CollaborationContext), currentComment = _g.currentComment, commentsMap = _g.commentsMap, getComment = _g.getComment, clearCurrentComment = _g.clearCurrentComment, objectTypes = _g.objectTypes;
    var showComments = !!commentsMap && (!objectTypes || objectTypes.includes(objectType));
    var openComments = ((commentsMap === null || commentsMap === void 0 ? void 0 : commentsMap[uri]) || []).filter(function (_a) {
        var status = _a.status;
        return status !== 'resolved';
    }).reverse();
    var shouldShowAddCommentButton = !allowOnlyOneComment || (allowOnlyOneComment && openComments.length === 0);
    var isOpenPopupWithNewComment = currentComment === null && isOpenPopup;
    var handleClickCommentButton = function (commentId) {
        getComment(commentId);
        setShouldOpenPopup(true);
    };
    var handleClickAddCommentButton = function (event) {
        setAnchorEl(event.currentTarget);
        setShouldOpenPopup(true);
    };
    var handleClose = function () {
        setAnchorEl(null);
        clearCurrentComment();
        setShouldOpenPopup(false);
    };
    useDidUpdateEffect(function () {
        if (onChangePopupVisibility) {
            onChangePopupVisibility(isOpenPopup);
        }
    }, [isOpenPopup]);
    var additionalRelatedObjectUris = useContext(RelatedObjectUrisContext);
    var allRelatedObjectUris = useMemo(function () {
        return additionalRelatedObjectUris ? __spreadArray(__spreadArray([], relatedObjectUris, true), additionalRelatedObjectUris, true) : relatedObjectUris;
    }, [additionalRelatedObjectUris, relatedObjectUris]);
    return (showComments && (React.createElement("div", { "data-reltio-id": "comments-container", className: classnames(styles.root, className) },
        shouldShowAddCommentButton && (React.createElement(AddCommentButton, { className: classnames(styles.addButton, (_b = {},
                _b[styles.showAlways] = showAlways || isOpenPopupWithNewComment,
                _b)), onClick: handleClickAddCommentButton })),
        openComments.map(function (_a) {
            var replies = _a.replies, commentId = _a.commentId;
            return (React.createElement(CommentButton, { ref: commentId === (currentComment === null || currentComment === void 0 ? void 0 : currentComment.commentId) ? setAnchorEl : null, key: commentId, onClick: function () { return handleClickCommentButton(commentId); } }, 1 + replies));
        }),
        React.createElement(CollaborationPopup, { anchorEl: anchorEl, open: isOpenPopup, onClose: handleClose, uri: uri, relatedObjectUris: allRelatedObjectUris, objectType: objectType, comment: currentComment }))));
};
