"use strict";
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 __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));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommentsContainer = void 0;
var react_1 = __importStar(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var useDidUpdateEffect_1 = require("../hooks/useDidUpdateEffect");
var RelatedObjectUrisContext_1 = require("../contexts/RelatedObjectUrisContext");
var CollaborationContext_1 = require("../contexts/CollaborationContext");
var CommentButton_1 = require("./components/CommentButton");
var AddCommentButton_1 = require("./components/AddCommentButton");
var CollaborationPopup_1 = require("./components/CollaborationPopup");
var styles_1 = require("./styles");
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 = (0, styles_1.useStyles)();
    var _e = (0, react_1.useState)(null), anchorEl = _e[0], setAnchorEl = _e[1];
    var _f = (0, react_1.useState)(false), shouldOpenPopup = _f[0], setShouldOpenPopup = _f[1];
    var isOpenPopup = shouldOpenPopup && Boolean(anchorEl);
    var _g = (0, react_1.useContext)(CollaborationContext_1.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);
    };
    (0, useDidUpdateEffect_1.useDidUpdateEffect)(function () {
        if (onChangePopupVisibility) {
            onChangePopupVisibility(isOpenPopup);
        }
    }, [isOpenPopup]);
    var additionalRelatedObjectUris = (0, react_1.useContext)(RelatedObjectUrisContext_1.RelatedObjectUrisContext);
    var allRelatedObjectUris = (0, react_1.useMemo)(function () {
        return additionalRelatedObjectUris ? __spreadArray(__spreadArray([], relatedObjectUris, true), additionalRelatedObjectUris, true) : relatedObjectUris;
    }, [additionalRelatedObjectUris, relatedObjectUris]);
    return (showComments && (react_1.default.createElement("div", { "data-reltio-id": "comments-container", className: (0, classnames_1.default)(styles.root, className) },
        shouldShowAddCommentButton && (react_1.default.createElement(AddCommentButton_1.AddCommentButton, { className: (0, classnames_1.default)(styles.addButton, (_b = {},
                _b[styles.showAlways] = showAlways || isOpenPopupWithNewComment,
                _b)), onClick: handleClickAddCommentButton })),
        openComments.map(function (_a) {
            var replies = _a.replies, commentId = _a.commentId;
            return (react_1.default.createElement(CommentButton_1.CommentButton, { ref: commentId === (currentComment === null || currentComment === void 0 ? void 0 : currentComment.commentId) ? setAnchorEl : null, key: commentId, onClick: function () { return handleClickCommentButton(commentId); } }, 1 + replies));
        }),
        react_1.default.createElement(CollaborationPopup_1.CollaborationPopup, { anchorEl: anchorEl, open: isOpenPopup, onClose: handleClose, uri: uri, relatedObjectUris: allRelatedObjectUris, objectType: objectType, comment: currentComment }))));
};
exports.CommentsContainer = CommentsContainer;
