import { clipboard, remote } from 'electron';
import isDev from 'electron-is-dev';

function injectDevelopmentTemplate(event, templates) {
  if (!isDev) return templates;
  const { screenX, screenY } = event;
  const separator = { type: 'separator' };
  const developmentTemplateAddition = [
    {
      label: 'Inspect element',
      accelerator: 'CmdOrCtrl+Shift+I',
      click: () => {
        remote.getCurrentWindow().inspectElement(screenX, screenY);
      },
    },
  ];
  if (templates.length > 0) {
    templates.push(separator);
  }
  templates.push(...developmentTemplateAddition);
  return templates;
}

export function openContextMenu(event, templates = []) {
  const isSomethingSelected = window.getSelection().toString().length > 0;
  const { type } = event.target;
  const isInput = event.target.matches('input') && (type === 'text' || type === 'number');
  const { value } = event.target;

  templates.push({
    label: 'Copy',
    accelerator: 'CmdOrCtrl+C',
    role: 'copy',
    enabled: isSomethingSelected,
  });

  // If context menu is opened on Input and there is text on the input and something is selected.
  const { selectionStart, selectionEnd } = event.target;
  if (!!value && isInput && selectionStart !== selectionEnd) {
    templates.push({ label: 'Cut', accelerator: 'CmdOrCtrl+X', role: 'cut' });
  }

  // If context menu is opened on Input and text is present on clipboard
  if (clipboard.readText().length > 0 && isInput) {
    templates.push({
      label: 'Paste',
      accelerator: 'CmdOrCtrl+V',
      role: 'paste',
    });
  }

  // If context menu is opened on Input
  if (isInput && value) {
    templates.push({
      label: 'Select All',
      accelerator: 'CmdOrCtrl+A',
      role: 'selectall',
    });
  }

  injectDevelopmentTemplate(event, templates);
  remote.Menu.buildFromTemplate(templates).popup();
}
export function openCopyLinkMenu(text, event) {
  const templates = [
    {
      label: 'Copy link',
      click: () => {
        clipboard.writeText(text);
      },
    },
  ];
  openContextMenu(event, templates, false);
}

@@ -664,19 +664,76 @@
 tes = %5B%5D
-) %7B
+, canEdit = false) %7B%0A  const %7B type, value %7D = event.target;
 %0A  const
@@ -804,41 +804,8 @@
  0;%0A
-  const %7B type %7D = event.target;%0A
   co
@@ -903,32 +903,76 @@
 nst 
-%7B value %7D = event.target
+isTextField = canEdit %7C%7C isInput %7C%7C event.target.matches('textarea')
 ;%0A%0A 
@@ -1273,21 +1273,25 @@
 ue && is
-Input
+TextField
  && sele
@@ -1521,21 +1521,25 @@
  0 && is
-Input
+TextField
 ) %7B%0A    
@@ -1693,21 +1693,25 @@
   if (is
-Input
+TextField
  && valu
@@ -2115,16 +2115,16 @@
 %7D,%0A  %5D;%0A
+
   openCo
@@ -2153,15 +2153,8 @@
 ates
-, false
 );%0A%7D

refactor context-menu logic