"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocialNetworkInstagramsMethods = void 0;
const abstract_method_1 = require("../abstract.method");
const bot_intagram_1 = require("../../utils/plugins/bot-intagram");
const request_promise_1 = require("request-promise");
class SocialNetworkInstagramsMethods extends abstract_method_1.AbstractMethods {
    constructor(model) {
        super(model);
    }
    async startProvider(body) {
        const ig = new bot_intagram_1.IgApiClient();
        ig.state.generateDevice(body.username);
        ig.request.end$.subscribe(async () => {
            const serialized = await ig.state.serialize();
            delete serialized.constants;
            await this.saveSession(body.username, { username: body.username, provider_id: body.provider_id, session: serialized });
        });
        const loggedInUser = await ig.account.login(body.username, body.password);
        process.nextTick(async () => await ig.simulate.postLoginFlow());
        return loggedInUser;
    }
    async saveSession(username, session) {
        try {
            return await this.model.updateOne({ username }, { username, session }, { upsert: true });
        }
        catch (error) {
            throw error;
        }
    }
    async loadSession(username) {
        try {
            const sessionData = await this.model.findOne({ username });
            if (sessionData) {
                const ig = new bot_intagram_1.IgApiClient();
                await ig.state.deserialize(JSON.stringify(sessionData.session.session));
                return ig;
            }
            else {
                throw 'SESSION_NOT_FOUND';
            }
        }
        catch (error) {
            throw error;
        }
    }
    async deleteSession(username) {
        try {
            return await this.model.deleteOne({ username });
        }
        catch (error) {
            throw error;
        }
    }
    async sendMessages(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    return new Promise((resol, rejc) => {
                        ctx.user.getIdByUsername(item.username).then((userId) => {
                            console.log(item.username, userId, item.message);
                            const thread = ctx.entity.directThread([userId.toString()]);
                            resol(thread.broadcastText(item.message));
                        }).catch((error) => {
                            console.log('error', error);
                            rejc(error);
                        });
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async sendPhotos(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const imageBuffer = await (0, request_promise_1.get)({
                    url: list[0].url,
                    encoding: null
                });
                const listPromises = list.map((item) => {
                    return new Promise((resol, rejc) => {
                        ctx.user.getIdByUsername(item.username).then((userId) => {
                            const thread = ctx.entity.directThread([userId.toString()]);
                            resol(thread.broadcastPhoto({ file: imageBuffer }));
                        }).catch((error) => {
                            console.log('error', error);
                            rejc(error);
                        });
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async sendVideos(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const videoBuffer = await (0, request_promise_1.get)({
                    url: list[0].url,
                    encoding: null
                });
                const listPromises = list.map((item) => {
                    return new Promise((resol, rejc) => {
                        ctx.user.getIdByUsername(item.username).then((userId) => {
                            const thread = ctx.entity.directThread([userId.toString()]);
                            resol(thread.broadcastVideo({ video: videoBuffer }));
                        }).catch((error) => {
                            console.log('error', error);
                            rejc(error);
                        });
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async sendVoices(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const audioBuffer = await (0, request_promise_1.get)({
                    url: list[0].url,
                    encoding: null
                });
                const listPromises = list.map((item) => {
                    return new Promise((resol, rejc) => {
                        ctx.user.getIdByUsername(item.username).then((userId) => {
                            const thread = ctx.entity.directThread([userId.toString()]);
                            resol(thread.broadcastVoice({ file: audioBuffer }));
                        }).catch((error) => {
                            console.log('error', error);
                            rejc(error);
                        });
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async sendStories(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const fileBuffer = await (0, request_promise_1.get)({
                    url: list[0].url,
                    encoding: null
                });
                let coverBuffer;
                if (list[0].url_cover) {
                    coverBuffer = await (0, request_promise_1.get)({
                        url: list[0].url_cover,
                        encoding: null
                    });
                }
                const listPromises = list.map((item) => {
                    return new Promise((resol, rejc) => {
                        if (!item.url_cover) {
                            ctx.user.getIdByUsername(item.username).then((userId) => {
                                const thread = ctx.entity.directThread([userId.toString()]);
                                resol(thread.broadcastStory({
                                    file: fileBuffer,
                                    viewMode: 'replayable',
                                    replyType: 'story'
                                }));
                            }).catch((error) => {
                                console.log('error', error);
                                rejc(error);
                            });
                        }
                        else {
                            ctx.user.getIdByUsername(item.username).then((userId) => {
                                const thread = ctx.entity.directThread([userId.toString()]);
                                resol(thread.broadcastStory({
                                    video: fileBuffer,
                                    coverImage: coverBuffer,
                                    replyType: 'story',
                                    viewMode: 'replayable',
                                }));
                            }).catch((error) => {
                                console.log('error', error);
                                rejc(error);
                            });
                        }
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async publishPhoto(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    return new Promise(async (resol, rejc) => {
                        try {
                            const imageBuffer = await (0, request_promise_1.get)({
                                url: item.url,
                                encoding: null
                            });
                            resol(ctx.publish.photo({
                                file: imageBuffer,
                                caption: item.caption,
                            }));
                        }
                        catch (error) {
                            rejc(error);
                        }
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async publishVideo(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    return new Promise(async (resol, rejc) => {
                        try {
                            const videoBuffer = await (0, request_promise_1.get)({
                                url: item.url,
                                encoding: null
                            });
                            let coverBuffer;
                            if (coverBuffer) {
                                coverBuffer = await (0, request_promise_1.get)({
                                    url: list[0].url_cover,
                                    encoding: null
                                });
                            }
                            resol(ctx.publish.video({
                                video: videoBuffer,
                                coverImage: coverBuffer,
                                caption: item.caption,
                            }));
                        }
                        catch (error) {
                            rejc(error);
                        }
                    }).catch((error) => {
                        reject(error);
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async publishAlbum(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    return new Promise(async (resol, rejc) => {
                        (0, request_promise_1.get)({
                            url: item.url,
                            encoding: null
                        }).then((fileBuffer) => {
                            if (item.url_cover) {
                                (0, request_promise_1.get)({
                                    url: item.url_cover,
                                    encoding: null
                                }).then((coverBuffer) => {
                                    resol({
                                        video: fileBuffer,
                                        coverImage: coverBuffer,
                                    });
                                }).catch((error) => {
                                    rejc(error);
                                });
                            }
                            else {
                                resol({
                                    file: fileBuffer
                                });
                            }
                        }).catch((error) => {
                            rejc(error);
                        });
                    }).catch((error) => {
                        reject(error);
                    });
                });
                const items = await Promise.allSettled(listPromises);
                console.log(items);
                const responses = await ctx.publish.album({
                    items: [...items].map((item) => item.value),
                    caption: list[0].caption,
                });
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async publishStory(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    return new Promise(async (resol, rejc) => {
                        const fileBuffer = await (0, request_promise_1.get)({
                            url: item.url,
                            encoding: null
                        });
                        try {
                            if (item.url_cover) {
                                const coverBuffer = await (0, request_promise_1.get)({
                                    url: item.url_cover,
                                    encoding: null
                                });
                                resol(ctx.publish.story({
                                    video: fileBuffer,
                                    coverImage: coverBuffer,
                                }));
                            }
                            else {
                                resol(ctx.publish.story({
                                    file: fileBuffer,
                                }));
                            }
                        }
                        catch (error) {
                            rejc(error);
                        }
                    }).catch((error) => {
                        reject(error);
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async sendLinks(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    return new Promise((resol, rejc) => {
                        ctx.user.getIdByUsername(item.username).then((userId) => {
                            console.log(item.username, userId, item.message);
                            const thread = ctx.entity.directThread([userId.toString()]);
                            resol(thread.broadcastLink(item.message));
                        }).catch((error) => {
                            console.log('error', error);
                            rejc(error);
                        });
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async sendProfiles(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    return new Promise((resol, rejc) => {
                        ctx.user.getIdByUsername(item.username).then((userId) => {
                            console.log(item.username, userId, item.message);
                            const thread = ctx.entity.directThread([userId.toString()]);
                            resol(thread.broadcastProfile(item.message));
                        }).catch((error) => {
                            console.log('error', error);
                            rejc(error);
                        });
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async sendReels(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    return new Promise((resol, rejc) => {
                        ctx.user.getIdByUsername(item.username).then((userId) => {
                            console.log(item.username, userId, item.message);
                            const thread = ctx.entity.directThread([userId.toString()]);
                            resol(thread.broadcastReel(item.message));
                        }).catch((error) => {
                            console.log('error', error);
                            rejc(error);
                        });
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async sendUserStories(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    return new Promise((resol, rejc) => {
                        ctx.user.getIdByUsername(item.username).then((userId) => {
                            console.log(item.username, userId, item.message);
                            const thread = ctx.entity.directThread([userId.toString()]);
                            resol(thread.broadcastUserStory(item.message));
                        }).catch((error) => {
                            console.log('error', error);
                            rejc(error);
                        });
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
    async sendPosts(ctx, list) {
        return new Promise(async (resolve, reject) => {
            try {
                const listPromises = list.map((item) => {
                    return new Promise((resol, rejc) => {
                        ctx.user.getIdByUsername(item.username).then((userId) => {
                            console.log(item.username, userId, item.message);
                            const thread = ctx.entity.directThread([userId.toString()]);
                            resol(thread.broadcastPost(item.message));
                        }).catch((error) => {
                            console.log('error', error);
                            rejc(error);
                        });
                    });
                });
                const responses = await Promise.allSettled(listPromises);
                setTimeout(() => {
                    resolve(responses);
                }, 500);
            }
            catch (error) {
                console.log('error 2', error);
                reject(error);
            }
        });
    }
}
exports.SocialNetworkInstagramsMethods = SocialNetworkInstagramsMethods;
