"use strict";

exports.__esModule = true;
exports.default = ActivitiesMenu;

var React = _interopRequireWildcard(require("react"));

var _Box = _interopRequireDefault(require("@mui/material/Box"));

var _Menu = _interopRequireDefault(require("@mui/material/Menu"));

var _Tooltip = _interopRequireDefault(require("@mui/material/Tooltip"));

var _material = require("@mui/material");

var _Icon = _interopRequireDefault(require("@mui/material/Icon"));

var _styles = require("@mui/material/styles");

var _feedObject = require("../../../../types/feedObject");

var _reactIntl = require("react-intl");

var _core = require("@selfcommunity/core");

var _classnames = _interopRequireDefault(require("classnames"));

var _useThemeProps = _interopRequireDefault(require("@mui/material/styles/useThemeProps"));

const _excluded = ["className", "selectedActivities", "hideRelevantActivitiesItem", "onChange"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

const messages = (0, _reactIntl.defineMessages)({
  relevantActivities: {
    id: 'ui.feedObject.activitiesMenu.relevantActivities',
    defaultMessage: 'ui.feedObject.activitiesMenu.relevantActivities'
  },
  recentComments: {
    id: 'ui.feedObject.activitiesMenu.recentComments',
    defaultMessage: 'ui.feedObject.activitiesMenu.recentComments'
  },
  firstComments: {
    id: 'ui.feedObject.activitiesMenu.firstComments',
    defaultMessage: 'ui.feedObject.activitiesMenu.firstComments'
  },
  followedComments: {
    id: 'ui.feedObject.activitiesMenu.followedComments',
    defaultMessage: 'ui.feedObject.activitiesMenu.followedComments'
  },
  connectionsComments: {
    id: 'ui.feedObject.activitiesMenu.connectionsComments',
    defaultMessage: 'ui.feedObject.activitiesMenu.connectionsComments'
  }
});
const PREFIX = 'SCActivitiesMenu';
const classes = {
  root: `${PREFIX}-root`,
  selector: `${PREFIX}-selector`
};
const Root = (0, _styles.styled)(_Box.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  [`& .${classes.selector}`]: {
    display: 'flex',
    alignItems: 'center',
    textAlign: 'center',
    justifyContent: 'flex-end'
  },
  [`& .${classes.selector} .MuiButton-root`]: {
    textTransform: 'capitalize',
    fontWeight: 'bold'
  }
}));

function ActivitiesMenu(inProps) {
  // PROPS
  const props = (0, _useThemeProps.default)({
    props: inProps,
    name: PREFIX
  });

  const {
    className = null,
    selectedActivities = null,
    hideRelevantActivitiesItem = false,
    onChange = null
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // CONTEXT


  const scPreferencesContext = (0, React.useContext)(_core.SCPreferencesContext);
  const scUserContext = (0, _core.useSCUser)(); // STATE

  const followEnabled = scPreferencesContext.preferences[_core.SCPreferences.CONFIGURATIONS_FOLLOW_ENABLED].value;
  const [anchorEl, setAnchorEl] = React.useState(null);
  const open = Boolean(anchorEl); // INTL

  const intl = (0, _reactIntl.useIntl)(); // HANDLERS

  const handleClick = event => {
    setAnchorEl(event.currentTarget);
  };

  const handleClose = () => {
    setAnchorEl(null);
  };

  const handleChangeActivitiesType = type => {
    return () => {
      onChange && onChange(type);
    };
  };
  /**
   * Renders root object
   */


  return /*#__PURE__*/React.createElement(Root, _extends({
    className: (0, _classnames.default)(classes.root, className)
  }, rest), /*#__PURE__*/React.createElement(_Box.default, {
    className: classes.selector
  }, /*#__PURE__*/React.createElement(_Tooltip.default, {
    title: /*#__PURE__*/React.createElement(_reactIntl.FormattedMessage, {
      id: "ui.feedObject.activitiesMenu.tooltipTitle",
      defaultMessage: "ui.feedObject.activitiesMenu.tooltipTitle"
    })
  }, /*#__PURE__*/React.createElement(_material.Button, {
    variant: "text",
    size: "small",
    onClick: handleClick,
    endIcon: /*#__PURE__*/React.createElement(_Icon.default, null, "expand_more"),
    color: "inherit"
  }, selectedActivities === _feedObject.SCFeedObjectActivitiesType.CONNECTIONS_COMMENTS && followEnabled ? intl.formatMessage(messages.followedComments) : intl.formatMessage(messages[`${_core.StringUtils.camelCase(selectedActivities)}`])))), /*#__PURE__*/React.createElement(_Menu.default, {
    anchorEl: anchorEl,
    open: open,
    onClose: handleClose,
    onClick: handleClose,
    PaperProps: {
      elevation: 0,
      sx: {
        overflow: 'visible',
        filter: 'drop-shadow(0px 2px 8px rgba(0,0,0,0.32))',
        mt: 1.5,
        '&:before': {
          content: '""',
          display: 'block',
          position: 'absolute',
          top: 0,
          right: 14,
          width: 10,
          height: 10,
          bgcolor: 'background.paper',
          transform: 'translateY(-50%) rotate(45deg)',
          zIndex: 0
        }
      }
    },
    transformOrigin: {
      horizontal: 'right',
      vertical: 'top'
    },
    anchorOrigin: {
      horizontal: 'right',
      vertical: 'bottom'
    }
  }, !hideRelevantActivitiesItem && /*#__PURE__*/React.createElement(_material.ListItem, {
    selected: selectedActivities === _feedObject.SCFeedObjectActivitiesType.RELEVANCE_ACTIVITIES,
    button: true,
    onClick: handleChangeActivitiesType(_feedObject.SCFeedObjectActivitiesType.RELEVANCE_ACTIVITIES)
  }, /*#__PURE__*/React.createElement(_material.ListItemText, {
    primary: /*#__PURE__*/React.createElement("b", null, intl.formatMessage(messages.relevantActivities)),
    secondary: /*#__PURE__*/React.createElement(_reactIntl.FormattedMessage, {
      id: 'ui.feedObject.activitiesMenu.relevantActivitiesDesc',
      defaultMessage: 'ui.feedObject.activitiesMenu.relevantActivitiesDesc'
    })
  })), /*#__PURE__*/React.createElement(_material.ListItem, {
    selected: selectedActivities === _feedObject.SCFeedObjectActivitiesType.RECENT_COMMENTS,
    button: true,
    onClick: handleChangeActivitiesType(_feedObject.SCFeedObjectActivitiesType.RECENT_COMMENTS)
  }, /*#__PURE__*/React.createElement(_material.ListItemText, {
    primary: /*#__PURE__*/React.createElement("b", null, intl.formatMessage(messages.recentComments)),
    secondary: /*#__PURE__*/React.createElement(_reactIntl.FormattedMessage, {
      id: 'ui.feedObject.activitiesMenu.recentCommentsDesc',
      defaultMessage: 'ui.feedObject.activitiesMenu.recentCommentsDesc'
    })
  })), /*#__PURE__*/React.createElement(_material.ListItem, {
    selected: selectedActivities === _feedObject.SCFeedObjectActivitiesType.FIRST_COMMENTS,
    button: true,
    onClick: handleChangeActivitiesType(_feedObject.SCFeedObjectActivitiesType.FIRST_COMMENTS)
  }, /*#__PURE__*/React.createElement(_material.ListItemText, {
    primary: /*#__PURE__*/React.createElement("b", null, intl.formatMessage(messages.firstComments)),
    secondary: /*#__PURE__*/React.createElement(_reactIntl.FormattedMessage, {
      id: 'ui.feedObject.activitiesMenu.firstCommentsDesc',
      defaultMessage: 'ui.feedObject.activitiesMenu.firstCommentsDesc'
    })
  })), scUserContext.user && /*#__PURE__*/React.createElement(_material.ListItem, {
    selected: selectedActivities === _feedObject.SCFeedObjectActivitiesType.CONNECTIONS_COMMENTS,
    button: true,
    onClick: handleChangeActivitiesType(_feedObject.SCFeedObjectActivitiesType.CONNECTIONS_COMMENTS)
  }, /*#__PURE__*/React.createElement(_material.ListItemText, {
    primary: /*#__PURE__*/React.createElement("b", null, followEnabled ? intl.formatMessage(messages.followedComments) : intl.formatMessage(messages.connectionsComments)),
    secondary: followEnabled ? /*#__PURE__*/React.createElement(_reactIntl.FormattedMessage, {
      id: 'ui.feedObject.activitiesMenu.followedCommentsDesc',
      defaultMessage: 'ui.feedObject.activitiesMenu.followedCommentsDesc'
    }) : /*#__PURE__*/React.createElement(_reactIntl.FormattedMessage, {
      id: 'ui.feedObject.activitiesMenu.connectionsCommentsDesc',
      defaultMessage: 'ui.feedObject.activitiesMenu.connectionsCommentsDesc'
    })
  }))));
}