"use strict";

function midBranch(line) {
	return ` ├ ${line}`;
}

function midPipe(line) {
	return ` │ ${line}`;
}

function destinationBranch(line) {
	return ` └🠲 ${line}`;
}

function destinationPipe(line) {
	return `    ${line}`;
}

function item(text, branchFunc, pipeFunc) {
	let lines = text.split("\n");
	let firstLine = branchFunc(lines[0]);

	if (lines.length === 1) {
		return firstLine;
	} else {
		let otherLines = lines.slice(1).map((line) => pipeFunc(line));
		return [firstLine].concat(otherLines).join("\n");
	}
}

module.exports = function drawTransformTree(history) {
	let firstItem = history[0];
	let lastItem = item(history[history.length - 1], destinationBranch, destinationPipe);
	let otherItems;

	if (history.length > 2) {
		otherItems = history.slice(1, -1).map((historyItem) => {
			return item(historyItem, midBranch, midPipe);
		});
	} else {
		otherItems = [];
	}

	return [firstItem].concat(otherItems).concat([lastItem]).join("\n");
};
