const nameOfNode = 'rotatingMachine';
const nodeClass   = require('./src/nodeClass.js');
const { MenuManager, configManager } = require('generalFunctions');

module.exports = function(RED) {
  // 1) Register the node type and delegate to your class
  RED.nodes.registerType(nameOfNode, function(config) {
    RED.nodes.createNode(this, config);
    this.nodeClass = new nodeClass(config, RED, this, nameOfNode);
  });

  // 2) Setup the dynamic menu & config endpoints
  const menuMgr = new MenuManager();
  const cfgMgr  = new configManager();

  // Serve /rotatingMachine/menu.js
  RED.httpAdmin.get(`/${nameOfNode}/menu.js`, (req, res) => {
    try {
      const script = menuMgr.createEndpoint(nameOfNode, ['asset','logger','position']);
      res.type('application/javascript').send(script);
    } catch (err) {
      res.status(500).send(`// Error generating menu: ${err.message}`);
    }
  });

  // Serve /rotatingMachine/configData.js
  RED.httpAdmin.get(`/${nameOfNode}/configData.js`, (req, res) => {
    try {
      const script = cfgMgr.createEndpoint(nameOfNode);
      res.type('application/javascript').send(script);
    } catch (err) {
      res.status(500).send(`// Error generating configData: ${err.message}`);
    }
  });
};