define(["jquery","core/notification","core/str","core/templates","core/user_date","core_message/message_drawer_view_conversation_constants"],function(a,b,c,d,e,f){var g=f.SELECTORS,h=f.TEMPLATES,i=f.CONVERSATION_TYPES,j=function(a){return a.find(g.CONTENT_MESSAGES_CONTAINER)},k=function(a){j(a).removeClass("hidden")},l=function(a){j(a).addClass("hidden")},m=function(a){return a.find(g.SELF_CONVERSATION_MESSAGE_CONTAINER)},n=function(a){return m(a).addClass("hidden")},o=function(a){return a.find(g.CONTACT_REQUEST_SENT_MESSAGE_CONTAINER)},p=function(a){return o(a).addClass("hidden")},q=function(a){return a.find(g.CONTENT_MESSAGES_FOOTER_CONTAINER)},r=function(a){q(a).removeClass("hidden")},s=function(a){q(a).addClass("hidden")},t=function(a){return a.find(g.CONTENT_MESSAGES_FOOTER_EDIT_MODE_CONTAINER)},u=function(a){t(a).removeClass("hidden")},v=function(a){t(a).addClass("hidden")},w=function(a){return a.find(g.PLACEHOLDER_CONTAINER)},x=function(a){w(a).removeClass("hidden")},y=function(a){w(a).addClass("hidden")},z=function(a){return a.find(g.CONTENT_MESSAGES_FOOTER_REQUIRE_CONTACT_CONTAINER)},A=function(a){z(a).removeClass("hidden")},B=function(a){z(a).addClass("hidden")},C=function(a){return a.find(g.CONTENT_MESSAGES_FOOTER_REQUIRE_UNBLOCK_CONTAINER)},D=function(a){C(a).removeClass("hidden")},E=function(a){C(a).addClass("hidden")},F=function(a){return a.find(g.CONTENT_MESSAGES_FOOTER_UNABLE_TO_MESSAGE_CONTAINER)},G=function(a){F(a).removeClass("hidden")},H=function(a){F(a).addClass("hidden")},I=function(a){P(a),S(a),V(a)},J=function(a){s(a),v(a),y(a),B(a),E(a),H(a)},K=function(a){return a.find(g.CONTENT_PLACEHOLDER_CONTAINER)},L=function(a){K(a).removeClass("hidden")},M=function(a){K(a).addClass("hidden")},N=function(a){return a.find(g.HEADER)},O=function(a){N(a).removeClass("hidden")},P=function(a){N(a).addClass("hidden")},Q=function(a){return a.find(g.HEADER_EDIT_MODE)},R=function(a){Q(a).removeClass("hidden")},S=function(a){Q(a).addClass("hidden")},T=function(a){return a.find(g.HEADER_PLACEHOLDER_CONTAINER)},U=function(a){T(a).removeClass("hidden")},V=function(a){T(a).addClass("hidden")},W=function(a){return a.find(g.MESSAGE_TEXT_AREA)},X=function(a,b){var c=j(a);return c.find('[data-message-id="'+b+'"]')},Y=function(a,b){var c=j(a);return c.find('[data-day-id="'+b+'"]')},Z=function(a){return a.find(g.MORE_MESSAGES_LOADING_ICON_CONTAINER)},$=function(a){Z(a).removeClass("hidden")},_=function(a){Z(a).addClass("hidden")},aa=function(a){a.find(g.SEND_MESSAGE_BUTTON).prop("disabled",!0),W(a).prop("disabled",!0)},ba=function(a){a.find(g.SEND_MESSAGE_BUTTON).prop("disabled",!1),W(a).prop("disabled",!1)},ca=function(a){aa(a),a.find(g.SEND_MESSAGE_ICON_CONTAINER).addClass("hidden"),a.find(g.LOADING_ICON_CONTAINER).removeClass("hidden")},da=function(a){ba(a),a.find(g.SEND_MESSAGE_ICON_CONTAINER).removeClass("hidden"),a.find(g.LOADING_ICON_CONTAINER).addClass("hidden")},ea=function(a){var b=W(a);b.val(""),b.focus()},fa=function(a){return a.find(g.CONFIRM_DIALOGUE_CONTAINER)},ga=function(a){var b=fa(a),c=b.siblings(":not(.hidden)");c.attr("aria-hidden",!0),c.attr("tabindex",-1),c.attr("data-confirm-dialogue-hidden",!0),b.removeClass("hidden")},ha=function(a){var b=fa(a),c=b.siblings('[data-confirm-dialogue-hidden="true"]');c.removeAttr("aria-hidden"),c.removeAttr("tabindex"),c.removeAttr("data-confirm-dialogue-hidden"),b.addClass("hidden")},ia=function(a,b){Q(a).find(g.MESSAGES_SELECTED_COUNT).text(b)},ja=function(a,b){return a.map(function(a){return{id:a.id,isread:a.isRead,fromloggedinuser:a.fromLoggedInUser,userfrom:a.userFrom,text:a.text,formattedtime:b[a.timeCreated]}})},ka=function(b,c,e,f,g){var i=j(c),k=f.map(function(a){return d.render(h.DAY,{timestamp:a.value.timestamp,messages:ja(a.value.messages,g)})});return a.when.apply(a,k).then(function(){f.forEach(function(b,d){k[d].then(function(d){if(b.before){var e=Y(c,b.before.timestamp);return a(d).insertBefore(e)}return i.append(d)})["catch"](function(){})})})},la=function(b,c,e,f,i){var j=f.map(function(a){return a.value}),k=ja(j,i);return d.render(h.MESSAGES,{messages:k}).then(function(b){var d=a(b);f.forEach(function(a){var b=d.find('[data-message-id="'+a.value.id+'"]');if(a.before){var e=X(c,a.before.id);return b.insertBefore(e)}var f=Y(c,a.day.timestamp),h=f.find(g.DAY_MESSAGES_CONTAINER);return h.append(b)})})},ma=function(a,b){b.forEach(function(b){Y(a,b.timestamp).remove()})},na=function(a,b){b.forEach(function(b){X(a,b.id).remove()})},oa=function(b,d,f,g){var h=[],i=g.days.add.length>0,j=g.messages.add.length>0,k=[],l=a.Deferred().resolve({}).promise();return i&&(k=k.concat(g.days.add.reduce(function(a,b){return a.concat(b.value.messages.map(function(a){return a.timeCreated}))},[]))),j&&(k=k.concat(g.messages.add.map(function(a){return a.value.timeCreated}))),k.length&&(l=c.get_string("strftimetime24","core_langconfig").then(function(a){var b=k.map(function(b){return{timestamp:b,format:a}});return e.get(b)}).then(function(a){return k.reduce(function(b,c,d){return b[c]=a[d],b},{})})),i&&h.push(l.then(function(a){return ka(b,d,f,g.days.add,a)})),j&&h.push(l.then(function(a){return la(b,d,f,g.messages.add,a)})),g.days.remove.length>0&&ma(d,g.days.remove),g.messages.remove.length>0&&na(d,g.messages.remove),a.when.apply(a,h)},pa=function(a,b,c,e){var f=N(a),g=h.HEADER_PUBLIC;return e.context.showrouteback="false"===a.attr("data-from-panel"),e.type==i.PRIVATE?g=e.showControls?h.HEADER_PRIVATE:h.HEADER_PRIVATE_NO_CONTROLS:e.type==i.SELF&&(g=h.HEADER_SELF),d.render(g,e.context).then(function(a,b){d.replaceNodeContents(f,a,b)})},qa=function(a,b,d,e){switch(J(d),e.type){case"placeholder":return x(d);case"add-contact":return c.get_strings([{key:"requirecontacttomessage",component:"core_message",param:e.user.fullname},{key:"isnotinyourcontacts",component:"core_message",param:e.user.fullname}]).then(function(a){var b=a[1],c=a[0],e=z(d);return e.find(g.TITLE).text(b),e.find(g.TEXT).text(c),A(d),a});case"edit-mode":return u(d);case"content":return r(d);case"unblock":return D(d);case"unable-to-message":return G(d)}return!0},ra=function(a,b,c,d){var e=j(b),f=X(b,d),g=f.position();if(g){var h=e.scrollTop()+g.top;e.scrollTop(h)}},sa=function(a,b,c,d){d?(P(a),U(a)):(O(a),V(a))},ta=function(a,b,c,d){d?(l(b),L(b)):(k(b),M(b))},ua=function(a,b,c,d){d?$(b):_(b)},va=function(a,b,c,d){d?ca(c):(da(c),ea(c))},wa=function(a,b,c,d,e,f,h,i){var j=fa(b),k=d.map(function(a){return j.find(a)}),l=j.find(g.CONFIRM_DIALOGUE_CANCEL_BUTTON),m=j.find(g.CONFIRM_DIALOGUE_TEXT),n=j.find(g.CONFIRM_DIALOGUE_HEADER);j.find("button").addClass("hidden"),h?l.removeClass("hidden"):l.addClass("hidden"),f?(n.removeClass("hidden"),n.text(f)):(n.addClass("hidden"),n.text("")),k.forEach(function(a){a.removeClass("hidden")}),m.text(e),ga(c),ga(b),i||ga(a),j.find(g.CAN_RECEIVE_FOCUS).filter(":visible").first().focus()},xa=function(a,b,c){var d=fa(b),e=d.find(g.CONFIRM_DIALOGUE_CANCEL_BUTTON),f=d.find(g.CONFIRM_DIALOGUE_TEXT),h=d.find(g.CONFIRM_DIALOGUE_HEADER);return Ga(b),ha(b),ha(c),ha(a),d.find("button").addClass("hidden"),e.removeClass("hidden"),f.text(""),h.addClass("hidden"),h.text(""),a.find(g.CAN_RECEIVE_FOCUS).first().focus(),!0},ya=function(a,b,d,e){return e?c.get_string("blockuserconfirm","core_message",e.fullname).then(function(c){return wa(a,b,d,[g.ACTION_CONFIRM_BLOCK],c,"",!0,!1)}):xa(a,b,d)},za=function(a,b,d,e){return e?c.get_string("unblockuserconfirm","core_message",e.fullname).then(function(c){return wa(a,b,d,[g.ACTION_CONFIRM_UNBLOCK],c,"",!0,!1)}):xa(a,b,d)},Aa=function(a,b,d,e){return e?c.get_string("addcontactconfirm","core_message",e.fullname).then(function(c){return wa(a,b,d,[g.ACTION_CONFIRM_ADD_CONTACT],c,"",!0,!1)}):xa(a,b,d)},Ba=function(a,b,d,e){return e?c.get_string("removecontactconfirm","core_message",e.fullname).then(function(c){return wa(a,b,d,[g.ACTION_CONFIRM_REMOVE_CONTACT],c,"",!0,!1)}):xa(a,b,d)},Ca=function(a,b,d,e){var f=null;return e.type==i.SELF?f="deleteselectedmessagesconfirmselfconversation":e.canDeleteMessagesForAllUsers?(Fa(b),f="deleteforeveryoneselectedmessagesconfirm"):f="deleteselectedmessagesconfirm",e.show?c.get_string(f,"core_message").then(function(c){return wa(a,b,d,[g.ACTION_CONFIRM_DELETE_SELECTED_MESSAGES],c,"",!0,!1)}):xa(a,b,d)},Da=function(a,b,d,e){var f=null;return e==i.SELF?f="deleteallselfconfirm":e&&(f="deleteallconfirm"),f?c.get_string(f,"core_message").then(function(c){return wa(a,b,d,[g.ACTION_CONFIRM_DELETE_CONVERSATION],c,"",!0,!1)}):xa(a,b,d)},Ea=function(a,b,d,e){return e?c.get_string("userwouldliketocontactyou","core_message",e.fullname).then(function(c){var e=[g.ACTION_ACCEPT_CONTACT_REQUEST,g.ACTION_DECLINE_CONTACT_REQUEST];return wa(a,b,d,e,c,"",!1,!0)}):xa(a,b,d)},Fa=function(a){var b=fa(a),c=b.find(g.DELETE_MESSAGES_FOR_ALL_USERS_TOGGLE_CONTAINER);c.removeClass("hidden")},Ga=function(a){var b=fa(a),c=b.find(g.DELETE_MESSAGES_FOR_ALL_USERS_TOGGLE_CONTAINER),d=b.find(g.DELETE_MESSAGES_FOR_ALL_USERS_TOGGLE);d.prop("checked",!1),c.addClass("hidden")},Ha=function(a,b,c,d){d?(a.find(g.ACTION_REQUEST_BLOCK).addClass("hidden"),a.find(g.ACTION_REQUEST_UNBLOCK).removeClass("hidden")):(a.find(g.ACTION_REQUEST_BLOCK).removeClass("hidden"),a.find(g.ACTION_REQUEST_UNBLOCK).addClass("hidden"))},Ia=function(a,b,c,d){var e=a.find(g.FAVOURITE_ICON_CONTAINER),f=a.find(g.ACTION_CONFIRM_FAVOURITE),h=a.find(g.ACTION_CONFIRM_UNFAVOURITE);switch(d){case"hide":e.addClass("hidden"),f.addClass("hidden"),h.addClass("hidden");break;case"show-add":e.addClass("hidden"),f.removeClass("hidden"),h.addClass("hidden");break;case"show-remove":e.removeClass("hidden"),f.addClass("hidden"),h.removeClass("hidden")}},Ja=function(a,b,c,d){var e=a.find(g.MUTED_ICON_CONTAINER),f=a.find(g.ACTION_CONFIRM_MUTE),h=a.find(g.ACTION_CONFIRM_UNMUTE);switch(d){case"hide":e.addClass("hidden"),f.addClass("hidden"),h.addClass("hidden");break;case"show-mute":e.addClass("hidden"),f.removeClass("hidden"),h.addClass("hidden");break;case"show-unmute":e.removeClass("hidden"),f.addClass("hidden"),h.removeClass("hidden")}},Ka=function(a,b,c,d){var e=a.find(g.ACTION_REQUEST_ADD_CONTACT),f=a.find(g.ACTION_REQUEST_REMOVE_CONTACT);switch(d){case"pending-contact":e.addClass("hidden"),f.addClass("hidden");break;case"contact":e.addClass("hidden"),f.removeClass("hidden");break;case"non-contact":e.removeClass("hidden"),f.addClass("hidden")}},La=function(a,b,c,d){var e=fa(b),f=e.find("button"),h=e.find(g.CONFIRM_DIALOGUE_BUTTON_TEXT),i=e.find(g.LOADING_ICON_CONTAINER);d?(f.prop("disabled",!0),h.addClass("hidden"),i.removeClass("hidden")):(f.prop("disabled",!1),h.removeClass("hidden"),i.addClass("hidden"))},Ma=function(a,b,c,d){var e=null;d?(e=b.find(g.MESSAGE_NOT_SELECTED),e.find(g.MESSAGE_NOT_SELECTED_ICON).removeClass("hidden"),P(a),R(a)):(e=j(b),e.find(g.MESSAGE_NOT_SELECTED_ICON).addClass("hidden"),e.find(g.MESSAGE_SELECTED_ICON).addClass("hidden"),O(a),S(a))},Na=function(a,b,c,d){var e=d.count>0;d.add.length&&d.add.forEach(function(a){var c=X(b,a);c.find(g.MESSAGE_NOT_SELECTED_ICON).addClass("hidden"),c.find(g.MESSAGE_SELECTED_ICON).removeClass("hidden"),c.attr("aria-checked",!0)}),d.remove.length&&d.remove.forEach(function(a){var c=X(b,a);e&&c.find(g.MESSAGE_NOT_SELECTED_ICON).removeClass("hidden"),c.find(g.MESSAGE_SELECTED_ICON).addClass("hidden"),c.attr("aria-checked",!1)}),ia(a,d.count)},Oa=function(a,b,d,e){return e.show&&!e.hasMessages?c.get_strings([{key:"requirecontacttomessage",component:"core_message",param:e.user.fullname},{key:"isnotinyourcontacts",component:"core_message",param:e.user.fullname}]).then(function(c){var e=c[1],f=c[0];return wa(a,b,d,[g.ACTION_REQUEST_ADD_CONTACT],f,e,!1,!0)}):xa(a,b,d)},Pa=function(a,b,c,d){var e=m(b);return d?e.removeClass("hidden"):e.addClass("hidden"),!0},Qa=function(a,b,d,e){var f=o(b);return e?c.get_string("yourcontactrequestpending","core_message",e).then(function(a){return f.find(g.TEXT).text(a),f.removeClass("hidden"),a}):(f.addClass("hidden"),!0)},Ra=function(a,b,c){return xa(a,b,c),p(b),n(b),I(a),U(a),J(c),x(c),!0},Sa=function(c,d,e,f){var g=[{reset:Ra},{conversation:oa,header:pa,footer:qa,confirmBlockUser:ya,confirmUnblockUser:za,confirmAddContact:Aa,confirmRemoveContact:Ba,confirmDeleteSelectedMessages:Ca,confirmDeleteConversation:Da,confirmContactRequest:Ea,requireAddContact:Oa,selfConversationMessage:Pa,contactRequestSent:Qa},{loadingMembers:sa,loadingFirstMessages:ta,loadingMessages:ua,sendingMessage:va,isBlocked:Ha,isContact:Ka,isFavourite:Ia,isMuted:Ja,loadingConfirmAction:La,inEditMode:Ma},{scrollToMessage:ra,selectedMessages:Na}],h=function(a){var b=[];for(var g in f)if(a.hasOwnProperty(g)){var h=a[g],i=f[g];b.push(h(c,d,e,i))}return b},i=h(g[0]);return i=i.concat(h(g[1])),a.when.apply(a,i).then(function(){for(var a=2;a