import { Transform, ScillaComponent } from 'scilla-components/src';

import { createTween, Entity } from 'scilla/src';

export const getTween = (context, val: Transform | Entity | ScillaComponent, obj, duration, ease?) => {
	return new Promise((r) => {
		let target: Transform;
		if (val instanceof Transform) {
			target = val;
		} else {
			target = val.getComponent(Transform)
		}
		const postionObj = {};
		for (const key in obj) {
			if (key == 'x' || key == 'y') {
				postionObj[key] = obj[key];
				delete obj[key];
			}
		}
		const scaleObj = {};
		for (const key in obj) {
			if (key == 'scalex') {
				scaleObj['x'] = obj[key];
				delete obj[key];
			}
			if (key == 'scaley') {
				scaleObj['y'] = obj[key];
				delete obj[key];
			}
		}
		createTween(context, target.position, true).to(postionObj, duration, ease);
		createTween(context, target.scale, true).to(scaleObj, duration, ease);
		createTween(context, target, true).to(obj, duration, ease).call(r);
	});
}