import {
    INodeType,
    INodeTypeDescription,
    INodeExecutionData,
    IExecuteFunctions,
    NodeConnectionType,
} from 'n8n-workflow';

export class _MyCustomNode implements INodeType {
    description: INodeTypeDescription = {
        displayName: 'My Custom Node',
        name: 'myCustomNode',
        group: ['transform'],
        version: 1,
        description: 'Пример кастомного узла',
        defaults: {
            name: 'My Custom Node',
        },
        inputs: [NodeConnectionType.Main],
        outputs: [NodeConnectionType.Main],
        properties: [
            {
                displayName: 'Text to Append',
                name: 'appendText',
                type: 'string',
                default: ' Hello n8n!',
                description: 'Текст, который будет добавлен к входу',
            },
        ],
    };

    async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
        const items = this.getInputData();
        const returnData: INodeExecutionData[] = [];

        for (let i = 0; i < items.length; i++) {
            const appendText = this.getNodeParameter('appendText', i, '') as string;
            const input = items[i].json['input'] as string || '';

            const newItem: INodeExecutionData = {
                json: {
                    ...items[i].json,
                    result: input + appendText,
                },
            };

            returnData.push(newItem);
        }

        return [returnData];
    }
}
