'use strict';

var d     = require('d')
  , trunc = require('es5-ext/math/trunc')

  , up, down, right, left
  , abs = Math.abs, floor = Math.floor, max = Math.max;

var getMove = function (control) {
	return function (num) {
		num = isNaN(num) ? 0 : max(floor(num), 0);
		return num ? ('\x1b[' + num + control) : '';
	};
};
module.exports = Object.defineProperties(function (x, y) {
	x = isNaN(x) ? 0 : floor(x);
	y = isNaN(y) ? 0 : floor(y);
	return ((x > 0) ? right(x) : left(-x)) + ((y > 0) ? down(y) : up(-y));
}, {
	up: d(up = getMove('A')),
	down: d(down = getMove('B')),
	right: d(right = getMove('C')),
	left: d(left = getMove('D')),
	to: d(function (x, y) {
		x = isNaN(x) ? 1 : (max(floor(x), 0) + 1);
		y = isNaN(y) ? 1 : (max(floor(y), 0) + 1);
		return '\x1b[' + y + ';' + x + 'H';
	}),
	lines: d(function (n) {
		var dir;
		n = trunc(n) || 0;
		dir = (n >= 0) ? 'E' : 'F';
		n = floor(abs(n));
		return '\x1b[' + n + dir;
	})
});
