import { ConnectionManager_Connection } from '../ConnectionManager';
import { Undecked } from '../Core';
import { Deck } from '../Decks/Deck';
import { Log } from '../Logger';
import { EditorAPI } from './EditorAPI';

declare var Undecked: Undecked;
export class ActionAPI {
	private _integrationID: string;
	_openEditor: (editorAPI: EditorAPI, properties: ActionAPI_Properties) => void;
	_hdl: (properties: ActionAPI_Properties, status: (text: string) => void, deck: Deck) => void;

	constructor(settings: ActionAPI_Settings) {
		this._integrationID = settings.integrationID;
	}

	/**
	 * @deprecated
	 * Replaced by 'OnExecute'
	 */
	handle(
		callback: (
			properties: ActionAPI_Properties | any,
			status: (text: string, type?: ActionAPI_StatusTypes) => void,
			deck: Deck
		) => void
	) {
		if (this._hdl == undefined) {
			this._hdl = callback;
		} else Log('error', `Unable to register two 'handle' events`);
	}

	onExecute(callback: (
		properties: ActionAPI_Properties | any,
		status: (text: string, type?: ActionAPI_StatusTypes) => void,
		deck: Deck
	) => void
	) {
		if (this._hdl == undefined) {
			this._hdl = callback;
		} else Log('error', `Unable to register two 'onExecute' (Used to be 'handle') events`);
	}

	onOpenEditor(callback: (editorAPI: EditorAPI, properties: ActionAPI_Properties | any) => void) {
		if (this._openEditor == undefined) {
			this._openEditor = callback;
		} else Log('error', `Unable to register two 'onOpenEditor' events`);
	}

	getConnection(connectionType: string, connectionID: string): ConnectionManager_Connection {
		return Undecked.Connections.getConnection(this._integrationID, connectionType, connectionID);
	}
}

export type ActionAPI_StatusTypes = 'info' | 'error' | 'warn';
export interface ActionAPI_Settings {
	integrationID: string;
}

export interface ActionAPI_Properties {
	[propertyID: string]: any;
}
