"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.initServer = void 0;
const interface_1 = require("../model/interface");
const browser_1 = require("./browser");
const webpack_1 = require("../inject/webpack");
const callback_connect_layes_1 = require("./layes/callback-connect.layes");
const enum_1 = require("../model/enum");
const help_1 = require("../help");
const check_up_to_date_1 = require("./check-up-to-date");
const conn = new callback_connect_layes_1.CallbackConnection();
function initServer(options) {
    return __awaiter(this, void 0, void 0, function* () {
        return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
            const mergeOptionsDefault = Object.assign(Object.assign({}, interface_1.defaultConfig), options);
            if (!!(options === null || options === void 0 ? void 0 : options.puppeteerOptions)) {
                mergeOptionsDefault.puppeteerOptions = Object.assign(Object.assign({}, interface_1.defaultConfig.puppeteerOptions), options.puppeteerOptions);
            }
            if (mergeOptionsDefault.updatesLog) {
                yield (0, check_up_to_date_1.checkUpdates)();
            }
            const wpage = yield (0, browser_1.initLaunch)(mergeOptionsDefault);
            if (typeof wpage !== 'boolean') {
                const page = yield (0, browser_1.initBrowser)(wpage);
                if (typeof page !== 'boolean') {
                    const client = new webpack_1.webPack(page, wpage, mergeOptionsDefault);
                    (0, help_1.checkingCloses)(wpage, () => {
                        client.statusFind = {
                            erro: true,
                            text: 'The browser has closed',
                            statusFind: 'browserClosed',
                            onType: enum_1.onMode.connection,
                        };
                    }).catch(() => {
                        console.log('The client has been closed');
                    });
                    return resolve(client);
                }
                else {
                    resolve(conn);
                    conn.statusFind = {
                        erro: true,
                        text: 'Error open whatzapp',
                        statusFind: 'noOpenWhatzapp',
                        onType: enum_1.onMode.connection,
                    };
                    wpage.close();
                }
            }
            else {
                resolve(conn);
                conn.statusFind = {
                    erro: true,
                    text: 'Error open browser...',
                    statusFind: 'noOpenBrowser',
                    onType: enum_1.onMode.connection,
                };
            }
        }));
    });
}
exports.initServer = initServer;
//# sourceMappingURL=init.js.map