"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.scraping = void 0;
const qrcode = __importStar(require("qrcode-terminal"));
const callback_connect_layes_1 = require("./callback-connect.layes");
const enum_1 = require("../../model/enum");
const conn = new callback_connect_layes_1.CallbackConnection();
class scraping {
    constructor(page, browser, options) {
        this.page = page;
        this.browser = browser;
        this.options = options;
        this.autoCloseRemain = 0;
        this.startScanQrcode = false;
    }
    tryAutoClose() {
        if (this.options.timeAutoClose &&
            this.options.timeAutoClose > 0 &&
            !this.autoCloseInterval &&
            !this.page.isClosed()) {
            this.page.close().catch(() => { });
            this.browser.close().catch(() => { });
        }
    }
    cancelAutoClose() {
        clearInterval(this.autoCloseInterval);
        this.autoCloseInterval = null;
    }
    startAutoClose() {
        let remain = this.options.timeAutoClose ? this.options.timeAutoClose : false;
        if (this.options.timeAutoClose &&
            this.options.timeAutoClose > 0 &&
            !this.autoCloseInterval) {
            try {
                this.autoCloseInterval = setInterval(() => {
                    if (this.page.isClosed()) {
                        this.cancelAutoClose();
                        return;
                    }
                    if (Number.isInteger(remain)) {
                        remain -= 1000;
                        this.autoCloseRemain = Math.round(remain / 1000);
                        if (remain <= 0) {
                            conn.statusFind = {
                                erro: false,
                                text: 'Auto close called!',
                                statusFind: 'autoClose',
                                onType: enum_1.onMode.connection,
                            };
                            this.cancelAutoClose();
                            this.tryAutoClose();
                        }
                    }
                }, 1000);
            }
            catch (_a) { }
        }
    }
    qrCode() {
        return __awaiter(this, void 0, void 0, function* () {
            let click = yield this.page
                .evaluate(() => {
                const buttonReload = document.querySelector('button');
                if (buttonReload != null) {
                    buttonReload.click();
                    return true;
                }
                return false;
            })
                .catch();
            if (click) {
                yield this.page.waitForNavigation().catch();
            }
            const result = yield this.page
                .evaluate(() => {
                const selectorImg = document.querySelector('canvas');
                if (selectorImg) {
                    const selectorUrl = selectorImg.closest('[data-ref]');
                    const buttonReload = document.querySelector('button');
                    if (buttonReload === null &&
                        selectorImg != null &&
                        selectorUrl != null) {
                        let data = {
                            base64Image: selectorImg.toDataURL(),
                            urlCode: selectorUrl.getAttribute('data-ref'),
                        };
                        return data;
                    }
                    else {
                        return undefined;
                    }
                }
            })
                .catch();
            return result;
        });
    }
    asciiQr(code) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve) => {
                try {
                    qrcode.generate(code, { small: true }, (qrcode) => {
                        resolve(qrcode);
                    });
                }
                catch (e) { }
            });
        });
    }
}
exports.scraping = scraping;
//# sourceMappingURL=scraping.layes.js.map