<commit_before>import CoreLocation

public enum UploadTarget {
    case user(id: String)
    case group(id: String)
    
    var decoded: (userId: String?, groupId: String?) {
        switch self {
        case .user(let id):
            return (userId: id, groupId: nil)
        case .group(let id):
            return (userId: nil, groupId: id)
        }
    }
}

extension VK.Api {
    //Metods to upload Mediafiles
    public struct Upload {
        ///Methods to upload photo
        public struct Photo {
            ///Upload photo to user album
            public static func toAlbum(
                _ media: [Media],
                target: UploadTarget,
                albumId: String,
                caption: String = "",
                location: CLLocationCoordinate2D? = nil,
                config: Config = .default,
                uploadTimeout: TimeInterval = 30
                ) -> Request {
                
                return VK.Api.Photos.getUploadServer([
                    .albumId: albumId,
                    .userId: target.decoded.userId,
                    .groupId: target.decoded.groupId
                    ])
                    .request(with: config)
                    .next {
                        Request(
                            of: .upload(
                                url: $0["upload_url"].stringValue,
                                media: Array(media.prefix(5)
                                )
                            ),
                            config: config.mutatedWith(timeout: uploadTimeout)
                        )
                    }
                    .next {
                        VK.Api.Photos.save([
                            .albumId: albumId,
                            .userId: target.decoded.userId,
                            .groupId: target.decoded.groupId,
                            .server: $0["server"].stringValue,
                            .photosList: $0["photos_list"].stringValue,
                            .aid: $0["aid"].stringValue,
                            .hash: $0["hash"].stringValue,
                            .caption: caption,
                            .latitude: location?.latitude.toString(),
                            .longitude: location?.longitude.toString()
                            ]).request()
                }
            }
        }

//            ///Upload photo to message
//            public static func toMessage(_ media: Media) -> RequestConfig {
//                var getServerReq = Api.Photos.getMessagesUploadServer()
//
//                getServerReq.next {response -> RequestConfig in
//                    var uploadReq = RequestConfig(url: response["upload_url"].stringValue, media: [media])
//
//                    uploadReq.next {response -> RequestConfig in
//                        return Api.Photos.saveMessagesPhoto([
//                            .photo: response["photo"].stringValue,
//                            .server: response["server"].stringValue,
//                            .hash: response["hash"].stringValue
//                            ])
//                    }
//                    return uploadReq
//                }
//                return getServerReq
//            }
//
//            ///Upload photo to market
//            public static func toMarket(<commit_msg>Refactor upload photo to message method<commit_after>import CoreLocation

public enum UploadTarget {
    case user(id: String)
    case group(id: String)
    
    var decoded: (userId: String?, groupId: String?) {
        switch self {
        case .user(let id):
            return (userId: id, groupId: nil)
        case .group(let id):
            return (userId: nil, groupId: id)
        }
    }
}

extension VK.Api {
    //Metods to upload Mediafiles
    public struct Upload {
        ///Methods to upload photo
        public struct Photo {
            ///Upload photo to user album
            public static func toAlbum(
                _ media: [Media],
                target: UploadTarget,
                albumId: String,
                caption: String = "",
                location: CLLocationCoordinate2D? = nil,
                config: Config = .default,
                uploadTimeout: TimeInterval = 30
                ) -> Request {
                
                return VK.Api.Photos.getUploadServer([
                    .albumId: albumId,
                    .userId: target.decoded.userId,
                    .groupId: target.decoded.groupId
                    ])
                    .request(with: config)
                    .next {
                        Request(
                            of: .upload(
                                url: $0["upload_url"].stringValue,
                                media: Array(media.prefix(5)
                                )
                            ),
                            config: config.mutatedWith(timeout: uploadTimeout)
                        )
                    }
                    .next {
                        VK.Api.Photos.save([
                            .albumId: albumId,
                            .userId: target.decoded.userId,
                            .groupId: target.decoded.groupId,
                            .server: $0["server"].string,
                            .photosList: $0["photos_list"].string,
                            .aid: $0["aid"].string,
                            .hash: $0["hash"].string,
                            .caption: caption,
                            .latitude: location?.latitude.toString(),
                            .longitude: location?.longitude.toString()
                            ]).request()
                }
            }
        }
        
        ///Upload photo to message
        public static func toMessage(
            _ media: Media,
            config: Config = .default,
            uploadTimeout: TimeInterval = 30
            ) -> Request {
            
            return VK.Api.Photos.getMessagesUploadServer(.empty)
                .request(with: config)
                .next {
                    return VK.Api.Photos.saveMessagesPhoto([
                        .photo: $0["photo"].string,
                        .server: $0["server"].string,
                        .hash: $0["hash"].string
                        ])
                        .request(with: config.mutatedWith(timeout: uploadTimeout))
            }
        }
//
//            ///Upload photo to market
//            public static func toMarket(
{'fraction_non_alphanumeric': 0.09509295946357818, 'fraction_numerical': 0.003505028954587016, 'mean_word_length': 0.9492129492129492, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5193967', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1457, 'n_words': 337}