// Automatic Genearted File for NopeModule
// To update run `npm run build:backend` 

import { INopeDispatcher } from "{{pathOfDispatcher}}";
import { Operation } from "express-openapi";

export default function (_dispatcher: INopeDispatcher) {

    let operations = {
        {{mode}}
    };

    // Function to Parse the Inputs
    const parseParams = (req) => {
        return [{{#each schema.inputs}}req.body.{{name}}{{#unless @last}}, {{/unless}}{{/each}}]
    }

    // Define the Action.
    async function {{mode}}(req, res, next) {
        try {
            // Transform the Operation to the Callback of the Dispatcher.
            const result = await _dispatcher.performCall('{{id}}', parseParams(req))
            // Finish the Task.
            res.status(200).json(result)
        } catch (e) {
            // An error Occourd => 
            res.status(500).json(e)
        }        
    }

    // Define the apiDoc for this specific Funtion
    {{mode}}.apiDoc = {
        tags: ['{{tag}}'],
        {{#if methodDescription}}summary: '{{methodDescription}}',{{/if}}
        {{#if operationId}}operationId: '{{operationId}}',{{/if}}
        parameters: [
            {{#if hasInput}}
            {
                name: '{{name}}',
                in: "body",
                description: '{{description}}',
                required: true,
                schema: {{{parsedInput}}}            
            }
            {{/if}}                    
        ],
        responses: {
            200: {
                {{#if resultDescription}}description: '{{resultDescription}}',{{/if}}
                schema: {{{parsedOutput}}}
            },
            default: {
                description: 'An error occurred',
                schema: {
                    additionalProperties: true
                }
            }
        }
    } as Operation;

    return operations;
}