"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.PmacUnsolicitedBaseClient = void 0;
const event_1 = require("@f2/event");
class PmacUnsolicitedBaseClient {
    constructor(_sshClient, _buffers) {
        this._sshClient = _sshClient;
        this._buffers = _buffers;
        this._shells = [];
        this._maxMessages = 500;
        (0, event_1.beginEventEmitter)(this);
    }
    _connect() {
        return __awaiter(this, void 0, void 0, function* () {
            yield this._sshClient.connect();
            const isEnd = (data) => {
                return data.toString().indexOf('\n') !== -1 || data.toString().indexOf('\r') !== -1;
            };
            this._buffers.forEach((buffer) => {
                const shell = this._sshClient.shell();
                const task = shell.runTaskCmd(`/opt/ppmac/getsends/getsends -${buffer}`);
                let ready = false;
                task.onProgress((step) => {
                    let str;
                    step.is('data', (data) => {
                        str = data.toString();
                        if (ready) {
                            let messages = str.substring(0, str.length - 1).split('\n');
                            const length = messages.length;
                            if (length > this._maxMessages) {
                                messages = messages.slice(length - this._maxMessages - 1, length - 1);
                            }
                            messages.forEach((msg) => {
                                if (msg.charCodeAt(0) === 6) {
                                    return;
                                }
                                this.emit('message', msg.match(/^-[0-9]\:/) ? msg.substring(4) : msg, buffer);
                            });
                        }
                        else {
                            ready = isEnd(data);
                        }
                    });
                });
                shell.on('error', (err) => {
                    this.emit('error', err);
                });
                this._shells.push({
                    shell,
                    buffer
                });
            });
        });
    }
    _close() {
        this._sshClient.close();
    }
}
exports.PmacUnsolicitedBaseClient = PmacUnsolicitedBaseClient;
