"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const server_1 = require("@methodus/server");
const tail_1 = require("./tail");
const path = require("path");
const fs = require("fs");
const logsPath = process.env.NODE_LOG_DIR || './logs';
let SocketView = class SocketView {
    connect(socket) {
        return __awaiter(this, void 0, void 0, function* () {
            const logs = fs.readdirSync(logsPath);
            return new server_1.MethodResult(logs);
        });
    }
    endTail(socket, filename) {
        return __awaiter(this, void 0, void 0, function* () {
            let logPath = path.join(logsPath, filename);
            this.activeTails[logPath].close();
        });
    }
    tail(socket, filename) {
        return __awaiter(this, void 0, void 0, function* () {
            this.activeTails = this.activeTails || {};
            let logPath = path.join(logsPath, filename);
            this.activeTails[logPath] = new tail_1.Tail();
            this.activeTails[logPath].start(logPath, (data) => {
                //MethodEvent.emit('log', data, ServerType.Socket);
                socket.emit('log', data);
            });
        });
    }
};
__decorate([
    server_1.Method("GET" /* Get */, '/connect'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SocketView.prototype, "connect", null);
__decorate([
    server_1.Method("GET" /* Get */, '/end_tail'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], SocketView.prototype, "endTail", null);
__decorate([
    server_1.Method("GET" /* Get */, '/tail'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], SocketView.prototype, "tail", null);
SocketView = __decorate([
    server_1.MethodConfig('tail')
], SocketView);
exports.SocketView = SocketView;
//# sourceMappingURL=socketView.js.map