"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.truncateDisplayText=exports.suppressAutoReplyPanelOnInit=exports.shouldCountAddCommentForUnread=exports.scheduleAutoReplyFallback=exports.normalizeAutoReplyInfo=exports.mergeAutoReplyByAnchor=exports.isOwnAnchorComment=exports.isAutoReplyComment=exports.isAutoReplyAnchorVisible=exports.hasAutoReplyForPollingKey=exports.hasAutoReplyForAnchor=exports.getH5MsgIdCache=exports.getCommentAnchorKeys=exports.getAutoReplyPollingKey=exports.getAnchorPreviewParts=exports.expandAutoReplyComments=exports.clearH5MsgIdCache=exports.clearAllAutoReplyFallbackTimers=exports.cancelAutoReplyFallbackByAnchorKeys=exports.buildAutoReplyDisplayMeta=exports.buildAutoReplyComment=exports.addH5MsgIdToCache=exports.AUTO_REPLY_DISPLAY=void 0;var _comment=require("./comment");function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_unsupportedIterableToArray(e)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(e,o){if(e){if("string"==typeof e)return _arrayLikeToArray(e,o);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_arrayLikeToArray(e,o):void 0}}function _iterableToArray(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _arrayWithoutHoles(e){if(Array.isArray(e))return _arrayLikeToArray(e)}function _arrayLikeToArray(e,o){(null==o||o>e.length)&&(o=e.length);for(var t=0,n=new Array(o);t>>0;return-1*(o||1)},isAutoReplyComment=function(e){var o,t;return Boolean(null==e||null===(o=e.Extra)||void 0===o||null===(t=o._autoReplyInfo_)||void 0===t?void 0:t.IsAutoReply)};exports.isAutoReplyComment=isAutoReplyComment;var shouldCountAddCommentForUnread=function(e){return!isAutoReplyComment(e)};exports.shouldCountAddCommentForUnread=shouldCountAddCommentForUnread;var hasAutoReplyForPollingKey=function(e,o,t){if(null==o||null==t||!Number.isFinite(Number(o))||!Number.isFinite(Number(t)))return!1;var n=getAutoReplyPollingKey(o,t);return(e||[]).some(function(e){var r,l=null==e||null===(r=e.Extra)||void 0===r?void 0:r._autoReplyInfo_;return!(null==l||!l.IsAutoReply)&&(l.AutoReplyId===n||Number(l.SourceMsgId)===Number(o)&&Number(l.ServerShowMessageAnchorId)===Number(t))})};exports.hasAutoReplyForPollingKey=hasAutoReplyForPollingKey;var hasAutoReplyForAnchor=function(e,o){return hasAutoReplyForPollingKey(e,o,o)};exports.hasAutoReplyForAnchor=hasAutoReplyForAnchor;var buildAutoReplyComment=function(e,o){var t,n,r,l,a,u=e.ShowMessageAnchorId,i=e.ReplyContent||"智能助理已回复该问题",s=e.AutoReplyCommentRobotName||DEFAULT_ROBOT_NAME,c=(null==o||null===(t=o.Extra)||void 0===t?void 0:t.User)||{},p=c.Nickname?String(c.Nickname):"",A=null!=o&&null!==(n=o.Extra)&&void 0!==n&&n.TextContent?String(o.Extra.TextContent):"";return{Common:{H5MsgId:"auto_reply_".concat(e.AutoReplyId),MsgId:getStableAutoReplyMsgId(e.AutoReplyId),CreateTime:(null==o||null===(r=o.Common)||void 0===r?void 0:r.CreateTime)||Date.now(),_formerMsgId_:u},Extra:{ContentType:_comment.MessageContentType.Text,TextContent:i,TextContentExtra:[],User:{Nickname:s,Email:"",AvatarUrl:e.AutoReplyCommentRobotAvatar||""},IsDelete:!1,TextContentCn:"",IsSilence:0,TextContentEn:"",TextContentJp:"",TextContentKo:"",IsSelfLike:!1,LikeCount:0,IsPresenter:!1,_autoReplyInfo_:_objectSpread(_objectSpread({},e),{},{ReplyContent:i,ShowMessageAnchorId:u,SourceMsgId:null!==(l=e.SourceMsgId)&&void 0!==l?l:null==o||null===(a=o.Common)||void 0===a?void 0:a.MsgId,ServerShowMessageAnchorId:e.ServerShowMessageAnchorId,IsAutoReply:!0,AnchorNickname:p||e.AnchorNickname,AnchorTextContent:A||e.AnchorTextContent||e.Keyword})}}};exports.buildAutoReplyComment=buildAutoReplyComment;var getH5MsgIdCache=function(){try{var e=wx.getStorageSync(H5_MSG_ID_CACHE_KEY);if(e){var o=JSON.parse(e);return new Set(Array.isArray(o)?o.map(String):[])}}catch(e){console.warn("[autoReply] parse H5MsgId cache failed",e)}return new Set};exports.getH5MsgIdCache=getH5MsgIdCache;var addH5MsgIdToCache=function(e){if(null!=e&&""!==e)try{var o=getH5MsgIdCache();o.add(String(e));var t=Array.from(o),n=t.length>MAX_H5_MSG_ID_CACHE_SIZE?t.slice(-MAX_H5_MSG_ID_CACHE_SIZE):t;wx.setStorageSync(H5_MSG_ID_CACHE_KEY,JSON.stringify(n))}catch(e){console.warn("[autoReply] save H5MsgId cache failed",e)}};exports.addH5MsgIdToCache=addH5MsgIdToCache;var clearH5MsgIdCache=function(){try{wx.removeStorageSync(H5_MSG_ID_CACHE_KEY)}catch(e){console.warn("[autoReply] clear H5MsgId cache failed",e)}};exports.clearH5MsgIdCache=clearH5MsgIdCache;var isOwnComment=function(e,o){var t,n,r,l;if(1===(null==e||null===(t=e.Extra)||void 0===t?void 0:t.IsOwn)||!0===(null==e||null===(n=e.Extra)||void 0===n?void 0:n.IsOwn))return!0;var a=null==e||null===(r=e.Extra)||void 0===r||null===(l=r.User)||void 0===l?void 0:l.UserId;return null!=o&&null!=a&&String(a)===String(o)},isOwnAnchorComment=function(e,o){var t,n,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[];if(isOwnComment(e,o))return!0;var l=getH5MsgIdCache(),a=null==e||null===(t=e.Common)||void 0===t?void 0:t.MsgId,u=null==e||null===(n=e.Common)||void 0===n?void 0:n.H5MsgId;return!(null==a||!l.has(String(a)))||!(null==u||!l.has(String(u)))||(r||[]).some(function(e){var t,n;if(!isOwnComment(e,o))return!1;if(null!=a&&(null===(t=e.Common)||void 0===t?void 0:t.MsgId)===a)return!0;var r=null===(n=e.Common)||void 0===n?void 0:n.H5MsgId;return null!=a&&null!=r&&String(r)===String(a)||null!=u&&null!=r&&String(r)===String(u)})};exports.isOwnAnchorComment=isOwnAnchorComment;var isJustSentOwnComment=function(e,o){var t,n,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],l=null==e||null===(t=e.Common)||void 0===t?void 0:t.MsgId,a=null==e||null===(n=e.Common)||void 0===n?void 0:n.H5MsgId;return(r||[]).some(function(e){var t,n,r;return!(!isOwnComment(e,o)||!e.isMock)&&(null!=l&&(null===(t=e.Common)||void 0===t?void 0:t.MsgId)===l||null!=a&&String(null===(n=e.Common)||void 0===n?void 0:n.H5MsgId)===String(a)||null!=l&&String(null===(r=e.Common)||void 0===r?void 0:r.H5MsgId)===String(l))})},getCommentAnchorKeys=function(e){var o,t,n=[],r=null==e||null===(o=e.Common)||void 0===o?void 0:o.MsgId,l=null==e||null===(t=e.Common)||void 0===t?void 0:t.H5MsgId;return null!=r&&n.push(String(r)),null==l||null!=r&&String(l)===String(r)||n.push(String(l)),n};exports.getCommentAnchorKeys=getCommentAnchorKeys;var cancelAutoReplyFallbackByAnchorKeys=function(e){(e||[]).forEach(function(e){var o=pendingAutoReplyFallbackTimers.get(e);null!=o&&(clearTimeout(o),pendingAutoReplyFallbackTimers.delete(e))})};exports.cancelAutoReplyFallbackByAnchorKeys=cancelAutoReplyFallbackByAnchorKeys;var clearAllAutoReplyFallbackTimers=function(){pendingAutoReplyFallbackTimers.forEach(function(e){clearTimeout(e)}),pendingAutoReplyFallbackTimers.clear()};exports.clearAllAutoReplyFallbackTimers=clearAllAutoReplyFallbackTimers;var scheduleAutoReplyFallback=function(e,o,t){var n,r,l,a,u;if(e&&o&&t){var i=getCommentAnchorKeys(t);if(0!==i.length){cancelAutoReplyFallbackByAnchorKeys(i);var s=Number(null===(n=t.Common)||void 0===n?void 0:n.MsgId),c=Number(null!==(r=o.ShowMessageAnchorId)&&void 0!==r?r:s),p=null!==(l=t.Extra)&&void 0!==l&&l.TextContent?String(t.Extra.TextContent):"",A=null!==(a=t.Extra)&&void 0!==a&&null!==(u=a.User)&&void 0!==u&&u.Nickname?String(t.Extra.User.Nickname):"",y=setTimeout(function(){cancelAutoReplyFallbackByAnchorKeys(i);var n=e.get("chat.chatList")||[];if(!hasAutoReplyForPollingKey(n,s,c)){var r=buildAutoReplyComment(_objectSpread(_objectSpread({},o),{},{AutoReplyId:getAutoReplyPollingKey(s,c),SourceMsgId:s,ServerShowMessageAnchorId:c,ShowMessageAnchorId:s,AutoShowPanel:!0,Keyword:o.Keyword||p,AnchorTextContent:o.AnchorTextContent||p,AnchorNickname:o.AnchorNickname||A}),t),l=mergeAutoReplyByAnchor(n,[r]);e.set("chat.chatList",l),e.set("chat.addComment",{data:[r],isSend:!0})}},AUTO_REPLY_PANEL_FALLBACK_MS);i.forEach(function(e){pendingAutoReplyFallbackTimers.set(e,y)})}}};exports.scheduleAutoReplyFallback=scheduleAutoReplyFallback;var suppressAutoReplyPanelOnInit=function(e,o){return o?(e||[]).map(function(e){var o,t=null==e||null===(o=e.Extra)||void 0===o?void 0:o._autoReplyInfo_;return null!=t&&t.IsAutoReply&&t.AutoShowPanel?_objectSpread(_objectSpread({},e),{},{Extra:_objectSpread(_objectSpread({},e.Extra),{},{_autoReplyInfo_:_objectSpread(_objectSpread({},t),{},{AutoShowPanel:!1})})}):e}):e};exports.suppressAutoReplyPanelOnInit=suppressAutoReplyPanelOnInit;var isAutoReplyAnchorVisible=function(e,o){var t,n;if(!isAutoReplyComment(e))return!0;var r=null===(t=e.Extra)||void 0===t||null===(n=t._autoReplyInfo_)||void 0===n?void 0:n.ShowMessageAnchorId;return null!=r&&(o||[]).some(function(e){var o,t,n;return Number(null===(o=e.Common)||void 0===o?void 0:o.MsgId)===Number(r)&&!(null!==(t=e.Extra)&&void 0!==t&&t.IsDelete)&&Boolean(null===(n=e.Extra)||void 0===n?void 0:n.TextContent)})};exports.isAutoReplyAnchorVisible=isAutoReplyAnchorVisible;var expandAutoReplyComments=function(e,o){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],n=arguments.length>3&&void 0!==arguments[3]&&arguments[3],r=[];return(e||[]).forEach(function(e){var l,a;r.push(e);var u=normalizeAutoReplyInfo(getAutoReplyPollingSource(e),null==e||null===(l=e.Common)||void 0===l?void 0:l.MsgId);if(u){var i=Number(null==e||null===(a=e.Common)||void 0===a?void 0:a.MsgId),s=getServerAutoReplyAnchorMsgId(u,e);if(Number.isFinite(i)&&Number.isFinite(s)){var c,p=getAutoReplyPollingKey(i,s),A=isOwnAnchorComment(e,o,t);if(A&&!n){var y=hasAutoReplyForPollingKey([].concat(_toConsumableArray(t),r),i,s);if(pendingOwnAutoReplyExpansion.has(i))pendingOwnAutoReplyExpansion.delete(i);else if(!y&&isJustSentOwnComment(e,o,t))return void pendingOwnAutoReplyExpansion.add(i);cancelAutoReplyFallbackByAnchorKeys(getCommentAnchorKeys(e))}hasAutoReplyForPollingKey([].concat(_toConsumableArray(t),r),i,s)||r.push(buildAutoReplyComment(_objectSpread(_objectSpread({},u),{},{AutoReplyId:p,SourceMsgId:i,ServerShowMessageAnchorId:s,ShowMessageAnchorId:i,AutoShowPanel:null!==(c=u.AutoShowPanel)&&void 0!==c?c:A}),e))}}}),r};exports.expandAutoReplyComments=expandAutoReplyComments;var mergeAutoReplyByAnchor=function(e,o){var t=_toConsumableArray(e||[]);return(o||[]).forEach(function(e){var o;if(isAutoReplyComment(e)){var n=null===(o=e.Extra._autoReplyInfo_)||void 0===o?void 0:o.ShowMessageAnchorId,r=t.findIndex(function(e){var o;return Number(null===(o=e.Common)||void 0===o?void 0:o.MsgId)===Number(n)});if(r<0)t.push(e);else{for(var l=r+1;l