import { ActionAPI } from '../../ActionAPI';
import { EditorAPI } from '../../EditorAPI';
import { Getiyo } from './getiyo';

export function getiyoSimpleOpenEditor(EditorAPI: EditorAPI, properties) {
	var connectionID =
		properties.connectionID != undefined ? properties.connectionID : 'none';

	EditorAPI.setFields([
		{
			id: 'connectionID',
			name: 'Connection',
			type: 'connection',
			connectionType: 'channel',
			value: connectionID,
		},
	]);

	EditorAPI.onFieldChanges((fields) => {
		var fieldObject = EditorAPI.tools.objectifyFieldsValues(fields);
		EditorAPI.saveProperties({ connectionID: fieldObject.connectionID });
	});
}

export function getiyoSimpleExecute(
	ActionAPI: ActionAPI,
	properties,
	status: (text: string, type: string) => void,
	functionName: string
): Promise<any> {
	var connectionID =
		properties.connectionID != undefined ? properties.connectionID : 'none';
	if (connectionID != 'none') {
		var connection = ActionAPI.getConnection('channel', connectionID);
		if (connection) {
			var channel: Getiyo = connection.instance;

			return channel[functionName]();
		} else status(`Connection doesn't exist`, 'error');
	} else status('No connection specfied', 'error');
}
