import test from 'ava';
import sinon from 'sinon';
import {Reflect} from './fixtures/reflect';
import {bind as m} from '../../../angular2-polyfill/src/platform/utils/input';

const sandbox = sinon.sandbox.create();

const createTarget = (inputs) => {
	const ret = {
		__annotations__: {
			component: {}
		}
	};

	if (Array.isArray(inputs)) {
		ret.__annotations__.component.inputs = inputs;
	} else {
		ret.__annotations__.inputs = inputs;
	}

	return ret;
};

const bind = (inputs) => {
	const target = createTarget(inputs);
	const directive = {bindToController: {}};

	m(target, directive);

	return directive.bindToController;
};

test.before(() => {
	const has = sandbox.stub(Reflect, 'hasMetadata');
	has.withArgs('design:type', sinon.match.any, 'hello').returns(true);
	has.withArgs('design:type', sinon.match.any, 'world').returns(true);

	const get = sandbox.stub(Reflect, 'getMetadata');
	get.withArgs('design:type', sinon.match.any, 'hello').returns(String);
	get.withArgs('design:type', sinon.match.any, 'world').returns(Object);
});

test.after(() => {
	sandbox.restore();
});

test('inputs array', t => {
	t.deepEqual(bind(['foo']), {foo: '@foo'});
	t.deepEqual(bind(['foo', 'bar', 'baz']), {foo: '@foo', bar: '@bar', baz: '@baz'});
});

test('mapped inputs array', t => {
	t.deepEqual(bind(['foo:bar']), {foo: '@bar'});
	t.deepEqual(bind(['foo:bar', 'bar: baz']), {foo: '@bar', bar: '@baz'});
});

test('signed inputs array', t => {
	t.deepEqual(bind(['=foo']), {foo: '=foo'});
});

test('inputs annotation', t => {
	t.deepEqual(bind({hello: 'hello'}), {hello: '@hello'});
	t.deepEqual(bind({world: 'world'}), {world: '=world'});
});

test('mapped inputs annotation', t => {
	t.deepEqual(bind({hello: 'world'}), {hello: '@world'});
	t.deepEqual(bind({world: 'hello'}), {world: '=hello'});
});

test('signed inputs annotation', t => {
	t.deepEqual(bind({hello: '<'}), {hello: '<hello'});
	t.deepEqual(bind({world: '@world'}), {world: '@world'});
});

@@ -88,16 +88,54 @@
 flect';%0A
+import utils from './fixtures/utils';%0A
 import %7B
@@ -250,425 +250,49 @@
 ();%0A
-%0A
 const 
-createTarget = (inputs) =%3E %7B%0A%09const ret = %7B%0A%09%09__annotations__: %7B%0A%09%09%09component: %7B%7D%0A%09%09%7D%0A%09%7D;%0A%0A%09if (Array.isArray(inputs)) %7B%0A%09%09ret.__annotations__.component.inputs = inputs;%0A%09%7D else %7B%0A%09%09ret.__annotations__.inputs = inputs;%0A%09%7D%0A%0A%09return ret;%0A%7D;%0A%0Aconst bind = (inputs) =%3E %7B%0A%09const target = createTarget(inputs);%0A%09const directive = %7BbindToController: %7B%7D%7D;%0A%0A%09m(target, directive);%0A%0A%09return directive.bindToController;%0A%7D
+bind = utils.bind.bind(m, 'inputs')
 ;%0A%0At

refactor input tests