"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AssignUsers = void 0;
var _react = _interopRequireWildcard(require("react"));
var _eui = require("@elastic/eui");
var _use_assignees = require("../../../containers/user_profiles/use_assignees");
var i18n = _interopRequireWildcard(require("../translations"));
var _sidebar_title = require("./sidebar_title");
var _removable_user = require("../../user_profiles/removable_user");
var _use_cases_context = require("../../cases_context/use_cases_context");
var _suggest_users_popover = require("./suggest_users_popover");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0; you may not use this file except in compliance with the Elastic License
 * 2.0.
 */

const AssigneesList = ({
  assignees,
  currentUserProfile,
  permissions,
  assignSelf,
  togglePopOver,
  onAssigneeRemoved
}) => {
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, assignees.length === 0 ? /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    direction: "column",
    gutterSize: "none"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: false
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiText, {
    size: "s",
    color: "subdued"
  }, /*#__PURE__*/_react.default.createElement("p", null, i18n.NO_ASSIGNEES, permissions.update && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("br", null), /*#__PURE__*/_react.default.createElement(_eui.EuiLink, {
    "data-test-subj": "case-view-assign-users-link",
    onClick: togglePopOver
  }, i18n.ASSIGN_A_USER)), currentUserProfile && permissions.update && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("span", null, i18n.SPACED_OR), /*#__PURE__*/_react.default.createElement(_eui.EuiLink, {
    "data-test-subj": "case-view-assign-yourself-link",
    onClick: assignSelf
  }, i18n.ASSIGN_YOURSELF)))))) : /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    direction: "column",
    gutterSize: "s"
  }, assignees.map(assignee => /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    key: assignee.uid,
    grow: false
  }, /*#__PURE__*/_react.default.createElement(_removable_user.RemovableUser, {
    assignee: assignee,
    onRemoveAssignee: onAssigneeRemoved
  })))));
};
AssigneesList.displayName = 'AssigneesList';
const AssignUsersComponent = ({
  caseAssignees,
  userProfiles,
  currentUserProfile,
  onAssigneesChanged,
  isLoading
}) => {
  const {
    assigneesWithProfiles,
    assigneesWithoutProfiles,
    allAssignees
  } = (0, _use_assignees.useAssignees)({
    caseAssignees,
    userProfiles
  });
  const [selectedAssignees, setSelectedAssignees] = (0, _react.useState)();
  const [needToUpdateAssignees, setNeedToUpdateAssignees] = (0, _react.useState)(false);
  const [isPopoverOpen, setIsPopoverOpen] = (0, _react.useState)(false);
  const togglePopover = (0, _react.useCallback)(() => {
    setIsPopoverOpen(value => !value);
    setNeedToUpdateAssignees(true);
  }, []);
  const onClosePopover = (0, _react.useCallback)(() => {
    // Order matters here because needToUpdateAssignees will likely be true already
    // from the togglePopover call when opening the popover, so if we set the popover to false
    // first, we'll get a rerender and then get another after we set needToUpdateAssignees to true again
    setNeedToUpdateAssignees(true);
    setIsPopoverOpen(false);
  }, []);
  const onAssigneeRemoved = (0, _react.useCallback)(removedAssigneeUID => {
    const remainingAssignees = allAssignees.filter(assignee => assignee.uid !== removedAssigneeUID);
    setSelectedAssignees(remainingAssignees);
    setNeedToUpdateAssignees(true);
  }, [allAssignees]);
  const onUsersChange = (0, _react.useCallback)(users => {
    // if users are selected then also include the users without profiles
    if (users.length > 0) {
      setSelectedAssignees([...users, ...assigneesWithoutProfiles]);
    } else {
      // all users were deselected so lets remove the users without profiles as well
      setSelectedAssignees([]);
    }
  }, [assigneesWithoutProfiles]);
  const assignSelf = (0, _react.useCallback)(() => {
    if (!currentUserProfile) {
      return;
    }
    const newAssignees = [currentUserProfile, ...allAssignees];
    setSelectedAssignees(newAssignees);
    setNeedToUpdateAssignees(true);
  }, [currentUserProfile, allAssignees]);
  const {
    permissions
  } = (0, _use_cases_context.useCasesContext)();
  (0, _react.useEffect)(() => {
    // selectedAssignees will be undefined on initial render or a rerender occurs, so we only want to update the assignees
    // after the users have been changed in some manner not when it is an initial value
    if (isPopoverOpen === false && needToUpdateAssignees && selectedAssignees) {
      setNeedToUpdateAssignees(false);
      onAssigneesChanged(selectedAssignees);
    }
  }, [isPopoverOpen, needToUpdateAssignees, onAssigneesChanged, selectedAssignees]);
  return /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: false,
    "data-test-subj": "case-view-assignees"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    alignItems: "center",
    gutterSize: "xs",
    justifyContent: "spaceBetween",
    responsive: false
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: false
  }, /*#__PURE__*/_react.default.createElement(_sidebar_title.SidebarTitle, {
    title: i18n.ASSIGNEES
  })), isLoading && /*#__PURE__*/_react.default.createElement(_eui.EuiLoadingSpinner, {
    "data-test-subj": "case-view-assignees-button-loading"
  }), !isLoading && permissions.update && /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    "data-test-subj": "case-view-assignees-edit",
    grow: false
  }, /*#__PURE__*/_react.default.createElement(_suggest_users_popover.SuggestUsersPopover, {
    assignedUsersWithProfiles: assigneesWithProfiles,
    currentUserProfile: currentUserProfile,
    isLoading: isLoading,
    isPopoverOpen: isPopoverOpen,
    onUsersChange: onUsersChange,
    onClosePopover: onClosePopover,
    togglePopover: togglePopover
  }))), /*#__PURE__*/_react.default.createElement(_eui.EuiHorizontalRule, {
    margin: "xs"
  }), /*#__PURE__*/_react.default.createElement(AssigneesList, {
    assignees: allAssignees,
    currentUserProfile: currentUserProfile,
    permissions: permissions,
    assignSelf: assignSelf,
    togglePopOver: togglePopover,
    onAssigneeRemoved: onAssigneeRemoved
  }));
};
AssignUsersComponent.displayName = 'AssignUsers';
const AssignUsers = /*#__PURE__*/_react.default.memo(AssignUsersComponent);
exports.AssignUsers = AssignUsers;