text: import { app, Menu, shell } from 'electron';
  import path from 'path';
  import aboutWindow from 'about-window';
  
  export default class MenuBuilder {
    mainWindow;
  
    constructor(mainWindow) {
      this.mainWindow = mainWindow;
    }
  
    openAboutWindow() {
      aboutWindow({
        icon_path:
          process.env.NODE_ENV === 'development'
            ? path.join(__dirname, '..', '..', 'resources', 'about-icon.png')
            : path.join(__dirname, '..', 'resources', 'about-icon.png'), // in prod build this files is compiled in main.prod.js which resides in app/ folder
        package_json_dir: path.join(__dirname, '..', '..'),
        win_options: {
          parent: this.mainWindow,
          alwaysOnTop: true,
          minimizable: false,
          maximizable: false,
          fullscreenable: false
        }
      });
    }
  
    buildMenu() {
      if (
        process.env.NODE_ENV === 'development' ||
        process.env.DEBUG_PROD === 'true'
      ) {
        this.setupDevelopmentEnvironment();
      }
  
      let template;
  
      if (process.platform === 'darwin') {
        template = this.buildDarwinTemplate();
      } else {
        template = this.buildDefaultTemplate();
      }
  
      const menu = Menu.buildFromTemplate(template);
      Menu.setApplicationMenu(menu);
  
      return menu;
    }
  
    setupDevelopmentEnvironment() {
      this.mainWindow.openDevTools();
      this.mainWindow.webContents.on('context-menu', (e, props) => {
        const { x, y } = props;
  
        Menu.buildFromTemplate([
          {
            label: 'Inspect element',
            click: () => {
              this.mainWindow.inspectElement(x, y);
            }
          }
        ]).popup(this.mainWindow);
      });
    }
  
    buildDarwinTemplate() {
      const subMenuAbout = {
        label: 'JoalDesktop',
        submenu: [
          {
            label: 'About',
            click: () => {
              this.openAboutWindow();
            }
          },
          { type: 'separator' },
          {
            label: 'Quit',
            accelerator: 'Command+Q',
            click: () => {
              app.quit();
            }
          }
        ]
      };
      const subMenuViewDev = {
        label: 'View',
        submenu: [
          {
            label: 'Reload',
            accelerator: 'Command+R',
            click: () => {
              this.mainWindow.webContents.reload();
            }
          },
          {
            label: 'Toggle Full Screen',
            accelerator: 'Ctrl+Command+F',
            click: () => {
              this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
            }
          },
          {
            label: 'Toggle Developer Tools',
            accelerator: 'Alt+Command+I',
            click: () => {
              this.mainWindow.toggleDevTools();
            }
          }
        ]
      };
      const subMenuViewProd = {
        label: 'View',
        submenu: [
          {
            label: 'Toggle Full Screen',
            accelerator: 'Ctrl+Command+F',
            click: () => {
              this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
            }
          },
          {
            label: 'Toggle Developer Tools',
            accelerator: 'Shift+Command+I',
            click: () => {
              this.mainWindow.toggleDevTools();
            }
          }
        ]
      };
      const subMenuWindow = {
        label: 'Window',
        submenu: [
          {
            label: 'Minimize',
            accelerator: 'Command+M',
            selector: 'performMiniaturize:'
          },
          { label: 'Close', accelerator: 'Command+W', selector: 'performClose:' },
          { type: 'separator' },
          { label: 'Bring All to Front', selector: 'arrangeInFront:' }
        ]
      };
      const subMenuHelp = {
        label: 'Help',
        submenu: [
          {
            label: 'Github repository',
            click() {
              shell.openExternal(
                'https://github.com/anthonyraymond/joal-desktop'
              );
            }
          }
        ]
      };
  
      const subMenuView =
        process.env.NODE_ENV === 'development' ? subMenuViewDev : subMenuViewProd;
  
      return [subMenuAbout, subMenuView, subMenuWindow, subMenuHelp];
    }
  
    buildDefaultTemplate() {
      const templateDefault = [
        {
          label: '&File',
          submenu: [
            {
              label: '&Close',
              accelerator: 'Ctrl+W',
              click: () => {
                this.mainWindow.close();
              }
            }
          ]
        },
        {
          label: '&View',
          submenu:
            process.env.NODE_ENV === 'development'
              ? [
                  {
                    label: '&Reload',
                    accelerator: 'Ctrl+R',
                    click: () => {
                      this.mainWindow.webContents.reload();
                    }
                  },
                  {
                    label: 'Toggle &Full Screen',
                    accelerator: 'F11',
                    click: () => {
                      this.mainWindow.setFullScreen(
                        !this.mainWindow.isFullScreen()
                      );
                    }
                  },
                  {
                    label: 'Toggle &Developer Tools',
                    accelerator: 'Alt+Ctrl+I',
                    click: () => {
                      this.mainWindow.toggleDevTools();
                    }
                  }
                ]
              : [
                  {
                    label: 'Toggle &Full Screen',
                    accelerator: 'F11',
                    click: () => {
                      this.mainWindow.setFullScreen(
                        !this.mainWindow.isFullScreen()
                      );
                    }
                  },
                  {
                    label: 'Toggle &Developer Tools',
                    accelerator: 'Shift+Ctrl+I',
                    click: () => {
                      this.mainWindow.toggleDevTools();
                    }
                  }
                ]
        },
        {
          label: 'Help',
          submenu: [
            {
              label: 'Github repository',
              click() {
                shell.openExternal(
                  'https://github.com/anthonyraymond/joal-desktop'
                );
              }
            },
            {
              label: 'About',
              click: () => {
                this.openAboutWindow();
              }
            }
          ]
        }
      ];
  
      return templateDefault;
    }
  }
  