"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.ListenerLayer = void 0;
const events_1 = require("events");
const enum_1 = require("../../model/enum/");
const scraping_layes_1 = require("./scraping.layes");
const help_1 = require("../../help");
class ListenerLayer extends scraping_layes_1.scraping {
    constructor(page, browser, options) {
        super(page, browser, options);
        this.page = page;
        this.browser = browser;
        this.options = options;
        this.listenerEmitter = new events_1.EventEmitter();
        this.statusFind = '';
    }
    qrCodeScan() {
        return __awaiter(this, void 0, void 0, function* () {
            let urlCode = null;
            this.startAutoClose();
            while (true) {
                const result = yield this.qrCode().catch();
                if (!(result === null || result === void 0 ? void 0 : result.urlCode)) {
                    break;
                }
                if (urlCode !== result.urlCode) {
                    Object.assign(result, { onType: enum_1.onMode.qrcode });
                    this.statusFind = result;
                    urlCode = result.urlCode;
                    const qr = yield this.asciiQr(urlCode).catch(() => undefined);
                    if (this.options.printQRInTerminal) {
                        console.log(qr);
                    }
                }
                yield (0, help_1.sleep)(100);
            }
        });
    }
    onChange(event) {
        return __awaiter(this, void 0, void 0, function* () {
            let change = null;
            while (true) {
                if (this.statusFind !== change) {
                    change = this.statusFind;
                    event && event(change);
                }
                yield (0, help_1.sleep)(100);
            }
        });
    }
    initModeInterfaceChange() {
        return __awaiter(this, void 0, void 0, function* () {
            this.on(enum_1.onMode.interfaceChange, (interFace) => __awaiter(this, void 0, void 0, function* () {
                try {
                    this.cancelAutoClose();
                    if (interFace.mode === 'MAIN') {
                        if (interFace.info === 'NORMAL') {
                            this.statusFind = {
                                erro: false,
                                connect: true,
                                onType: enum_1.onMode.connection,
                            };
                        }
                    }
                    if (interFace.mode === 'QR') {
                        if (interFace.info === 'NORMAL') {
                            this.statusFind = {
                                erro: false,
                                qrcode: interFace.info,
                                onType: enum_1.onMode.connection,
                            };
                            yield this.qrCodeScan();
                        }
                    }
                }
                catch (_a) { }
            }));
        });
    }
    initLitener() {
        return __awaiter(this, void 0, void 0, function* () {
            const functions = [...Object.values(enum_1.onMode)];
            for (const func of functions) {
                const has = yield this.page
                    .evaluate((func) => typeof window[func] === 'function', func)
                    .catch(() => false);
                if (!has) {
                    yield this.page
                        .exposeFunction(func, (...args) => this.listenerEmitter.emit(func, ...args))
                        .catch(() => { });
                }
            }
            yield this.page
                .evaluate(() => {
                if (!window.interfaceChange.exposed) {
                    window.API.interfaceChange((e) => {
                        window.interfaceChange(e);
                    });
                    window.interfaceChange.exposed = true;
                }
                if (!window.newMessage.exposed) {
                    window.API.newMessage((e) => {
                        window.newMessage(e);
                    });
                    window.interfaceChange.exposed = true;
                }
            })
                .catch(() => { });
        });
    }
    on(type, callback) {
        return __awaiter(this, void 0, void 0, function* () {
            switch (type) {
                case enum_1.onMode.interfaceChange:
                    this.listener(enum_1.onMode.interfaceChange, callback);
                    break;
                case enum_1.onMode.newMessage:
                    this.listener(enum_1.onMode.newMessage, callback);
                    break;
                case enum_1.onMode.qrcode:
                    this.onChange((event) => {
                        if (event.onType === enum_1.onMode.qrcode) {
                            callback(event);
                        }
                    });
                    break;
                case enum_1.onMode.connection:
                    this.onChange((event) => {
                        if (event.onType === enum_1.onMode.connection) {
                            callback(event);
                        }
                    });
                    break;
            }
        });
    }
    listener(type, callback) {
        this.listenerEmitter.on(type, callback);
        return {
            dispose: () => {
                this.listenerEmitter.off(type, callback);
            },
        };
    }
}
exports.ListenerLayer = ListenerLayer;
//# sourceMappingURL=listener.layes.js.map