"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = require("debug");
const eventemitter2_1 = require("eventemitter2");
class SerializedAction {
}
class ProcessMetadata {
}
exports.ProcessMetadata = ProcessMetadata;
class WebsocketTransport extends eventemitter2_1.EventEmitter2 {
    constructor() {
        super(...arguments);
        this.initiated = false;
        this.logger = debug_1.default('axm:transport:websocket');
    }
    init(config) {
        if (this.initiated === true) {
            console.error(`Trying to re-init the transport, please avoid`);
            return this;
        }
        this.initiated = true;
        const AgentNode = require('@pm2/agent-node');
        this.logger('Init new transport service');
        this.config = config;
        this.process = {
            axm_actions: [],
            axm_options: {},
            axm_monitor: {}
        };
        this.agent = new AgentNode(this.config, this.process);
        if (this.agent instanceof Error) {
            throw this.agent;
        }
        this.agent.sendLogs = config.sendLogs || false;
        this.agent.start();
        this.agent.transport.on('**', (data) => {
            this.logger(`Received reverse message from websocket transport`);
            this.emit('data', data);
        });
        this.logger('Agent launched');
        return this;
    }
    setMetrics(metrics) {
        return this.process.axm_monitor = metrics.reduce((object, metric) => {
            if (typeof metric.name !== 'string')
                return object;
            object[metric.name] = {
                historic: metric.historic,
                unit: metric.unit,
                type: metric.id,
                value: metric.value
            };
            return object;
        }, {});
    }
    addAction(action) {
        this.logger(`Add action: ${action.name}:${action.type}`);
        const serializedAction = {
            action_name: action.name,
            action_type: action.type,
            arity: action.arity,
            opts: action.opts
        };
        this.process.axm_actions.push(serializedAction);
    }
    setOptions(options) {
        this.logger(`Set options: [${Object.keys(options).join(',')}]`);
        return this.process.axm_options = Object.assign(this.process.axm_options, options);
    }
    getFormattedPayload(channel, payload) {
        switch (channel) {
            case 'axm:reply':
                return { data: payload };
            case 'process:exception':
                return { data: payload };
            case 'human:event': {
                const name = payload.__name;
                payload.__name = undefined;
                return { name, data: payload };
            }
        }
        return payload;
    }
    send(channel, payload) {
        return this.agent.send(channel, this.getFormattedPayload(channel, payload)) ? 0 : -1;
    }
    destroy() {
        this.agent.transport.disconnect();
        this.logger('destroy');
    }
    removeListener() {
        return this.agent.transport.removeListener.apply(this, arguments);
    }
    removeAllListeners() {
        return this.agent.transport.removeAllListeners.apply(this, arguments);
    }
    on() {
        return this.agent.transport.on.apply(this, arguments);
    }
}
exports.WebsocketTransport = WebsocketTransport;
//# sourceMappingURL=data:application/json;base64,[encoded data, 4352 characters]