﻿// Classes
export class TextBuilder
{
    // By default all attributes and methods are public

    // Attributes
    private _text: string[];

    // Constructor
    // Only one constructor allowed
    // Note: No return type for a constructor
    // Can overload constructor signature.
    constructor()
    constructor(initialText: string)
    constructor(initialText?: string)  // Basic signature (optional initialText)
    {
        this._text = [];            // Must create array before using it!!

        if (initialText !== undefined)
        {
            this._text[0] = initialText;
        }
    }

    // Properties
    // Get and Set Accessors
    get Text(): string[]
    {
        return this._text;
    }

    set Text(text: string[])
    {
        this._text = text;
    }

    get Result(): string
    {
        return this.ReadText();
    }

    // Methods
    // Note: You can't overload method names
    // each method must have a unique name.
    // However you can overload the signature.
    AppendText(): void
    AppendText(num: number): void
    AppendText(text: string): void
    AppendText(text: string[]): void
    AppendText(text: TextBuilder): void
    AppendText(val?: any): void     // Accept number or string
    {
        if (val instanceof Array)
        {
            val = val.join(" ");
        }
        else if (val instanceof TextBuilder)
        {
            val = val._text;
        }
        else if (typeof val === "number")
        {
            val = val + ""; // Convert number to text
        }
        //else if (typeof val === "boolean")
        //else if (typeof val === "string")
        this._text.push(val);  // Append to end
    }

    Undo(): void
    {
        this._text.pop();       // Remove last
    }

    Reset(): void
    {
        this._text.length = 0;  // Clear
    }

    private ReadText(): string
    {
        let output: string = "";

        for (let i in this._text)
        {
            output = output + this._text[i];
        }
        return output;
    }
} 