"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 });
exports.getPathChrome = exports.oneTab = exports.initLaunch = exports.PathSession = exports.initBrowser = void 0;
const puppeteer_extra_1 = __importDefault(require("puppeteer-extra"));
const ChromeLauncher = __importStar(require("chrome-launcher"));
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const help_1 = require("../help");
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
function initBrowser(Browser) {
    return __awaiter(this, void 0, void 0, function* () {
        const wpage = yield oneTab(Browser);
        if (wpage) {
            try {
                yield wpage.setUserAgent(help_1.puppeteerConfig.useragentOverride);
                yield wpage.goto(help_1.puppeteerConfig.whatsappUrl, {
                    waitUntil: 'domcontentloaded',
                });
                Browser.userAgent();
                return wpage;
            }
            catch (_a) {
                return false;
            }
        }
        return false;
    });
}
exports.initBrowser = initBrowser;
function PathSession(options) {
    options.mkdirFolderToken = options.mkdirFolderToken
        ? options.mkdirFolderToken
        : '';
    options.pathNameToken = options.pathNameToken ? options.pathNameToken : '';
    options.session = options.session ? options.session : '';
    const folderNameToken = path.join(path.resolve(process.cwd(), options.mkdirFolderToken, options.pathNameToken));
    if (!fs.existsSync(folderNameToken)) {
        fs.mkdirSync(folderNameToken, {
            recursive: true,
        });
    }
    const pathSession = path.join(path.resolve(process.cwd(), options.mkdirFolderToken, options.pathNameToken, options.session));
    fs.chmodSync(folderNameToken, '777');
    if (options && options.puppeteerOptions) {
        options.puppeteerOptions.userDataDir = pathSession;
    }
    help_1.puppeteerConfig.chromiumArgs.push(`--user-data-dir=${pathSession}`);
}
exports.PathSession = PathSession;
function initLaunch(options) {
    var _a, _b, _c, _d, _e;
    return __awaiter(this, void 0, void 0, function* () {
        PathSession(options);
        if (((_a = options.puppeteerOptions) === null || _a === void 0 ? void 0 : _a.executablePath) === 'useChrome') {
            const chromePath = getPathChrome();
            if (chromePath === undefined) {
                console.log('erro get Path chrome');
            }
            else {
                options.puppeteerOptions.executablePath = chromePath;
            }
        }
        try {
            puppeteer_extra_1.default.use(StealthPlugin());
            return yield puppeteer_extra_1.default.launch({
                headless: (_b = options.puppeteerOptions) === null || _b === void 0 ? void 0 : _b.headless,
                args: (_c = options.puppeteerOptions) === null || _c === void 0 ? void 0 : _c.args,
                executablePath: (_d = options.puppeteerOptions) === null || _d === void 0 ? void 0 : _d.executablePath,
                userDataDir: (_e = options.puppeteerOptions) === null || _e === void 0 ? void 0 : _e.userDataDir,
            });
        }
        catch (e) {
            return false;
        }
    });
}
exports.initLaunch = initLaunch;
function oneTab(Browser) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const page = yield Browser.pages();
            if (page.length)
                return page[0];
            return yield Browser.newPage();
        }
        catch (_a) {
            return false;
        }
    });
}
exports.oneTab = oneTab;
function getPathChrome() {
    try {
        const chromeInstalations = ChromeLauncher.Launcher.getInstallations();
        return chromeInstalations[0];
    }
    catch (_a) {
        return undefined;
    }
}
exports.getPathChrome = getPathChrome;
//# sourceMappingURL=browser.js.map