define(["jquery","core/ajax","core/notification","core_message/message_drawer_view_conversation_constants"],function(a,b,c,d){var e=d.CONVERSATION_TYPES,f=function(a){"undefined"==typeof a.limit&&(a.limit=0),"undefined"==typeof a.offset&&(a.offset=0),"undefined"==typeof a.type&&(a.type=null),"undefined"==typeof a.favouritesonly&&(a.favouritesonly=!1),a.limitfrom=a.offset,a.limitnum=a.limit,delete a.limit,delete a.offset;var d={methodname:"core_message_data_for_messagearea_conversations",args:a},e=b.call([d])[0];return e.fail(c.exception),e},g=function(a){var d={methodname:"core_message_get_unread_conversations_count",args:a},e=b.call([d])[0];return e.fail(c.exception),e},h=function(a){var d={methodname:"core_message_mark_all_messages_as_read",args:a},e=b.call([d])[0];return e.fail(c.exception),e},i=function(a,c,d){var e={userid:a};"undefined"!=typeof c&&(e.limitnum=c),"undefined"!=typeof d&&(e.limitfrom=d);var f={methodname:"core_message_get_user_contacts",args:e};return b.call([f])[0]},j=function(a,c){var d={methodname:"core_message_data_for_messagearea_get_profile",args:{currentuserid:a,otheruserid:c}};return b.call([d])[0]},k=function(c,d){var e=[{methodname:"core_message_block_user",args:{userid:c,blockeduserid:d}},{methodname:"core_message_get_member_info",args:{referenceuserid:c,userids:[d],includecontactrequests:!0,includeprivacyinfo:!0}}];return a.when.apply(null,b.call(e)).then(function(a,b){return b.length?b[0]:{}})},l=function(c,d){var e=[{methodname:"core_message_unblock_user",args:{userid:c,unblockeduserid:d}},{methodname:"core_message_get_member_info",args:{referenceuserid:c,userids:[d],includecontactrequests:!0,includeprivacyinfo:!0}}];return a.when.apply(null,b.call(e)).then(function(a,b){return b.length?b[0]:{}})},m=function(a,c){var d={methodname:"core_message_create_contact_request",args:{userid:a,requesteduserid:c}};return b.call([d])[0]},n=function(c,d){var e=[{methodname:"core_message_delete_contacts",args:{userid:c,userids:d}},{methodname:"core_message_get_member_info",args:{referenceuserid:c,userids:d,includecontactrequests:!0,includeprivacyinfo:!0}}];return a.when.apply(null,b.call(e)).then(function(a,b){return b})},o=function(a,c,d,e,f,g){var h={currentuserid:a,convid:c,newest:!!f};"undefined"!=typeof d&&(h.limitnum=d),"undefined"!=typeof e&&(h.limitfrom=e),"undefined"!=typeof g&&(h.timefrom=g);var i={methodname:"core_message_get_conversation_messages",args:h};return b.call([i])[0]},p=function(a,c,d,e){var f={userid:a,search:c};"undefined"!=typeof d&&(f.limitnum=d),"undefined"!=typeof e&&(f.limitfrom=e);var g={methodname:"core_message_message_search_users",args:f};return b.call([g])[0]},q=function(a,c,d,e){var f={userid:a,search:c};"undefined"!=typeof d&&(f.limitnum=d),"undefined"!=typeof e&&(f.limitfrom=e);var g={methodname:"core_message_data_for_messagearea_search_messages",args:f};return b.call([g])[0]},r=function(a,c){var d=c.map(function(b){return{touserid:a,text:b}}),e={methodname:"core_message_send_instant_messages",args:{messages:d}};return b.call([e])[0].then(function(a){var b=a.reduce(function(a,b){return b.errormessage&&a.push(b.errormessage),a},[]);if(b.length)throw new Error(b.join("\n"));return a}).then(function(a){return a.map(function(a){return{id:a.msgid,text:a.text,timecreated:a.timecreated,useridfrom:a.useridfrom,conversationid:a.conversationid,candeletemessagesforallusers:a.candeletemessagesforallusers}})})},s=function(a,b){return r(a,[b]).then(function(a){return a[0]})},t=function(a,c){var d=c.map(function(a){return{text:a}}),e={methodname:"core_message_send_messages_to_conversation",args:{conversationid:a,messages:d}};return b.call([e])[0]},u=function(a,b){return t(a,[b]).then(function(a){return a[0]})},v=function(a,c){var d={methodname:"core_user_update_user_preferences",args:{userid:a,preferences:c}};return b.call([d])[0]},w=function(a){var c={methodname:"core_user_get_user_preferences",args:{userid:a}};return b.call([c])[0]},x=function(c,d){return a.when.apply(null,b.call(d.map(function(a){return{methodname:"core_message_delete_message",args:{messageid:a,userid:c}}})))},y=function(c,d){return a.when.apply(null,b.call(d.map(function(a){return{methodname:"core_message_delete_message_for_all_users",args:{messageid:a,userid:c}}})))},z=function(a,c){var d={methodname:"core_message_delete_conversations_by_id",args:{userid:a,conversationids:[c]}};return b.call([d])[0]},A=function(a){var c={methodname:"core_message_get_contact_requests",args:{userid:a}};return b.call([c])[0]},B=function(c,d){var e=[{methodname:"core_message_confirm_contact_request",args:{userid:c,requesteduserid:d}},{methodname:"core_message_get_member_info",args:{referenceuserid:d,userids:[c],includecontactrequests:!0,includeprivacyinfo:!0}}];return a.when.apply(null,b.call(e)).then(function(a,b){return b.length?b[0]:{}})},C=function(c,d){var e=[{methodname:"core_message_decline_contact_request",args:{userid:c,requesteduserid:d}},{methodname:"core_message_get_member_info",args:{referenceuserid:d,userids:[c],includecontactrequests:!0,includeprivacyinfo:!0}}];return a.when.apply(null,b.call(e)).then(function(a,b){return b.length?b[0]:{}})},D=function(a,c,d,e,f,g,h,i,j){var k={userid:a,conversationid:c};"undefined"!=typeof d&&null!==d&&(k.includecontactrequests=d),"undefined"!=typeof e&&null!==e&&(k.includeprivacyinfo=e),"undefined"!=typeof f&&null!==f&&(k.memberlimit=f),"undefined"!=typeof g&&null!==g&&(k.memberoffset=g),"undefined"!=typeof h&&null!==h&&(k.messagelimit=h),"undefined"!=typeof i&&null!==i&&(k.messageoffset=i),"undefined"!=typeof j&&null!==j&&(k.newestmessagesfirst=j);var l={methodname:"core_message_get_conversation",args:k};return b.call([l])[0]},E=function(a,c,d,e,f,g,h,i,j){var k={userid:a,otheruserid:c};"undefined"!=typeof d&&null!==d&&(k.includecontactrequests=d),"undefined"!=typeof e&&null!==e&&(k.includeprivacyinfo=e),"undefined"!=typeof f&&null!==f&&(k.memberlimit=f),"undefined"!=typeof g&&null!==g&&(k.memberoffset=g),"undefined"!=typeof h&&null!==h&&(k.messagelimit=h),"undefined"!=typeof i&&null!==i&&(k.messageoffset=i),"undefined"!=typeof j&&null!==j&&(k.newestmessagesfirst=j);var l={methodname:"core_message_get_conversation_between_users",args:k};return b.call([l])[0]},F=function(a,c,d,e){var f={userid:a};"undefined"!=typeof c&&null!==c&&(f.messagelimit=c),"undefined"!=typeof d&&null!==d&&(f.messageoffset=d),"undefined"!=typeof e&&null!==e&&(f.newestmessagesfirst=e);var g={methodname:"core_message_get_self_conversation",args:f};return b.call([g])[0]},G=function(a,c,d,f,g,h){var i={userid:a,type:c};"undefined"!=typeof d&&null!==d&&(i.limitnum=d),"undefined"!=typeof f&&null!==f&&(i.limitfrom=f),"undefined"!=typeof g&&null!==g&&(i.favourites=g),"undefined"!=typeof h&&null!==h&&(i.mergeself=h);var j={methodname:"core_message_get_conversations",args:i};return b.call([j])[0].then(function(a){return a.conversations.length&&(a.conversations=a.conversations.map(function(a){if(a.type==e.PRIVATE||a.type==e.SELF){var b=a.members.length?a.members[0]:null;b&&(a.name=a.name?a.name:b.fullname,a.imageurl=a.imageurl?a.imageurl:b.profileimageurl)}return a})),a})},H=function(a,c,d,e,f){var g={userid:c,conversationid:a};"undefined"!=typeof d&&null!==d&&(g.limitnum=d),"undefined"!=typeof e&&null!==e&&(g.limitfrom=e),"undefined"!=typeof f&&null!==f&&(g.includecontactrequests=f);var h={methodname:"core_message_get_conversation_members",args:g};return b.call([h])[0]},I=function(a,c){var d={methodname:"core_message_set_favourite_conversations",args:{userid:a,conversations:c}};return b.call([d])[0]},J=function(a,c){var d={methodname:"core_message_unset_favourite_conversations",args:{userid:a,conversations:c}};return b.call([d])[0]},K=function(a,c){var d={methodname:"core_message_mute_conversations",args:{userid:a,conversationids:c}};return b.call([d])[0]},L=function(a,c){var d={methodname:"core_message_unmute_conversations",args:{userid:a,conversationids:c}};return b.call([d])[0]},M=function(a,c,d,e){var f={referenceuserid:a,userids:c};"undefined"!=typeof d&&(f.includecontactrequests=d),"undefined"!=typeof e&&(f.includeprivacyinfo=e);var g={methodname:"core_message_get_member_info",args:f};return b.call([g])[0]},N=function(a,c){var d={methodname:"core_message_mark_all_conversation_messages_as_read",args:{userid:a,conversationid:c}};return b.call([d])[0]},O=function(a){var c={methodname:"core_message_get_user_message_preferences",args:{userid:a}};return b.call([c])[0]},P=function(a){var c={methodname:"core_message_get_conversation_counts",args:{userid:a}};return b.call([c])[0]},Q=function(a){var c={methodname:"core_message_get_unread_conversation_counts",args:{userid:a}};return b.call([c])[0]},R=function(c){var d=[{methodname:"core_message_get_conversation_counts",args:{userid:c}},{methodname:"core_message_get_unread_conversation_counts",args:{userid:c}}];return a.when.apply(null,b.call(d)).then(function(a,b){return{total:a,unread:b}})};return{query:f,countUnreadConversations:g,markAllAsRead:h,getContacts:i,getProfile:j,blockUser:k,unblockUser:l,createContactRequest:m,deleteContacts:n,getMessages:o,searchUsers:p,searchMessages:q,sendMessagesToUser:r,sendMessageToUser:s,sendMessagesToConversation:t,sendMessageToConversation:u,savePreferences:v,getPreferences:w,deleteMessages:x,deleteMessagesForAllUsers:y,deleteConversation:z,getContactRequests:A,acceptContactRequest:B,declineContactRequest:C,getConversation:D,getConversationBetweenUsers:E,getSelfConversation:F,getConversations:G,getConversationMembers:H,setFavouriteConversations:I,setMutedConversations:K,unsetFavouriteConversations:J,unsetMutedConversations:L,getMemberInfo:M,markAllConversationMessagesAsRead:N,getUserMessagePreferences:O,getTotalConversationCounts:P,getUnreadConversationCounts:Q,getAllConversationCounts:R}});