//
//  MBIMConversationListOperation.m
//  kordophoned
//
//  Created by James Magahern on 11/13/18.
//  Copyright © 2018 James Magahern. All rights reserved.
//

#import "MBIMConversationListOperation.h"
#import "MBIMHTTPUtilities.h"
#import "IMChat+Encoded.h"

#import "IMCore_ClassDump.h"

@implementation MBIMConversationListOperation

+ (void)load { [super load]; }

+ (NSString *)endpointName
{
    return @"conversations";
}

- (void)main
{
    __block NSMutableArray *conversations = [NSMutableArray array];
    
    dispatch_sync([[self class] sharedIMAccessQueue], ^{
        NSArray<IMChat *> *chats = [[IMChatRegistry sharedInstance] allExistingChats];
        for (IMChat *chat in chats) {
            NSDictionary *chatDict = [chat mbim_dictionaryRepresentation];
            [conversations addObject:chatDict];
        }
    });
    
    MBIMJSONDataResponse *response = [MBIMJSONDataResponse responseWithJSONObject:conversations];
    self.serverCompletionBlock(response);
}

@end
