"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.init = void 0;
const child_process_1 = require("child_process");
const sessions_1 = require("../../help/sessions");
exports.init = new (class InicializePost {
    constructor() {
        this.child = '';
        this.res = '';
    }
    StartSession(req, res, option) {
        return __awaiter(this, void 0, void 0, function* () {
            const body = req.body;
            this.res = res;
            if (!!body.session &&
                body.session.length &&
                typeof body.session === 'string') {
                const session = yield sessions_1.sessionClient.newSession(body.session);
                if (session) {
                    option.session = body.session;
                    option.url = body.url;
                    const spawnArgs = [
                        __dirname + '../../../services/hydra.js',
                        JSON.stringify(option),
                    ];
                    this.child = (0, child_process_1.spawn)('node', spawnArgs, {
                        stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
                    });
                    this.child.on('message', (response) => __awaiter(this, void 0, void 0, function* () {
                        if (response.connect) {
                            yield sessions_1.sessionClient.addInfoSession(response.session, {
                                connect: response.connect,
                            });
                        }
                        if (response.delsession) {
                            yield sessions_1.sessionClient.deleteSession(response.session);
                        }
                    }));
                    this.child.on('disconnect', (err) => {
                        console.log('disconnect', err ? err : '');
                    });
                    this.child.on('error', (err) => __awaiter(this, void 0, void 0, function* () {
                        if (err) {
                            console.log('on error', err);
                        }
                    }));
                    this.child.on('close', (code) => __awaiter(this, void 0, void 0, function* () {
                        console.log(`child process exited with code`, code);
                    }));
                    this.child.on('uncaughtException', (err) => {
                        console.log(err);
                    });
                    sessions_1.sessionClient.addInfoSession(body.session, { child: this.child });
                    res.send({ erro: false, text: 'Wait for connection' });
                }
                else {
                    const getId = yield sessions_1.sessionClient.getSessionId(body.session);
                    const check = yield sessions_1.sessionClient.checkClient(body.session);
                    if (typeof getId === 'number') {
                        const client = yield sessions_1.sessionClient.checkObjectSession(body.session, 'connect', getId);
                        if (check && client) {
                            res.send({ erro: false, text: 'Successfully connected!' });
                        }
                        else {
                            res.send({ erro: false, text: 'Wait for connection' });
                        }
                    }
                }
            }
            else {
                res.send({
                    erro: true,
                    text: 'Need to inform the session',
                });
            }
        });
    }
    sendtext(req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const body = req.body;
            if (!!body.session &&
                body.session.length &&
                !!body.to &&
                body.to.length &&
                !!body.body &&
                body.body.length) {
                const check = yield sessions_1.sessionClient.checkClient(body.session);
                if (check) {
                    const getId = yield sessions_1.sessionClient.getSessionId(body.session);
                    if (typeof getId === 'number') {
                        const client = yield sessions_1.sessionClient.checkObjectSession(body.session, 'connect', getId);
                        if (client) {
                            const getUser = yield sessions_1.sessionClient.getUser(body.session);
                            getUser.child.send(Object.assign({ type: 'text' }, body));
                            this.child.on('message', (response) => {
                                if (response.result) {
                                    return res.send(response);
                                }
                            });
                        }
                    }
                }
                else {
                    res.send({
                        erro: true,
                        text: 'Not connected',
                    });
                }
            }
            else {
                res.send({
                    erro: true,
                    text: 'The parameters are missing',
                });
            }
        });
    }
})();
//# sourceMappingURL=post.js.map