"use strict";

exports.__esModule = true;
exports.getContribution = getContribution;
exports.getContributionHtml = getContributionHtml;
exports.getContributionRouteName = getContributionRouteName;
exports.getContributionSnippet = getContributionSnippet;
exports.getContributionType = getContributionType;
exports.getRouteData = getRouteData;

var _react = _interopRequireDefault(require("react"));

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

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

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

/**
 * From obj extract type of the contribution
 * @param obj
 */
function getContributionType(obj) {
  return _core.SCFeedObjectTypologyType.DISCUSSION in obj ? _core.SCFeedObjectTypologyType.DISCUSSION : _core.SCFeedObjectTypologyType.POST in obj ? _core.SCFeedObjectTypologyType.POST : _core.SCFeedObjectTypologyType.STATUS in obj ? _core.SCFeedObjectTypologyType.STATUS : _core.SCCommentTypologyType in obj ? _core.SCCommentTypologyType : null;
}
/**
 * From obj extract the contribution
 * @param obj
 */


function getContribution(obj) {
  return _core.SCFeedObjectTypologyType.DISCUSSION in obj ? obj[_core.SCFeedObjectTypologyType.DISCUSSION] : _core.SCFeedObjectTypologyType.POST in obj ? obj[_core.SCFeedObjectTypologyType.POST] : _core.SCFeedObjectTypologyType.STATUS in obj ? obj[_core.SCFeedObjectTypologyType.STATUS] : _core.SCCommentTypologyType in obj ? obj[_core.SCCommentTypologyType] : null;
}
/**
 * Get a snippet for a contribution
 * @param obj (Discussion, Post, Status, Comment)
 */


function getContributionSnippet(obj) {
  if (obj.type === _core.SCFeedObjectTypologyType.DISCUSSION) {
    return obj.summary ? /*#__PURE__*/_react.default.createElement("span", {
      dangerouslySetInnerHTML: {
        __html: obj.summary
      }
    }) : obj.title;
  } else {
    return obj.summary ? /*#__PURE__*/_react.default.createElement("span", {
      dangerouslySetInnerHTML: {
        __html: obj.summary
      }
    }) : /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: `ui.common.${obj.type.toLowerCase()}WithoutText`,
      defaultMessage: `ui.common.${obj.type.toLowerCase()}WithoutText`
    });
  }
}
/**
 * Get the contribution text
 * Hydrate text with mention, etc.
 * @param obj Object of types: Discussion, Post, Status, Comment
 * @param handleUrl Func that handle urls
 */


function getContributionHtml(obj, handleUrl) {
  return obj.html.replace(/<mention id="([0-9]+)">@([a-z\d_]+)<\/mention>/gi, (match, id, username) => {
    return `<a target='_blank' href='${handleUrl(_core.SCRoutes.USER_PROFILE_ROUTE_NAME, {
      id,
      username
    })}'>@${username}</a>`;
  });
}
/**
 * Get route name for a contribution
 * @param obj
 */


function getContributionRouteName(obj) {
  if (!obj) return null;

  if (obj.type) {
    return _core.SCRoutes[`${obj.type.toUpperCase()}_ROUTE_NAME`];
  }

  return obj.type;
}
/**
 * Get data for scRoutingContext.url()
 * @param obj (Discussion, Post, Status, Comment)
 */


function getRouteData(obj) {
  let data = {};

  if (obj.type === _core.SCFeedObjectTypologyType.DISCUSSION || obj.type === _core.SCFeedObjectTypologyType.POST || obj.type === _core.SCFeedObjectTypologyType.STATUS) {
    data = obj;
  } else if (obj.type === _core.SCCommentTypologyType) {
    const contribution_type = getContributionType(obj);
    const isContributionTypeObj = obj[contribution_type] && typeof obj[contribution_type] === 'object';
    data = Object.assign({}, obj, {
      contribution_type,
      contribution_id: isContributionTypeObj ? obj[contribution_type].id : obj[contribution_type],
      contribution_slug: isContributionTypeObj ? obj[contribution_type].slug : contribution_type
    });
  }

  return data;
}