"use strict";
/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileOutput = exports.BufferedOutput = exports.OutputMultiplexer = void 0;
const fs_1 = __importDefault(require("fs"));
class OutputMultiplexer {
    constructor(outputs) {
        this._enabled = true;
        this._outputs = outputs;
    }
    setEnabled(enabled) {
        this._enabled = enabled;
    }
    printLn(text) {
        if (!this._enabled)
            return;
        for (const output of this._outputs)
            output.printLn(text);
    }
    popLn(text) {
        if (!this._enabled)
            return;
        for (const output of this._outputs)
            output.popLn(text);
    }
    flush() {
        if (!this._enabled)
            return;
        for (const output of this._outputs)
            output.flush();
    }
}
exports.OutputMultiplexer = OutputMultiplexer;
class BufferedOutput {
    constructor(onUpdate = () => { }) {
        this._lines = [];
        this._buffer = null;
        this._onUpdate = onUpdate;
    }
    printLn(text) {
        this._buffer = null;
        this._lines.push(...text.trimEnd().split('\n'));
        this._onUpdate(this.buffer());
    }
    popLn(text) {
        this._buffer = null;
        this._lines.length -= text.trimEnd().split('\n').length;
    }
    buffer() {
        if (this._buffer === null)
            this._buffer = this._lines.join('\n');
        return this._buffer;
    }
    clear() {
        this._lines = [];
        this._buffer = null;
        this._onUpdate(this.buffer());
    }
    flush() {
    }
}
exports.BufferedOutput = BufferedOutput;
class FileOutput extends BufferedOutput {
    constructor(fileName) {
        super();
        this._fileName = fileName;
        process.on('exit', () => this.flush());
    }
    flush() {
        fs_1.default.writeFileSync(this._fileName, this.buffer());
    }
}
exports.FileOutput = FileOutput;
//# sourceMappingURL=outputs.js.map