"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.MultilineController = void 0;
const MultilineView_1 = require("../views/MultilineView");
class MultilineController {
    constructor() {
        this._mode = false;
        this._delimiter = "";
        this._buffer = [];
        this._multilineView = new MultilineView_1.MultilineView();
    }
    get mode() {
        return this._mode;
    }
    get delimiter() {
        return this._delimiter;
    }
    get buffer() {
        return [...this._buffer];
    }
    setDelimiter(value) {
        this._delimiter = value;
    }
    setMode(value) {
        this._mode = value;
    }
    addToBuffer(input) {
        this._buffer.push(input);
    }
    clearBuffer() {
        this._buffer = [];
    }
    initialize(input, prescribedDelim = "") {
        const delimeter = prescribedDelim === "" ? input.split(" ")[0].slice(2) : prescribedDelim;
        const firstInput = input.split(" ").slice(1).join(" ");
        this._mode = true;
        this._delimiter = delimeter;
        // conditionally accept firstInput IF the not empty
        firstInput.length !== 0 && this.addToBuffer(firstInput);
    }
    returnBufferAndReset() {
        this._mode = false;
        this._delimiter = "";
        const nlBuffer = this._buffer.join("\n");
        // console.log({ receivedBuffer: this.buffer });
        this.clearBuffer();
        return nlBuffer;
    }
    handleMultilineInput(rl, input) {
        return __awaiter(this, void 0, void 0, function* () {
            // I want to see if the last char of input is " \"
            // if (input.slice(0, 2) === "<<" && !this.mode) {
            //   // Case: starting mode
            //   const delimeter = input.split(" ")[0].slice(2);
            //   const DEFAULT_DELIMETER = "eof";
            //   delimeter.length === 0
            //     ? this.initialize(input, DEFAULT_DELIMETER)
            //     : this.initialize(input);
            //   this._multilineView.renderStartHeredocMode(
            //     delimeter || DEFAULT_DELIMETER
            //   );
            if (input.endsWith("\\") && !this.mode) {
                // Case: starting mode
                const DEFAULT_DELIMETER = "eof";
                this.initialize(input.slice(0, input.length - 1), DEFAULT_DELIMETER);
                this._multilineView.renderStartHeredocMode(DEFAULT_DELIMETER);
            }
            else if (input === this.delimiter && this.mode) {
                // Case: ending mode
                this.setMode(false);
                this._multilineView.renderEndHeredocMode(this.delimiter);
            }
            else if (this.mode) {
                this.addToBuffer(input);
            }
        });
    }
}
exports.MultilineController = MultilineController;
