"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const index_1 = require("../../index");
function sendParent(data) {
    process.send && process.send(data);
}
function Webhook(options, info) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!!options.url && options.url.length) {
            return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
                yield axios_1.default
                    .post(options.url, info)
                    .then(function (response) {
                    console.log(response);
                    resolve(response);
                })
                    .catch((err) => {
                    console.log(err);
                    resolve(err);
                });
            }));
        }
    });
}
(() => __awaiter(void 0, void 0, void 0, function* () {
    const args = process.argv.slice(2);
    const options = args[0];
    const objOptions = JSON.parse(options);
    const initWebpack = yield (0, index_1.initServer)(objOptions);
    initWebpack.on(index_1.onMode.interfaceChange, (change) => {
        Webhook(objOptions, change);
    });
    initWebpack.on(index_1.onMode.qrcode, (qrcode) => {
        Webhook(objOptions, qrcode);
    });
    initWebpack.on(index_1.onMode.connection, (conn) => __awaiter(void 0, void 0, void 0, function* () {
        if (conn.erro) {
            if (conn.statusFind === 'browserClosed' ||
                conn.statusFind === 'autoClose' ||
                conn.statusFind === 'noOpenWhatzapp' ||
                conn.statusFind === 'noOpenBrowser') {
                sendParent({ delsession: true, session: objOptions.session });
                if (!initWebpack.page.isClosed()) {
                    try {
                        initWebpack.page.close();
                    }
                    catch (_a) { }
                }
            }
        }
        if (conn.connect) {
            sendParent(Object.assign(Object.assign({}, conn), { session: objOptions.session }));
        }
        Webhook(objOptions, conn);
    }));
    process.on('message', (response) => __awaiter(void 0, void 0, void 0, function* () {
        Webhook(objOptions, response);
        if (response.type === 'text') {
            yield initWebpack
                .sendMessage({
                to: response.to,
                body: response.body,
                options: {
                    type: 'text',
                },
            })
                .then((result) => {
                sendParent(Object.assign({ result: true }, result));
            })
                .catch((error) => {
                sendParent(Object.assign({ result: true }, error));
            });
        }
    }));
}))();
//# sourceMappingURL=hydra.js.map