keep keep keep keep replace keep keep keep keep keep

/* eslint-disable @typescript-eslint/prefer-optional-chain */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-labels */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable no-continue */
/* eslint-disable no-prototype-builtins */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-param-reassign */
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
import PCancelable from 'p-cancelable';

import type {
	ExecutionError,
	ExecutionStatus,
	IConnection,
	IDataObject,
	IExecuteData,
	INode,
	INodeConnections,
	INodeExecutionData,
	IPinData,
	IRun,
	IRunData,
	IRunExecutionData,
	ISourceData,
	ITaskData,
	ITaskDataConnections,
	ITaskDataConnectionsSource,
	IWaitingForExecution,
	IWaitingForExecutionSource,
	IWorkflowExecuteAdditionalData,
	NodeApiError,
	NodeOperationError,
	Workflow,
	WorkflowExecuteMode,
} from 'n8n-workflow';
import { LoggerProxy as Logger, WorkflowOperationError } from 'n8n-workflow';
import get from 'lodash.get';
import * as NodeExecuteFunctions from './NodeExecuteFunctions';

export class WorkflowExecute {
	runExecutionData: IRunExecutionData;

	private additionalData: IWorkflowExecuteAdditionalData;

	private mode: WorkflowExecuteMode;

	private status: ExecutionStatus;

	constructor(
		additionalData: IWorkflowExecuteAdditionalData,
		mode: WorkflowExecuteMode,
		runExecutionData?: IRunExecutionData,
	) {
		this.additionalData = additionalData;
		this.mode = mode;
		this.status = 'new';
		this.runExecutionData = runExecutionData || {
			startData: {},
			resultData: {
				runData: {},
				pinData: {},
			},
			executionData: {
				contextData: {},
				nodeExecutionStack: [],
				waitingExecution: {},
				waitingExecutionSource: {},
			},
		};
	}

	/**
	 * Executes the given workflow.
	 *
	 * @param {Workflow} workflow The workflow to execute
	 * @param {INode[]} [startNodes] Node to start execution from
	 * @param {string} [destinationNode] Node to stop execution at
	 */
	// IMPORTANT: Do not add "async" to this function, it will then convert the
	//            PCancelable to a regular Promise and does so not allow canceling
	//            active executions anymore
	// eslint-disable-next-line @typescript-eslint/promise-function-async
	run(
		workflow: Workflow,
		startNode?: INode,
		destinationNode?: string,
		pinData?: IPinData,
	): PCancelable<IRun> {
		this.status = 'running';

		// Get the nodes to start workflow execution from
		startNode = startNode || workflow.getStartNode(destinationNode);

		if (startNode === undefined) {
			throw new Error('No node to start the workflow from could be found!');
		}

		// If a destination node is given we only run the direct parent nodes and no others
		let runNodeFilter: string[] | undefined;
		if (destinationNode) {
			runNodeFilter = workflow.getParentNodes(destinationNode);
			runNodeFilter.push(destinationNode);
		}

		// Initialize the data of the start nodes
		const nodeExecutionStack: IExecuteData[] = [
			{
				node: startNode,
				data: {
					main: [
						[
							{
								json: {},
							},
						],
					],
				},
				source: null,
			},
		];

		this.runExecutionData = {
			startData: {
				destinationNode,
				runNodeFilter,
			},
			resultData: {
				runData: {},
				pinData,
			},
			executionData: {
				contextData: {},
				nodeExecutionStack,
				waitingExecution: {},
				waitingExecutionSource: {},
			},
		};

		return this.processRunExecutionData(workflow);
	}

	/**
	 * Executes the given workflow but only
	 *
	 * @param {Workflow} workflow The workflow to execute
	 * @param {string[]} startNodes Nodes to start execution from
	 * @param {string} destinationNode Node to stop execution at
	 */
	// IMPORTANT: Do not add "async" to this function, it will then convert the
	//            PCancelable to a regular Promise and does so not allow canceling
	//            active executions anymore
	// eslint-disable-next-line @typescript-eslint/promise-function-async
	runPartialWorkflow(
		workflow: Workflow,
		runData: IRunData,
		startNodes: string[],
		destinationNode: string,
		pinData?: IPinData,
	): PCancelable<IRun> {
		let incomingNodeConnections: INodeConnections | undefined;
		let connection: IConnection;

		this.status = 'running';

		const runIndex = 0;

		// Initialize the nodeExecutionStack and waitingExecution with
		// the data from runData
		const nodeExecutionStack: IExecuteData[] = [];
		const waitingExecution: IWaitingForExecution = {};
		const waitingExecutionSource: IWaitingForExecutionSource = {};
		for (const startNode of startNodes) {
			incomingNodeConnections = workflow.connectionsByDestinationNode[startNode];

			const incomingData: INodeExecutionData[][] = [];
			let incomingSourceData: ITaskDataConnectionsSource | null = null;

			if (incomingNodeConnections === undefined) {
				// If it has no incoming data add the default empty data
				incomingData.push([
					{
						json: {},
					},
				]);
			} else {
				// Get the data of the incoming connections
				incomingSourceData = { main: [] };
				for (const connections of incomingNodeConnections.main) {
					for (let inputIndex = 0; inputIndex < connections.length; inputIndex++) {
						connection = connections[inputIndex];

						const node = workflow.getNode(connection.node);

						if (node?.disabled) continue;

						if (node && pinData && pinData[node.name]) {
							incomingData.push(pinData[node.name]);
						} else {
							incomingData.push(
								// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
								runData[connection.node][runIndex].data![connection.type][connection.index]!,
							);
						}

						incomingSourceData.main.push({
							previousNode: connection.node,
						});
					}
				}
			}

			const executeData: IExecuteData = {
				node: workflow.getNode(startNode) as INode,
				data: {
					main: incomingData,
				},
				source: incomingSourceData,
			};

			nodeExecutionStack.push(executeData);

			// Check if the destinationNode has to be added as waiting
			// because some input data is already fully available
			incomingNodeConnections = workflow.connectionsByDestinationNode[destinationNode];
			if (incomingNodeConnections !== undefined) {
				for (const connections of incomingNodeConnections.main) {
					for (let inputIndex = 0; inputIndex < connections.length; inputIndex++) {
						connection = connections[inputIndex];

						if (waitingExecution[destinationNode] === undefined) {
							waitingExecution[destinationNode] = {};
							waitingExecutionSource[destinationNode] = {};
						}
						if (waitingExecution[destinationNode][runIndex] === undefined) {
							waitingExecution[destinationNode][runIndex] = {};
							waitingExecutionSource[destinationNode][runIndex] = {};
						}
						if (waitingExecution[destinationNode][runIndex][connection.type] === undefined) {
							waitingExecution[destinationNode][runIndex][connection.type] = [];
							waitingExecutionSource[destinationNode][runIndex][connection.type] = [];
						}

						if (runData[connection.node] !== undefined) {
							// Input data exists so add as waiting
							// incomingDataDestination.push(runData[connection.node!][runIndex].data![connection.type][connection.index]);
							waitingExecution[destinationNode][runIndex][connection.type].push(
								runData[connection.node][runIndex].data![connection.type][connection.index],
							);
							waitingExecutionSource[destinationNode][runIndex][connection.type].push({
								previousNode: connection.node,
								previousNodeOutput: connection.index || undefined,
								previousNodeRun: runIndex || undefined,
							} as ISourceData);
						} else {
							waitingExecution[destinationNode][runIndex][connection.type].push(null);
							waitingExecutionSource[destinationNode][runIndex][connection.type].push(null);
						}
					}
				}
			}
		}

		// Only run the parent nodes and no others
		let runNodeFilter: string[] | undefined;
		// eslint-disable-next-line prefer-const
		runNodeFilter = workflow
			.getParentNodes(destinationNode)
			.filter((parentNodeName) => !workflow.getNode(parentNodeName)?.disabled);

		runNodeFilter.push(destinationNode);

		this.runExecutionData = {
			startData: {
				destinationNode,
				runNodeFilter,
			},
			resultData: {
				runData,
				pinData,
			},
			executionData: {
				contextData: {},
				nodeExecutionStack,
				waitingExecution,
				waitingExecutionSource,
			},
		};

		return this.processRunExecutionData(workflow);
	}

	/**
	 * Executes the hook with the given name
	 *
	 */
	// eslint-disable-next-line @typescript-eslint/no-explicit-any
	async executeHook(hookName: string, parameters: any[]): Promise<void> {
		if (this.additionalData.hooks === undefined) {
			return;
		}

		// eslint-disable-next-line consistent-return
		return this.additionalData.hooks.executeHookFunctions(hookName, parameters);
	}

	/**
	 * Checks the incoming connection does not receive any data
	 */
	incomingConnectionIsEmpty(
		runData: IRunData,
		inputConnections: IConnection[],
		runIndex: number,
	): boolean {
		// for (const inputConnection of workflow.connectionsByDestinationNode[nodeToAdd].main[0]) {
		for (const inputConnection of inputConnections) {
			const nodeIncomingData = get(
				runData,
				`[${inputConnection.node}][${runIndex}].data.main[${inputConnection.index}]`,
			);
			if (nodeIncomingData !== undefined && (nodeIncomingData as object[]).length !== 0) {
				return false;
			}
		}
		return true;
	}

	addNodeToBeExecuted(
		workflow: Workflow,
		connectionData: IConnection,
		outputIndex: number,
		parentNodeName: string,
		nodeSuccessData: INodeExecutionData[][],
		runIndex: number,
	): void {
		let stillDataMissing = false;

		// Check if node has multiple inputs as then we have to wait for all input data
		// to be present before we can add it to the node-execution-stack
		if (workflow.connectionsByDestinationNode[connectionData.node].main.length > 1) {
			// Node has multiple inputs
			let nodeWasWaiting = true;

			if (!this.runExecutionData.executionData!.waitingExecutionSource) {
				this.runExecutionData.executionData!.waitingExecutionSource = {};
			}

			// Check if there is already data for the node
			if (
				this.runExecutionData.executionData!.waitingExecution[connectionData.node] === undefined
			) {
				// Node does not have data yet so create a new empty one
				this.runExecutionData.executionData!.waitingExecution[connectionData.node] = {};
				this.runExecutionData.executionData!.waitingExecutionSource[connectionData.node] = {};
				nodeWasWaiting = false;
			}
			if (
				this.runExecutionData.executionData!.waitingExecution[connectionData.node][runIndex] ===
				undefined
			) {
				// Node does not have data for runIndex yet so create also empty one and init it
				this.runExecutionData.executionData!.waitingExecution[connectionData.node][runIndex] = {
					main: [],
				};
				this.runExecutionData.executionData!.waitingExecutionSource[connectionData.node][runIndex] =
					{
						main: [],
					};
				for (
					let i = 0;
					i < workflow.connectionsByDestinationNode[connectionData.node].main.length;
					i++
				) {
					this.runExecutionData.executionData!.waitingExecution[connectionData.node][
						runIndex
					].main.push(null);

					this.runExecutionData.executionData!.waitingExecutionSource[connectionData.node][
						runIndex
					].main.push(null);
				}
			}

			// Add the new data
			if (nodeSuccessData === null) {
				this.runExecutionData.executionData!.waitingExecution[connectionData.node][runIndex].main[
					connectionData.index
				] = null;
				this.runExecutionData.executionData!.waitingExecutionSource[connectionData.node][
					runIndex
				].main[connectionData.index] = null;
			} else {
				this.runExecutionData.executionData!.waitingExecution[connectionData.node][runIndex].main[
					connectionData.index
				] = nodeSuccessData[outputIndex];
				this.runExecutionData.executionData!.waitingExecutionSource[connectionData.node][
					runIndex
				].main[connectionData.index] = {
					previousNode: parentNodeName,
					previousNodeOutput: outputIndex || undefined,
					previousNodeRun: runIndex || undefined,
				};
			}

			// Check if all data exists now
			let thisExecutionData: INodeExecutionData[] | null;
			let allDataFound = true;
			for (
				let i = 0;
				i <
				this.runExecutionData.executionData!.waitingExecution[connectionData.node][runIndex].main
					.length;
				i++
			) {
				thisExecutionData =
					this.runExecutionData.executionData!.waitingExecution[connectionData.node][runIndex].main[
						i
					];
				if (thisExecutionData === null) {
					allDataFound = false;
					break;
				}
			}

			if (allDataFound) {
				// All data exists for node to be executed
				// So add it to the execution stack

				const executionStackItem = {
					node: workflow.nodes[connectionData.node],
					data: this.runExecutionData.executionData!.waitingExecution[connectionData.node][
						runIndex
					],
					source:
						this.runExecutionData.executionData!.waitingExecutionSource[connectionData.node][
							runIndex
						],
				} as IExecuteData;

				if (
					this.runExecutionData.executionData!.waitingExecutionSource !== null &&
					this.runExecutionData.executionData!.waitingExecutionSource !== undefined
				) {
					executionStackItem.source =
						this.runExecutionData.executionData!.waitingExecutionSource[connectionData.node][
							runIndex
						];
				}

				this.runExecutionData.executionData!.nodeExecutionStack.push(executionStackItem);

				// Remove the data from waiting
				delete this.runExecutionData.executionData!.waitingExecution[connectionData.node][runIndex];
				delete this.runExecutionData.executionData!.waitingExecutionSource[connectionData.node][
					runIndex
				];

				if (
					Object.keys(this.runExecutionData.executionData!.waitingExecution[connectionData.node])
						.length === 0
				) {
					// No more data left for the node so also delete that one
					delete this.runExecutionData.executionData!.waitingExecution[connectionData.node];
					delete this.runExecutionData.executionData!.waitingExecutionSource[connectionData.node];
				}
				return;
			}
			stillDataMissing = true;

			if (!nodeWasWaiting) {
				// Get a list of all the output nodes that we can check for siblings easier
				const checkOutputNodes = [];
				// eslint-disable-next-line @typescript-eslint/no-for-in-array
				for (const outputIndexParent in workflow.connectionsBySourceNode[parentNodeName].main) {
					if (
						!workflow.connectionsBySourceNode[parentNodeName].main.hasOwnProperty(outputIndexParent)
					) {
						continue;
					}
					for (const connectionDataCheck of workflow.connectionsBySourceNode[parentNodeName].main[
						outputIndexParent
					]) {
						checkOutputNodes.push(connectionDataCheck.node);
					}
				}

				// Node was not on "waitingExecution" so it is the first time it gets
				// checked. So we have to go through all the inputs and check if they
				// are already on the list to be processed.
				// If that is not the case add it.
				for (
					let inputIndex = 0;
					inputIndex < workflow.connectionsByDestinationNode[connectionData.node].main.length;
					inputIndex++
				) {
					for (const inputData of workflow.connectionsByDestinationNode[connectionData.node].main[
						inputIndex
					]) {
						if (inputData.node === parentNodeName) {
							// Is the node we come from so its data will be available for sure
							continue;
						}

						const executionStackNodes = this.runExecutionData.executionData!.nodeExecutionStack.map(
							(stackData) => stackData.node.name,
						);

						// Check if that node is also an output connection of the
						// previously processed one
						if (inputData.node !== parentNodeName && checkOutputNodes.includes(inputData.node)) {
							// So the parent node will be added anyway which
							// will then process this node next. So nothing to do
							// unless the incoming data of the node is empty
							// because then it would not be executed
							if (
								!this.incomingConnectionIsEmpty(
									this.runExecutionData.resultData.runData,
									workflow.connectionsByDestinationNode[inputData.node].main[0],
									runIndex,
								)
							) {
								continue;
							}
						}

						// Check if it is already in the execution stack
						if (executionStackNodes.includes(inputData.node)) {
							// Node is already on the list to be executed
							// so there is nothing to do
							continue;
						}

						// Check if node got processed already
						if (this.runExecutionData.resultData.runData[inputData.node] !== undefined) {
							// Node got processed already so no need to add it
							continue;
						}

						// Check if any of the parent nodes does not have any inputs. That
						// would mean that it has to get added to the list of nodes to process.
						const parentNodes = workflow.getParentNodes(inputData.node, 'main', -1);
						let nodeToAdd: string | undefined = inputData.node;
						parentNodes.push(inputData.node);
						parentNodes.reverse();

						for (const parentNode of parentNodes) {
							// Check if that node is also an output connection of the
							// previously processed one
							if (inputData.node !== parentNode && checkOutputNodes.includes(parentNode)) {
								// So the parent node will be added anyway which
								// will then process this node next. So nothing to do.
								nodeToAdd = undefined;
								break;
							}

							// Check if it is already in the execution stack
							if (executionStackNodes.includes(parentNode)) {
								// Node is already on the list to be executed
								// so there is nothing to do
								nodeToAdd = undefined;
								break;
							}

							// Check if node got processed already
							if (this.runExecutionData.resultData.runData[parentNode] !== undefined) {
								// Node got processed already so we can use the
								// output data as input of this node
								break;
							}

							nodeToAdd = parentNode;
						}
						const parentNodesNodeToAdd = workflow.getParentNodes(nodeToAdd as string);
						if (
							parentNodesNodeToAdd.includes(parentNodeName) &&
							nodeSuccessData[outputIndex].length === 0
						) {
							// We do not add the node if there is no input data and the node that should be connected
							// is a child of the parent node. Because else it would run a node even though it should be
							// specifically not run, as it did not receive any data.
							nodeToAdd = undefined;
						}

						if (nodeToAdd === undefined) {
							// No node has to get added so process
							continue;
						}

						let addEmptyItem = false;

						if (workflow.connectionsByDestinationNode[nodeToAdd] === undefined) {
							// Add empty item if the node does not have any input connections
							addEmptyItem = true;
						} else if (
							this.incomingConnectionIsEmpty(
								this.runExecutionData.resultData.runData,
								workflow.connectionsByDestinationNode[nodeToAdd].main[0],
								runIndex,
							)
						) {
							// Add empty item also if the input data is empty
							addEmptyItem = true;
						}

						if (addEmptyItem) {
							// Add only node if it does not have any inputs because else it will
							// be added by its input node later anyway.
							this.runExecutionData.executionData!.nodeExecutionStack.push({
								node: workflow.getNode(nodeToAdd) as INode,
								data: {
									main: [
										[
											{
												json: {},
											},
										],
									],
								},
								source: {
									main: [
										{
											previousNode: parentNodeName,
											previousNodeOutput: outputIndex || undefined,
											previousNodeRun: runIndex || undefined,
										},
									],
								},
							});
						}
					}
				}
			}
		}

		// Make sure the array has all the values
		const connectionDataArray: Array<INodeExecutionData[] | null> = [];
		for (let i: number = connectionData.index; i >= 0; i--) {
			connectionDataArray[i] = null;
		}

		// Add the data of the current execution
		if (nodeSuccessData === null) {
			connectionDataArray[connectionData.index] = null;
		} else {
			connectionDataArray[connectionData.index] = nodeSuccessData[outputIndex];
		}

		if (stillDataMissing) {
			// Additional data is needed to run node so add it to waiting
			if (
				!this.runExecutionData.executionData!.waitingExecution.hasOwnProperty(connectionData.node)
			) {
				this.runExecutionData.executionData!.waitingExecution[connectionData.node] = {};
			}
			this.runExecutionData.executionData!.waitingExecution[connectionData.node][runIndex] = {
				main: connectionDataArray,
			};
		} else {
			// All data is there so add it directly to stack
			this.runExecutionData.executionData!.nodeExecutionStack.push({
				node: workflow.nodes[connectionData.node],
				data: {
					main: connectionDataArray,
				},
				source: {
					main: [
						{
							previousNode: parentNodeName,
							previousNodeOutput: outputIndex || undefined,
							previousNodeRun: runIndex || undefined,
						},
					],
				},
			});
		}
	}

	/**
	 * Runs the given execution data.
	 *
	 */
	// IMPORTANT: Do not add "async" to this function, it will then convert the
	//            PCancelable to a regular Promise and does so not allow canceling
	//            active executions anymore
	// eslint-disable-next-line @typescript-eslint/promise-function-async
	processRunExecutionData(workflow: Workflow): PCancelable<IRun> {
		Logger.verbose('Workflow execution started', { workflowId: workflow.id });

		const startedAt = new Date();

		this.status = 'running';

		const startNode = this.runExecutionData.executionData!.nodeExecutionStack[0].node.name;

		let destinationNode: string | undefined;
		if (this.runExecutionData.startData && this.runExecutionData.startData.destinationNode) {
			destinationNode = this.runExecutionData.startData.destinationNode;
		}

		const pinDataNodeNames = Object.keys(this.runExecutionData.resultData.pinData ?? {});

		const workflowIssues = workflow.checkReadyForExecution({
			startNode,
			destinationNode,
			pinDataNodeNames,
		});
		if (workflowIssues !== null) {
			throw new Error(
				'The workflow has issues and can for that reason not be executed. Please fix them first.',
			);
		}

		// Variables which hold temporary data for each node-execution
		let executionData: IExecuteData;
		let executionError: ExecutionError | undefined;
		let executionNode: INode;
		let nodeSuccessData: INodeExecutionData[][] | null | undefined;
		let runIndex: number;
		let startTime: number;
		let taskData: ITaskData;

		if (this.runExecutionData.startData === undefined) {
			this.runExecutionData.startData = {};
		}

		if (this.runExecutionData.waitTill) {
			const lastNodeExecuted = this.runExecutionData.resultData.lastNodeExecuted as string;
			this.runExecutionData.executionData!.nodeExecutionStack[0].node.disabled = true;
			this.runExecutionData.waitTill = undefined;
			this.runExecutionData.resultData.runData[lastNodeExecuted].pop();
		}

		let currentExecutionTry = '';
		let lastExecutionTry = '';
		let closeFunction: Promise<void> | undefined;

		return new PCancelable(async (resolve, reject, onCancel) => {
			let gotCancel = false;

			onCancel.shouldReject = false;
			onCancel(() => {
				gotCancel = true;
			});

			const returnPromise = (async () => {
				try {
					await this.executeHook('workflowExecuteBefore', [workflow]);
				} catch (error) {
					// Set the error that it can be saved correctly
					executionError = {
						...(error as NodeOperationError | NodeApiError),
						message: (error as NodeOperationError | NodeApiError).message,
						stack: (error as NodeOperationError | NodeApiError).stack,
					};

					// Set the incoming data of the node that it can be saved correctly
					// eslint-disable-next-line prefer-destructuring
					executionData = this.runExecutionData.executionData!.nodeExecutionStack[0];
					this.runExecutionData.resultData = {
						runData: {
							[executionData.node.name]: [
								{
									startTime,
									executionTime: new Date().getTime() - startTime,
									data: {
										main: executionData.data.main,
									} as ITaskDataConnections,
									source: [],
									executionStatus: 'error',
								},
							],
						},
						lastNodeExecuted: executionData.node.name,
						error: executionError,
					};

					throw error;
				}

				executionLoop: while (
					this.runExecutionData.executionData!.nodeExecutionStack.length !== 0
				) {
					if (
						this.additionalData.executionTimeoutTimestamp !== undefined &&
						Date.now() >= this.additionalData.executionTimeoutTimestamp
					) {
						gotCancel = true;
					}

					if (gotCancel) {
						return Promise.resolve();
					}

					nodeSuccessData = null;
					executionError = undefined;
					executionData =
						this.runExecutionData.executionData!.nodeExecutionStack.shift() as IExecuteData;
					executionNode = executionData.node;

					// Update the pairedItem information on items
					const newTaskDataConnections: ITaskDataConnections = {};
					for (const inputName of Object.keys(executionData.data)) {
						newTaskDataConnections[inputName] = executionData.data[inputName].map(
							(input, inputIndex) => {
								if (input === null) {
									return input;
								}

								return input.map((item, itemIndex) => {
									return {
										...item,
										pairedItem: {
											item: itemIndex,
											input: inputIndex || undefined,
										},
									};
								});
							},
						);
					}
					executionData.data = newTaskDataConnections;

					Logger.debug(`Start processing node "${executionNode.name}"`, {
						node: executionNode.name,
						workflowId: workflow.id,
					});
					await this.executeHook('nodeExecuteBefore', [executionNode.name]);

					// Get the index of the current run
					runIndex = 0;
					if (this.runExecutionData.resultData.runData.hasOwnProperty(executionNode.name)) {
						runIndex = this.runExecutionData.resultData.runData[executionNode.name].length;
					}

					currentExecutionTry = `${executionNode.name}:${runIndex}`;

					if (currentExecutionTry === lastExecutionTry) {
						throw new Error('Did stop execution because execution seems to be in endless loop.');
					}

					if (
						this.runExecutionData.startData!.runNodeFilter !== undefined &&
						this.runExecutionData.startData!.runNodeFilter.indexOf(executionNode.name) === -1
					) {
						// If filter is set and node is not on filter skip it, that avoids the problem that it executes
						// leafs that are parallel to a selected destinationNode. Normally it would execute them because
						// they have the same parent and it executes all child nodes.
						continue;
					}

					// Check if all the data which is needed to run the node is available
					if (workflow.connectionsByDestinationNode.hasOwnProperty(executionNode.name)) {
						// Check if the node has incoming connections
						if (workflow.connectionsByDestinationNode[executionNode.name].hasOwnProperty('main')) {
							let inputConnections: IConnection[][];
							let connectionIndex: number;

							// eslint-disable-next-line prefer-const
							inputConnections = workflow.connectionsByDestinationNode[executionNode.name].main;

							for (
								connectionIndex = 0;
								connectionIndex < inputConnections.length;
								connectionIndex++
							) {
								if (
									workflow.getHighestNode(executionNode.name, 'main', connectionIndex).length === 0
								) {
									// If there is no valid incoming node (if all are disabled)
									// then ignore that it has inputs and simply execute it as it is without
									// any data
									continue;
								}

								if (!executionData.data.hasOwnProperty('main')) {
									// ExecutionData does not even have the connection set up so can
									// not have that data, so add it again to be executed later
									this.runExecutionData.executionData!.nodeExecutionStack.push(executionData);
									lastExecutionTry = currentExecutionTry;
									continue executionLoop;
								}

								// Check if it has the data for all the inputs
								// The most nodes just have one but merge node for example has two and data
								// of both inputs has to be available to be able to process the node.
								if (
									executionData.data.main.length < connectionIndex ||
									executionData.data.main[connectionIndex] === null
								) {
									// Does not have the data of the connections so add back to stack
									this.runExecutionData.executionData!.nodeExecutionStack.push(executionData);
									lastExecutionTry = currentExecutionTry;
									continue executionLoop;
								}
							}
						}
					}

					startTime = new Date().getTime();

					let maxTries = 1;
					if (executionData.node.retryOnFail === true) {
						// TODO: Remove the hardcoded default-values here and also in NodeSettings.vue
						maxTries = Math.min(5, Math.max(2, executionData.node.maxTries || 3));
					}

					let waitBetweenTries = 0;
					if (executionData.node.retryOnFail === true) {
						// TODO: Remove the hardcoded default-values here and also in NodeSettings.vue
						waitBetweenTries = Math.min(
							5000,
							Math.max(0, executionData.node.waitBetweenTries || 1000),
						);
					}

					for (let tryIndex = 0; tryIndex < maxTries; tryIndex++) {
						if (gotCancel) {
							return Promise.resolve();
						}
						try {
							if (tryIndex !== 0) {
								// Reset executionError from previous error try
								executionError = undefined;
								if (waitBetweenTries !== 0) {
									// TODO: Improve that in the future and check if other nodes can
									//       be executed in the meantime
									// eslint-disable-next-line @typescript-eslint/no-shadow
									await new Promise((resolve) => {
										setTimeout(() => {
											resolve(undefined);
										}, waitBetweenTries);
									});
								}
							}

							const { pinData } = this.runExecutionData.resultData;

							if (pinData && !executionNode.disabled && pinData[executionNode.name] !== undefined) {
								const nodePinData = pinData[executionNode.name];

								nodeSuccessData = [nodePinData]; // always zeroth runIndex
							} else {
								Logger.debug(`Running node "${executionNode.name}" started`, {
									node: executionNode.name,
									workflowId: workflow.id,
								});
								const runNodeData = await workflow.runNode(
									executionData,
									this.runExecutionData,
									runIndex,
									this.additionalData,
									NodeExecuteFunctions,
									this.mode,
								);
								nodeSuccessData = runNodeData.data;

								if (runNodeData.closeFunction) {
									// Explanation why we do this can be found in n8n-workflow/Workflow.ts -> runNode
									// eslint-disable-next-line @typescript-eslint/no-unsafe-call
									closeFunction = runNodeData.closeFunction();
								}
							}

							Logger.debug(`Running node "${executionNode.name}" finished successfully`, {
								node: executionNode.name,
								workflowId: workflow.id,
							});

							if (nodeSuccessData) {
								// Check if the output data contains pairedItem data
								checkOutputData: for (const outputData of nodeSuccessData) {
									if (outputData === null) {
										continue;
									}
									for (const [index, item] of outputData.entries()) {
										if (!item.pairedItem) {
											// The pairedItem data is missing, so check if it can get automatically fixed
											if (
												executionData.data.main.length === 1 &&
												executionData.data.main[0]?.length === 1
											) {
												// The node has one input and one incoming item, so we know
												// that all items must originate from that single
												item.pairedItem = {
													item: 0,
												};
											} else if (
												nodeSuccessData.length === 1 &&
												executionData.data.main.length === 1 &&
												executionData.data.main[0]?.length === nodeSuccessData[0].length
											) {
												// The node has one input and one output. The number of items on both is
												// identical so we can make the reasonable assumption that each of the input
												// items is the origin of the corresponding output items
												item.pairedItem = {
													item: index,
												};
											} else {
												// In all other cases autofixing is not possible
												break checkOutputData;
											}
										}
									}
								}
							}

							if (nodeSuccessData === undefined) {
								// Node did not get executed
								nodeSuccessData = null;
							} else {
								this.runExecutionData.resultData.lastNodeExecuted = executionData.node.name;
							}

							if (nodeSuccessData === null || nodeSuccessData[0][0] === undefined) {
								if (executionData.node.alwaysOutputData === true) {
									nodeSuccessData = nodeSuccessData || [];
									nodeSuccessData[0] = [
										{
											json: {},
										},
									];
								}
							}

							if (nodeSuccessData === null && !this.runExecutionData.waitTill!) {
								// If null gets returned it means that the node did succeed
								// but did not have any data. So the branch should end
								// (meaning the nodes afterwards should not be processed)
								continue executionLoop;
							}

							break;
						} catch (error) {
							this.runExecutionData.resultData.lastNodeExecuted = executionData.node.name;

							executionError = {
								...(error as NodeOperationError | NodeApiError),
								message: (error as NodeOperationError | NodeApiError).message,
								stack: (error as NodeOperationError | NodeApiError).stack,
							};

							Logger.debug(`Running node "${executionNode.name}" finished with error`, {
								node: executionNode.name,
								workflowId: workflow.id,
							});
						}
					}

					// Add the data to return to the user
					// (currently does not get cloned as data does not get changed, maybe later we should do that?!?!)

					if (!this.runExecutionData.resultData.runData.hasOwnProperty(executionNode.name)) {
						this.runExecutionData.resultData.runData[executionNode.name] = [];
					}

					taskData = {
						startTime,
						executionTime: new Date().getTime() - startTime,
						source: !executionData.source ? [] : executionData.source.main,
						executionStatus: 'success',
					};

					if (executionError !== undefined) {
						taskData.error = executionError;
						taskData.executionStatus = 'error';

						if (executionData.node.continueOnFail === true) {
							// Workflow should continue running even if node errors
							if (executionData.data.hasOwnProperty('main') && executionData.data.main.length > 0) {
								// Simply get the input data of the node if it has any and pass it through
								// to the next node
								if (executionData.data.main[0] !== null) {
									nodeSuccessData = [executionData.data.main[0]];
								}
							}
						} else {
							// Node execution did fail so add error and stop execution
							this.runExecutionData.resultData.runData[executionNode.name].push(taskData);

							// Add the execution data again so that it can get restarted
							this.runExecutionData.executionData!.nodeExecutionStack.unshift(executionData);

							await this.executeHook('nodeExecuteAfter', [
								executionNode.name,
								taskData,
								this.runExecutionData,
							]);

							break;
						}
					}

					// Merge error information to default output for now
					// As the new nodes can report the errors in
					// the `error` property.
					for (const execution of nodeSuccessData!) {
						for (const lineResult of execution) {
							if (
								lineResult.json !== undefined &&
								lineResult.json.$error !== undefined &&
								lineResult.json.$json !== undefined
							) {
								lineResult.error = lineResult.json.$error as NodeApiError | NodeOperationError;
								lineResult.json = {
									error: (lineResult.json.$error as NodeApiError | NodeOperationError).message,
								};
							} else if (lineResult.error !== undefined) {
								lineResult.json = { error: lineResult.error.message };
							}
						}
					}

					// Node executed successfully. So add data and go on.
					taskData.data = {
						main: nodeSuccessData,
					} as ITaskDataConnections;

					this.runExecutionData.resultData.runData[executionNode.name].push(taskData);

					if (this.runExecutionData.waitTill!) {
						await this.executeHook('nodeExecuteAfter', [
							executionNode.name,
							taskData,
							this.runExecutionData,
						]);

						// Add the node back to the stack that the workflow can start to execute again from that node
						this.runExecutionData.executionData!.nodeExecutionStack.unshift(executionData);

						break;
					}

					if (
						this.runExecutionData.startData &&
						this.runExecutionData.startData.destinationNode &&
						this.runExecutionData.startData.destinationNode === executionNode.name
					) {
						// Before stopping, make sure we are executing hooks so
						// That frontend is notified for example for manual executions.
						await this.executeHook('nodeExecuteAfter', [
							executionNode.name,
							taskData,
							this.runExecutionData,
						]);

						// If destination node is defined and got executed stop execution
						continue;
					}

					// Add the nodes to which the current node has an output connection to that they can
					// be executed next
					if (workflow.connectionsBySourceNode.hasOwnProperty(executionNode.name)) {
						if (workflow.connectionsBySourceNode[executionNode.name].hasOwnProperty('main')) {
							let outputIndex: string;
							let connectionData: IConnection;
							// Iterate over all the outputs

							// Add the nodes to be executed
							// eslint-disable-next-line @typescript-eslint/no-for-in-array
							for (outputIndex in workflow.connectionsBySourceNode[executionNode.name].main) {
								if (
									!workflow.connectionsBySourceNode[executionNode.name].main.hasOwnProperty(
										outputIndex,
									)
								) {
									continue;
								}

								// Iterate over all the different connections of this output
								for (connectionData of workflow.connectionsBySourceNode[executionNode.name].main[
									outputIndex
								]) {
									if (!workflow.nodes.hasOwnProperty(connectionData.node)) {
										return Promise.reject(
											new Error(
												`The node "${executionNode.name}" connects to not found node "${connectionData.node}"`,
											),
										);
									}

									if (
										nodeSuccessData![outputIndex] &&
										(nodeSuccessData![outputIndex].length !== 0 || connectionData.index > 0)
									) {
										// Add the node only if it did execute or if connected to second "optional" input
										this.addNodeToBeExecuted(
											workflow,
											connectionData,
											parseInt(outputIndex, 10),
											executionNode.name,
											nodeSuccessData!,
											runIndex,
										);
									}
								}
							}
						}
					}

					// If we got here, it means that we did not stop executing from manual executions / destination.
					// Execute hooks now to make sure that all hooks are executed properly
					// Await is needed to make sure that we don't fall into concurrency problems
					// When saving node execution data
					await this.executeHook('nodeExecuteAfter', [
						executionNode.name,
						taskData,
						this.runExecutionData,
					]);
				}

				return Promise.resolve();
			})()
				.then(async () => {
					if (gotCancel && executionError === undefined) {
						return this.processSuccessExecution(
							startedAt,
							workflow,
							new WorkflowOperationError('Workflow has been canceled or timed out!'),
							closeFunction,
						);
					}
					return this.processSuccessExecution(startedAt, workflow, executionError, closeFunction);
				})
				.catch(async (error) => {
					const fullRunData = this.getFullRunData(startedAt);

					fullRunData.data.resultData.error = {
						...error,
						message: error.message,
						stack: error.stack,
					};

					// Check if static data changed
					let newStaticData: IDataObject | undefined;
					// eslint-disable-next-line no-underscore-dangle
					if (workflow.staticData.__dataChanged === true) {
						// Static data of workflow changed
						newStaticData = workflow.staticData;
					}
					await this.executeHook('workflowExecuteAfter', [fullRunData, newStaticData]).catch(
						// eslint-disable-next-line @typescript-eslint/no-shadow
						(error) => {
							// eslint-disable-next-line no-console
							console.error('There was a problem running hook "workflowExecuteAfter"', error);
						},
					);

					if (closeFunction) {
						try {
							await closeFunction;
						} catch (errorClose) {
							Logger.error(
								// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions
								`There was a problem deactivating trigger of workflow "${workflow.id}": "${errorClose.message}"`,
								{
									workflowId: workflow.id,
								},
							);
						}
					}

					return fullRunData;
				});

			return returnPromise.then(resolve);
		});
	}

	async processSuccessExecution(
		startedAt: Date,
		workflow: Workflow,
		executionError?: ExecutionError,
		closeFunction?: Promise<void>,
	): Promise<IRun> {
		const fullRunData = this.getFullRunData(startedAt);

		if (executionError !== undefined) {
			Logger.verbose('Workflow execution finished with error', {
				error: executionError,
				workflowId: workflow.id,
			});
			fullRunData.data.resultData.error = {
				...executionError,
				message: executionError.message,
				stack: executionError.stack,
			} as ExecutionError;
		} else if (this.runExecutionData.waitTill!) {
			// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
			Logger.verbose(`Workflow execution will wait until ${this.runExecutionData.waitTill}`, {
				workflowId: workflow.id,
			});
			fullRunData.waitTill = this.runExecutionData.waitTill;
		} else {
			Logger.verbose('Workflow execution finished successfully', { workflowId: workflow.id });
			fullRunData.finished = true;
		}

		// Check if static data changed
		let newStaticData: IDataObject | undefined;
		// eslint-disable-next-line no-underscore-dangle
		if (workflow.staticData.__dataChanged === true) {
			// Static data of workflow changed
			newStaticData = workflow.staticData;
		}

		await this.executeHook('workflowExecuteAfter', [fullRunData, newStaticData]);

		if (closeFunction) {
			try {
				await closeFunction;
			} catch (error) {
				Logger.error(
					// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
					`There was a problem deactivating trigger of workflow "${workflow.id}": "${error.message}"`,
					{
						workflowId: workflow.id,
					},
				);
			}
		}

		return fullRunData;
	}

	getFullRunData(startedAt: Date): IRun {
		const fullRunData: IRun = {
			data: this.runExecutionData,
			mode: this.mode,
			startedAt,
			stoppedAt: new Date(),
			status: this.status,
		};

		return fullRunData;
	}
}


export const allCurrencies = [
	{ name: 'Euro', value: 'eur' },
	{ name: 'United States Dollar', value: 'usd' },
	{ name: 'British Pound Sterling', value: 'gbp' },
	{ name: 'Swiss Franc', value: 'chf' },
	{ name: 'Renminbi', value: 'cny' },
	{ name: '--------', value: '' },
	{ name: 'United Arab Emirates Dirham', value: 'aed' },
	{ name: 'Afghan Afghani', value: 'afn' },
	{ name: 'Albanian Lek', value: 'all' },
	{ name: 'Armenian Dram', value: 'amd' },
	{ name: 'Netherlands Antillean Guilder', value: 'ang' },
	{ name: 'Angolan Kwanza', value: 'aoa' },
	{ name: 'Argentine Peso', value: 'ars' },
	{ name: 'Australian Dollar', value: 'aud' },
	{ name: 'Aruban Florin', value: 'awg' },
	{ name: 'Azerbaijani Manat', value: 'azn' },
	{ name: 'Bosnia-Herzegovina Convertible Mark', value: 'bam' },
	{ name: 'Barbadian Dollar', value: 'bbd' },
	{ name: 'Bangladeshi Taka', value: 'bdt' },
	{ name: 'Bulgarian Lev', value: 'bgn' },
	{ name: 'Bahraini Dinar', value: 'bhd' },
	{ name: 'Burundian Franc', value: 'bif' },
	{ name: 'Bermudan Dollar', value: 'bmd' },
	{ name: 'Brunei Dollar', value: 'bnd' },
	{ name: 'Bolivian Boliviano', value: 'bob' },
	{ name: 'Brazilian Real', value: 'brl' },
	{ name: 'Bahamian Dollar', value: 'bsd' },
	{ name: 'Bitcoin', value: 'btc' },
	{ name: 'Bhutanese Ngultrum', value: 'btn' },
	{ name: 'Botswanan Pula', value: 'bwp' },
	{ name: 'Belarusian Ruble', value: 'byn' },
	{ name: 'Belize Dollar', value: 'bzd' },
	{ name: 'Canadian Dollar', value: 'cad' },
	{ name: 'Congolese Franc', value: 'cdf' },
	{ name: 'Chilean Unit of Account (UF)', value: 'clf' },
	{ name: 'Chilean Peso', value: 'clp' },
	{ name: 'Chinese Yuan (Offshore)', value: 'cnh' },
	{ name: 'Colombian Peso', value: 'cop' },
	{ name: 'Costa Rican Colón', value: 'crc' },
	{ name: 'Cuban Convertible Peso', value: 'cuc' },
	{ name: 'Cuban Peso', value: 'cup' },
	{ name: 'Cape Verdean Escudo', value: 'cve' },
	{ name: 'Czech Republic Koruna', value: 'czk' },
	{ name: 'Djiboutian Franc', value: 'djf' },
	{ name: 'Danish Krone', value: 'dkk' },
	{ name: 'Dominican Peso', value: 'dop' },
	{ name: 'Algerian Dinar', value: 'dzd' },
	{ name: 'Egyptian Pound', value: 'egp' },
	{ name: 'Eritrean Nakfa', value: 'ern' },
	{ name: 'Ethiopian Birr', value: 'etb' },
	{ name: 'Fijian Dollar', value: 'fjd' },
	{ name: 'Falkland Islands Pound', value: 'fkp' },
	{ name: 'Georgian Lari', value: 'gel' },
	{ name: 'Guernsey Pound', value: 'ggp' },
	{ name: 'Ghanaian Cedi', value: 'ghs' },
	{ name: 'Gibraltar Pound', value: 'gip' },
	{ name: 'Gambian Dalasi', value: 'gmd' },
	{ name: 'Guinean Franc', value: 'gnf' },
	{ name: 'Guatemalan Quetzal', value: 'gtq' },
	{ name: 'Guyanaese Dollar', value: 'gyd' },
	{ name: 'Hong Kong Dollar', value: 'hkd' },
	{ name: 'Honduran Lempira', value: 'hnl' },
	{ name: 'Croatian Kuna', value: 'hrk' },
	{ name: 'Haitian Gourde', value: 'htg' },
	{ name: 'Hungarian Forint', value: 'huf' },
	{ name: 'Indonesian Rupiah', value: 'idr' },
	{ name: 'Israeli New Sheqel', value: 'ils' },
	{ name: 'Manx Pound', value: 'imp' },
	{ name: 'Indian Rupee', value: 'inr' },
	{ name: 'Iraqi Dinar', value: 'iqd' },
	{ name: 'Iranian Rial', value: 'irr' },
	{ name: 'Icelandic Króna', value: 'isk' },
	{ name: 'Jersey Pound', value: 'jep' },
	{ name: 'Jamaican Dollar', value: 'jmd' },
	{ name: 'Jordanian Dinar', value: 'jod' },
	{ name: 'Japanese Yen', value: 'jpy' },
	{ name: 'Kenyan Shilling', value: 'kes' },
	{ name: 'Kyrgystani Som', value: 'kgs' },
	{ name: 'Cambodian Riel', value: 'khr' },
	{ name: 'Comorian Franc', value: 'kmf' },
	{ name: 'North Korean Won', value: 'kpw' },
	{ name: 'South Korean Won', value: 'krw' },
	{ name: 'Kuwaiti Dinar', value: 'kwd' },
	{ name: 'Cayman Islands Dollar', value: 'kyd' },
	{ name: 'Kazakhstani Tenge', value: 'kzt' },
	{ name: 'Laotian Kip', value: 'lak' },
	{ name: 'Lebanese Pound', value: 'lbp' },
	{ name: 'Sri Lankan Rupee', value: 'lkr' },
	{ name: 'Liberian Dollar', value: 'lrd' },
	{ name: 'Lesotho Loti', value: 'lsl' },
	{ name: 'Libyan Dinar', value: 'lyd' },
	{ name: 'Moroccan Dirham', value: 'mad' },
	{ name: 'Moldovan Leu', value: 'mdl' },
	{ name: 'Malagasy Ariary', value: 'mga' },
	{ name: 'Macedonian Denar', value: 'mkd' },
	{ name: 'Myanma Kyat', value: 'mmk' },
	{ name: 'Mongolian Tugrik', value: 'mnt' },
	{ name: 'Macanese Pataca', value: 'mop' },
	{ name: 'Mauritanian Ouguiya (Pre-2018)', value: 'mro' },
	{ name: 'Mauritanian Ouguiya', value: 'mru' },
	{ name: 'Mauritian Rupee', value: 'mur' },
	{ name: 'Maldivian Rufiyaa', value: 'mvr' },
	{ name: 'Malawian Kwacha', value: 'mwk' },
	{ name: 'Mexican Peso', value: 'mxn' },
	{ name: 'Malaysian Ringgit', value: 'myr' },
	{ name: 'Mozambican Metical', value: 'mzn' },
	{ name: 'Namibian Dollar', value: 'nad' },
	{ name: 'Nigerian Naira', value: 'ngn' },
	{ name: 'Nicaraguan Córdoba', value: 'nio' },
	{ name: 'Norwegian Krone', value: 'nok' },
	{ name: 'Nepalese Rupee', value: 'npr' },
	{ name: 'New Zealand Dollar', value: 'nzd' },
	{ name: 'Omani Rial', value: 'omr' },
	{ name: 'Panamanian Balboa', value: 'pab' },
	{ name: 'Peruvian Nuevo Sol', value: 'pen' },
	{ name: 'Papua New Guinean Kina', value: 'pgk' },
	{ name: 'Philippine Peso', value: 'php' },
	{ name: 'Pakistani Rupee', value: 'pkr' },
	{ name: 'Polish Zloty', value: 'pln' },
	{ name: 'Paraguayan Guarani', value: 'pyg' },
	{ name: 'Qatari Rial', value: 'qar' },
	{ name: 'Romanian Leu', value: 'ron' },
	{ name: 'Serbian Dinar', value: 'rsd' },
	{ name: 'Russian Ruble', value: 'rub' },
	{ name: 'Rwandan Franc', value: 'rwf' },
	{ name: 'Saudi Riyal', value: 'sar' },
	{ name: 'Solomon Islands Dollar', value: 'sbd' },
	{ name: 'Seychellois Rupee', value: 'scr' },
	{ name: 'Sudanese Pound', value: 'sdg' },
	{ name: 'Swedish Krona', value: 'sek' },
	{ name: 'Singapore Dollar', value: 'sgd' },
	{ name: 'Saint Helena Pound', value: 'shp' },
	{ name: 'Sierra Leonean Leone', value: 'sll' },
	{ name: 'Somali Shilling', value: 'sos' },
	{ name: 'Surinamese Dollar', value: 'srd' },
	{ name: 'South Sudanese Pound', value: 'ssp' },
	{ name: 'São Tomé and Príncipe Dobra (Pre-2018)', value: 'std' },
	{ name: 'São Tomé and Príncipe Dobra', value: 'stn' },
	{ name: 'Salvadoran Colón', value: 'svc' },
	{ name: 'Syrian Pound', value: 'syp' },
	{ name: 'Swazi Lilangeni', value: 'szl' },
	{ name: 'Thai Baht', value: 'thb' },
	{ name: 'Tajikistani Somoni', value: 'tjs' },
	{ name: 'Turkmenistani Manat', value: 'tmt' },
	{ name: 'Tunisian Dinar', value: 'tnd' },
	{ name: "Tongan Pa'anga", value: 'top' },
	{ name: 'Turkish Lira', value: 'try' },
	{ name: 'Trinidad and Tobago Dollar', value: 'ttd' },
	{ name: 'New Taiwan Dollar', value: 'twd' },
	{ name: 'Tanzanian Shilling', value: 'tzs' },
	{ name: 'Ukrainian Hryvnia', value: 'uah' },
	{ name: 'Ugandan Shilling', value: 'ugx' },
	{ name: 'Uruguayan Peso', value: 'uyu' },
	{ name: 'Uzbekistan Som', value: 'uzs' },
	{ name: 'Venezuelan Bolívar Fuerte', value: 'vef' },
	{ name: 'Vietnamese Dong', value: 'vnd' },
	{ name: 'Vanuatu Vatu', value: 'vuv' },
	{ name: 'Samoan Tala', value: 'wst' },
	{ name: 'CFA Franc BEAC', value: 'xaf' },
	{ name: 'Silver Ounce', value: 'xag' },
	{ name: 'Gold Ounce', value: 'xau' },
	{ name: 'East Caribbean Dollar', value: 'xcd' },
	{ name: 'Special Drawing Rights', value: 'xdr' },
	{ name: 'CFA Franc BCEAO', value: 'xof' },
	{ name: 'Palladium Ounce', value: 'xpd' },
	{ name: 'CFP Franc', value: 'xpf' },
	{ name: 'Platinum Ounce', value: 'xpt' },
	{ name: 'Yemeni Rial', value: 'yer' },
	{ name: 'South African Rand', value: 'zar' },
	{ name: 'Zambian Kwacha', value: 'zmw' },
	{ name: 'Zimbabwean Dollar', value: 'zwl' },
];


import type { OptionsWithUri } from 'request';

import type {
	IDataObject,
	IExecuteFunctions,
	IExecuteSingleFunctions,
	ILoadOptionsFunctions,
	INodeExecutionData,
	JsonObject,
} from 'n8n-workflow';
import { BINARY_ENCODING, NodeApiError } from 'n8n-workflow';

export async function microsoftApiRequest(
	this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
	method: string,
	resource: string,

	body: any = {},
	qs: IDataObject = {},
	uri?: string,
	headers: IDataObject = {},
	option: IDataObject = { json: true },
): Promise<any> {
	const credentials = await this.getCredentials('microsoftOutlookOAuth2Api');

	let apiUrl = `https://graph.microsoft.com/v1.0/me${resource}`;
	// If accessing shared mailbox
	if (credentials.useShared && credentials.userPrincipalName) {
		apiUrl = `https://graph.microsoft.com/v1.0/users/${credentials.userPrincipalName}${resource}`;
	}

	const options: OptionsWithUri = {
		headers: {
			'Content-Type': 'application/json',
		},
		method,
		body,
		qs,
		uri: uri || apiUrl,
	};
	try {
		Object.assign(options, option);

		if (Object.keys(headers).length !== 0) {
			options.headers = Object.assign({}, options.headers, headers);
		}

		if (Object.keys(body as IDataObject).length === 0) {
			delete options.body;
		}

		return await this.helpers.requestOAuth2.call(this, 'microsoftOutlookOAuth2Api', options);
	} catch (error) {
		throw new NodeApiError(this.getNode(), error as JsonObject);
	}
}

export async function microsoftApiRequestAllItems(
	this: IExecuteFunctions | ILoadOptionsFunctions,
	propertyName: string,
	method: string,
	endpoint: string,

	body: any = {},
	query: IDataObject = {},
	headers: IDataObject = {},
): Promise<any> {
	const returnData: IDataObject[] = [];

	let responseData;
	let uri: string | undefined;
	query.$top = 100;

	do {
		responseData = await microsoftApiRequest.call(
			this,
			method,
			endpoint,
			body,
			query,
			uri,
			headers,
		);
		uri = responseData['@odata.nextLink'];
		returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
	} while (responseData['@odata.nextLink'] !== undefined);

	return returnData;
}

export async function microsoftApiRequestAllItemsSkip(
	this: IExecuteFunctions | ILoadOptionsFunctions,
	propertyName: string,
	method: string,
	endpoint: string,

	body: any = {},
	query: IDataObject = {},
	headers: IDataObject = {},
): Promise<any> {
	const returnData: IDataObject[] = [];

	let responseData;
	query.$top = 100;
	query.$skip = 0;

	do {
		responseData = await microsoftApiRequest.call(
			this,
			method,
			endpoint,
			body,
			query,
			undefined,
			headers,
		);
		query.$skip += query.$top;
		returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
	} while (responseData.value.length !== 0);

	return returnData;
}

export function makeRecipient(email: string) {
	return {
		emailAddress: {
			address: email,
		},
	};
}

export function createMessage(fields: IDataObject) {
	const message: IDataObject = {};

	// Create body object
	if (fields.bodyContent || fields.bodyContentType) {
		const bodyObject = {
			content: fields.bodyContent,
			contentType: fields.bodyContentType,
		};

		message.body = bodyObject;
		delete fields.bodyContent;
		delete fields.bodyContentType;
	}

	// Handle custom headers
	if (
		'internetMessageHeaders' in fields &&
		'headers' in (fields.internetMessageHeaders as IDataObject)
	) {
		fields.internetMessageHeaders = (fields.internetMessageHeaders as IDataObject).headers;
	}

	// Handle recipient fields
	['bccRecipients', 'ccRecipients', 'replyTo', 'sender', 'toRecipients'].forEach((key) => {
		if (Array.isArray(fields[key])) {
			fields[key] = (fields[key] as string[]).map((email) => makeRecipient(email));
		} else if (fields[key] !== undefined) {
			fields[key] = (fields[key] as string)
				.split(',')
				.map((recipient: string) => makeRecipient(recipient));
		}
	});

	['from', 'sender'].forEach((key) => {
		if (fields[key] !== undefined) {
			fields[key] = makeRecipient(fields[key] as string);
		}
	});

	Object.assign(message, fields);

	return message;
}

export async function downloadAttachments(
	this: IExecuteFunctions,
	messages: IDataObject[] | IDataObject,
	prefix: string,
) {
	const elements: INodeExecutionData[] = [];
	if (!Array.isArray(messages)) {
		messages = [messages];
	}
	for (const message of messages) {
		const element: INodeExecutionData = {
			json: message,
			binary: {},
		};
		if (message.hasAttachments === true) {
			const attachments = await microsoftApiRequestAllItems.call(
				this,
				'value',
				'GET',
				`/messages/${message.id}/attachments`,
				{},
			);
			for (const [index, attachment] of attachments.entries()) {
				const response = await microsoftApiRequest.call(
					this,
					'GET',
					`/messages/${message.id}/attachments/${attachment.id}/$value`,
					undefined,
					{},
					undefined,
					{},
					{ encoding: null, resolveWithFullResponse: true },
				);

				const data = Buffer.from(response.body as string, 'utf8');
				element.binary![`${prefix}${index}`] = await this.helpers.prepareBinaryData(
					data as unknown as Buffer,
					attachment.name as string,
					attachment.contentType as string,
				);
			}
		}
		if (Object.keys(element.binary!).length === 0) {
			delete element.binary;
		}
		elements.push(element);
	}
	return elements;
}

export async function binaryToAttachments(
	this: IExecuteFunctions,
	attachments: IDataObject[],
	items: INodeExecutionData[],
	i: number,
) {
	return Promise.all(
		attachments.map(async (attachment) => {
			const binaryPropertyName = attachment.binaryPropertyName as string;
			const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
			const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
			return {
				'@odata.type': '#microsoft.graph.fileAttachment',
				name: binaryData.fileName,
				contentBytes: dataBuffer.toString(BINARY_ENCODING),
			};
		}),
	);
}


import type {
	IExecuteFunctions,
	IDataObject,
	ILoadOptionsFunctions,
	INodeExecutionData,
	INodePropertyOptions,
	INodeType,
	INodeTypeDescription,
} from 'n8n-workflow';

import { bannerbearApiRequest, keysToSnakeCase } from './GenericFunctions';

import { imageFields, imageOperations } from './ImageDescription';

import { templateFields, templateOperations } from './TemplateDescription';

export class Bannerbear implements INodeType {
	description: INodeTypeDescription = {
		displayName: 'Bannerbear',
		name: 'bannerbear',
		// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
		icon: 'file:bannerbear.png',
		group: ['output'],
		version: 1,
		subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
		description: 'Consume Bannerbear API',
		defaults: {
			name: 'Bannerbear',
		},
		inputs: ['main'],
		outputs: ['main'],
		credentials: [
			{
				name: 'bannerbearApi',
				required: true,
			},
		],
		properties: [
			{
				displayName: 'Resource',
				name: 'resource',
				type: 'options',
				noDataExpression: true,
				options: [
					{
						name: 'Image',
						value: 'image',
					},
					{
						name: 'Template',
						value: 'template',
					},
				],
				default: 'image',
			},
			// IMAGE
			...imageOperations,
			...imageFields,
			// TEMPLATE
			...templateOperations,
			...templateFields,
		],
	};

	methods = {
		loadOptions: {
			// Get all the available templates to display them to user so that he can
			// select them easily
			async getTemplates(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
				const returnData: INodePropertyOptions[] = [];
				const templates = await bannerbearApiRequest.call(this, 'GET', '/templates');
				for (const template of templates) {
					const templateName = template.name;
					const templateId = template.uid;
					returnData.push({
						name: templateName,
						value: templateId,
					});
				}
				return returnData;
			},

			// Get all the available modifications to display them to user so that he can
			// select them easily
			async getModificationNames(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
				const templateId = this.getCurrentNodeParameter('templateId');
				const returnData: INodePropertyOptions[] = [];
				const { available_modifications } = await bannerbearApiRequest.call(
					this,
					'GET',
					`/templates/${templateId}`,
				);
				for (const modification of available_modifications) {
					const modificationName = modification.name;
					const modificationId = modification.name;
					returnData.push({
						name: modificationName,
						value: modificationId,
					});
				}
				return returnData;
			},
		},
	};

	async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
		const items = this.getInputData();
		const returnData: IDataObject[] = [];
		const length = items.length;
		let responseData;
		const resource = this.getNodeParameter('resource', 0);
		const operation = this.getNodeParameter('operation', 0);
		for (let i = 0; i < length; i++) {
			if (resource === 'image') {
				//https://developers.bannerbear.com/#create-an-image
				if (operation === 'create') {
					const templateId = this.getNodeParameter('templateId', i) as string;
					const additionalFields = this.getNodeParameter('additionalFields', i);
					const modifications = (this.getNodeParameter('modificationsUi', i) as IDataObject)
						.modificationsValues as IDataObject;
					const body: IDataObject = {
						template: templateId,
					};
					if (additionalFields.webhookUrl) {
						body.webhook_url = additionalFields.webhookUrl as string;
					}
					if (additionalFields.metadata) {
						body.metadata = additionalFields.metadata as string;
					}
					if (modifications) {
						body.modifications = keysToSnakeCase(modifications);
						// delete all fields set to empty
						for (const modification of body.modifications as IDataObject[]) {
							for (const key of Object.keys(modification)) {
								if (modification[key] === '') {
									delete modification[key];
								}
							}
						}
					}
					responseData = await bannerbearApiRequest.call(this, 'POST', '/images', body);
					if (additionalFields.waitForImage && responseData.status !== 'completed') {
						let maxTries = (additionalFields.waitForImageMaxTries as number) || 3;

						const promise = async (uid: string) => {
							let data: IDataObject = {};
							return new Promise((resolve, reject) => {
								const timeout = setInterval(async () => {
									data = await bannerbearApiRequest.call(this, 'GET', `/images/${uid}`);

									if (data.status === 'completed') {
										clearInterval(timeout);
										resolve(data);
									}
									if (--maxTries === 0) {
										clearInterval(timeout);
										reject(new Error('Image did not finish processing after multiple tries.'));
									}
								}, 2000);
							});
						};

						responseData = await promise(responseData.uid as string);
					}
				}
				//https://developers.bannerbear.com/#get-a-specific-image
				if (operation === 'get') {
					const imageId = this.getNodeParameter('imageId', i) as string;
					responseData = await bannerbearApiRequest.call(this, 'GET', `/images/${imageId}`);
				}
			}
			if (resource === 'template') {
				//https://developers.bannerbear.com/#get-a-specific-template
				if (operation === 'get') {
					const templateId = this.getNodeParameter('templateId', i) as string;
					responseData = await bannerbearApiRequest.call(this, 'GET', `/templates/${templateId}`);
				}
				//https://developers.bannerbear.com/#list-templates
				if (operation === 'getAll') {
					responseData = await bannerbearApiRequest.call(this, 'GET', '/templates');
				}
			}
			if (Array.isArray(responseData)) {
				returnData.push.apply(returnData, responseData as IDataObject[]);
			} else {
				returnData.push(responseData as IDataObject);
			}
		}
		return [this.helpers.returnJsonArray(returnData)];
	}
}