import {IConsoleFormatter} from "./IConsoleFormatter";
import {EOL} from "os";

export class TextConsoleFormatter implements IConsoleFormatter {
    format(object: any, oneLine: boolean = false, pick?: string[]): string {
        let result = "";

        if (typeof object === "string") {
            result += object;
        } else if (["number", "boolean", "function"].indexOf(typeof object) !== -1) {
            result += object.toString();
        } else if (Array.isArray(object) && object.length > 0) {
            if (oneLine && pick && pick.length > 0) {
                result += pick.join("\t") + EOL;
            }

            result += object.map(o => this.format(o, oneLine, pick)).join(EOL) + EOL;
        } else {
            let lf = !oneLine ? EOL : "\t";

            let keys = pick ? pick : Object.keys( object );
            for ( let key of keys ) {
                if (!object.hasOwnProperty(key)) {
                    continue;
                }

                // if pick array is provided and key is not in it skip
                if (pick && pick.length > 0 && pick.indexOf(key) === -1) {
                    continue;
                }

                result += `${!oneLine ? key + ":\t" : ""}${object[key]}${lf}`;
            }
        }
        return result;
    }
}