function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
/**
 * @file Renders a single feed item row for ActivityFeedV2
 * @author Box
 */

import * as React from 'react';
import noop from 'lodash/noop';
import { ActivityFeed } from '@box/activity-feed';
import { dispatchReplyDelete, dispatchReplyEdit, logEditError, serializeEditorContent } from './helpers';
import { annotationTargetToBadge } from './transformers';
import { FEED_ITEM_TYPE_ANNOTATION, FEED_ITEM_TYPE_COMMENT, TASK_NEW_APPROVED, TASK_NEW_COMPLETED, TASK_NEW_NOT_STARTED, TASK_NEW_REJECTED } from '../../../constants';
const buildReplyPost = (parentId, parentType, isDisabled, onReplyCreate) => async content => {
  if (isDisabled || !onReplyCreate) return;
  const serialized = serializeEditorContent(content);
  if (!serialized || !serialized.text.trim()) return;
  onReplyCreate(parentId, parentType, serialized.text);
};
const FeedItemRow = ({
  currentUserId,
  isDisabled,
  item,
  onAnnotationCopyLink,
  onAnnotationDelete,
  onAnnotationEdit,
  onAnnotationSelect,
  onAnnotationStatusChange,
  onCommentCopyLink,
  onCommentDelete,
  onCommentUpdate,
  onReplyCreate,
  onReplyDelete,
  onReplyUpdate,
  onTaskAssignmentUpdate,
  onTaskDelete,
  onTaskEdit,
  onTaskView,
  onVersionHistoryClick,
  userSelectorProps
}) => {
  switch (item.type) {
    case 'comment':
      {
        const {
          permissions
        } = item;
        const handleDelete = id => {
          if (isDisabled) return;
          if (id === item.id) {
            onCommentDelete?.({
              id,
              permissions
            });
            return;
          }
          dispatchReplyDelete({
            id,
            messages: item.messages,
            onReplyDelete,
            parentId: item.id
          });
        };
        const handleStatusChange = status => id => {
          if (isDisabled) return;
          onCommentUpdate?.(id, undefined, status, false, permissions);
        };
        const handleEdit = (id, content) => {
          if (isDisabled) return;
          const serialized = serializeEditorContent(content);
          if (!serialized || !serialized.text.trim()) return;
          if (id === item.id) {
            onCommentUpdate?.(id, serialized.text, undefined, serialized.hasMention, permissions);
            return;
          }
          dispatchReplyEdit({
            id,
            messages: item.messages,
            onReplyUpdate,
            parentId: item.id,
            text: serialized.text
          });
        };
        return /*#__PURE__*/React.createElement(ActivityFeed.List.ThreadedAnnotation, {
          key: item.id,
          isAnnotations: false,
          isEditDisabled: isDisabled || item.isResolved,
          isResolved: item.isResolved,
          messages: item.messages,
          onAvatarClick: noop,
          onCopyLink: onCommentCopyLink ? id => onCommentCopyLink({
            id
          }) : undefined,
          onDelete: handleDelete,
          onEdit: handleEdit,
          onEditError: logEditError,
          onPost: buildReplyPost(item.id, FEED_ITEM_TYPE_COMMENT, isDisabled, onReplyCreate),
          onResolve: handleStatusChange('resolved'),
          onThreadDelete: () => handleDelete(item.id),
          onUnresolve: handleStatusChange('open'),
          resolvedAt: item.resolvedAt,
          resolvedBy: item.resolvedBy,
          userSelectorProps: userSelectorProps
        });
      }
    case 'annotation':
      {
        const {
          permissions
        } = item.annotation;
        const fileVersionId = item.annotation.file_version?.id;
        const handleDelete = id => {
          if (isDisabled) return;
          if (id === item.id) {
            onAnnotationDelete?.({
              id,
              permissions
            });
            return;
          }
          dispatchReplyDelete({
            id,
            messages: item.messages,
            onReplyDelete,
            parentId: item.id
          });
        };
        const handleStatusChange = status => id => {
          if (isDisabled) return;
          onAnnotationStatusChange?.({
            id,
            permissions,
            status
          });
        };
        const handleEdit = (id, content) => {
          if (isDisabled) return;
          const serialized = serializeEditorContent(content);
          if (!serialized || !serialized.text.trim()) return;
          if (id === item.id) {
            onAnnotationEdit?.({
              id,
              permissions,
              text: serialized.text
            });
            return;
          }
          dispatchReplyEdit({
            id,
            messages: item.messages,
            onReplyUpdate,
            parentId: item.id,
            text: serialized.text
          });
        };
        return /*#__PURE__*/React.createElement(ActivityFeed.List.ThreadedAnnotation, {
          key: item.id,
          annotationTarget: annotationTargetToBadge(item.annotation.target),
          isAnnotations: false,
          isEditDisabled: isDisabled || item.isResolved,
          isResolved: item.isResolved,
          messages: item.messages,
          onAnnotationBadgeClick: () => onAnnotationSelect?.(item.annotation),
          onAvatarClick: noop,
          onCopyLink: onAnnotationCopyLink && fileVersionId ? () => onAnnotationCopyLink({
            annotationId: item.id,
            fileVersionId
          }) : undefined,
          onDelete: handleDelete,
          onEdit: handleEdit,
          onEditError: logEditError,
          onPost: buildReplyPost(item.id, FEED_ITEM_TYPE_ANNOTATION, isDisabled, onReplyCreate),
          onResolve: handleStatusChange('resolved'),
          onThreadDelete: () => handleDelete(item.id),
          onUnresolve: handleStatusChange('open'),
          resolvedAt: item.resolvedAt,
          resolvedBy: item.resolvedBy,
          userSelectorProps: userSelectorProps
        });
      }
    case 'task':
      {
        const currentUserAssignment = currentUserId ? item.originalTask.assigned_to?.entries?.find(entry => entry.target?.id === currentUserId) : undefined;
        const canActOnAssignment = !!onTaskAssignmentUpdate && !!currentUserAssignment && currentUserAssignment.permissions?.can_update === true && currentUserAssignment.status === TASK_NEW_NOT_STARTED;
        const handleAssignmentUpdate = status => {
          if (isDisabled || !canActOnAssignment) return;
          onTaskAssignmentUpdate?.(item.id, currentUserAssignment.id, status);
        };
        return /*#__PURE__*/React.createElement(ActivityFeed.List.Task, _extends({
          key: item.id
        }, item.props, {
          disabled: isDisabled,
          onApprove: canActOnAssignment ? () => handleAssignmentUpdate(TASK_NEW_APPROVED) : undefined,
          onComplete: canActOnAssignment ? () => handleAssignmentUpdate(TASK_NEW_COMPLETED) : undefined,
          onDelete: onTaskDelete ? () => onTaskDelete(item.originalTask) : undefined,
          onEdit: onTaskEdit ? () => onTaskEdit(item.originalTask) : undefined,
          onReject: canActOnAssignment ? () => handleAssignmentUpdate(TASK_NEW_REJECTED) : undefined,
          onView: onTaskView ? taskId => onTaskView(taskId, currentUserId === item.props.author.id) : undefined
        }));
      }
    case 'version':
      return /*#__PURE__*/React.createElement(ActivityFeed.List.Version, _extends({
        key: item.id
      }, item.props, {
        onVersionClick: onVersionHistoryClick ? ({
          id,
          versionNumber
        }) => onVersionHistoryClick({
          id,
          version_number: versionNumber
        }) : undefined
      }));
    case 'app_activity':
      return /*#__PURE__*/React.createElement(ActivityFeed.List.AppActivity, _extends({
        key: item.id
      }, item.props));
    default:
      return null;
  }
};
export default FeedItemRow;
//# sourceMappingURL=FeedItemRow.js.map