'use strict';

// Livel4 Keyboard shortcuts

// Experiments for more late bound modules... that way the expots won't get frozen!
// idea

// #Duplication with shortcuts.js #TODO

// import lively from "./lively.js"; #TODO does not work anymore...

System.register(["src/client/bound-eval.js", "templates/graph-control.js", "src/client/keyinfo.js"], function (_export, _context) {
  "use strict";

  var boundEval, GraphControl, keyInfo;
  return {
    setters: [function (_srcClientBoundEvalJs) {
      boundEval = _srcClientBoundEvalJs.default;
    }, function (_templatesGraphControlJs) {
      GraphControl = _templatesGraphControlJs.default;
    }, function (_srcClientKeyinfoJs) {
      keyInfo = _srcClientKeyinfoJs.default;
    }],
    execute: function () {
      _recorder_._src_client_keys_js = _recorder_._src_client_keys_js || {};
      _recorder_._src_client_keys_js.boundEval = boundEval;
      _recorder_._src_client_keys_js.GraphControl = GraphControl;
      _recorder_._src_client_keys_js.keyInfo = keyInfo;
      class Keys {

        static getTextSelection() {
          return window.getSelection().toLocaleString();
        }

        static handle(evt) {
          function handledInCodeMirror(evt) {
            return evt.composedPath().find(node => node.tagName == "LIVELY-CODE-MIRROR");
          }

          // #Hack, fix a little but in ContextMenu movement...
          lively.lastScrollTop = document.scrollingElement.scrollTop;
          lively.lastScrollLeft = document.scrollingElement.scrollLeft;

          try {
            const { char, ctrl, shiftKey, altKey, keyCode, charCode } = _recorder_._src_client_keys_js.keyInfo(evt);

            const keyHandlers = [
            // #KeyboardShortcut Ctrl-K open workspace
            ["Open Workspace", ctrl && char == "K", evt => {
              lively.openWorkspace("");
            }],
            // #KeyboardShortcut Ctrl-Shift-F search throughout the whole repository
            ["Search", ctrl && shiftKey && char == "F", evt => {
              lively.openSearchWidget(this.getTextSelection(), null, evt.composedPath()[0]);
            }],
            // #KeyboardShortcut Ctrl-Shift-B open browser
            ["Open Container", shiftKey && ctrl && char == "B", evt => {
              lively.openBrowser(this.getTextSelection());
            }],
            // #KeyboardShortcut Ctrl-Shift-G open sync tool
            ["Open Sync Tool", shiftKey && ctrl && char == "G", evt => {
              // this does not work up on #Jens' windows machine
              lively.openComponentInWindow("lively-sync");
            }],
            // #KeyboardShortcut Ctrl-O open component bin
            ["Open Component Bin", ctrl && char == "O", evt => {
              lively.openComponentInWindow("lively-component-bin");
            }],
            // #KeyboardShortcut Ctrl-J open console
            ["Open Console", !shiftKey && ctrl && char == "J", evt => {
              lively.openComponentInWindow("lively-console");
            }],
            // ["Open DevDocs", ctrl && char == "H", evt => {
            //   lively.openHelpWindow(this.getTextSelection());
            // }],
            // #KeyboardShortcut Ctrl-Alt-G open graph control
            ["Open Graph Control", ctrl && altKey && char == "G", evt => {
              lively.openComponentInWindow("graph-control");
            }],
            // #KeyboardShortcut Ctrl-Alt-D open research diary        
            ["Open Research Diary", ctrl && altKey && char == "D", evt => {
              lively.openComponentInWindow("research-diary");
            }],
            // #KeyboardShortcut Ctrl-Alt-F search graph
            ["Search Graph", ctrl && altKey && char == "F", evt => {
              _recorder_._src_client_keys_js.GraphControl.fullTextSearch(this.getTextSelection());
            }],
            // #KeyboardShortcut Ctrl-Alt-V eval and open in vivide
            ["Eval & Script in Vivide", ctrl && altKey && char == "V", async evt => {
              if (handledInCodeMirror(evt)) {
                return; // code mirror does not stop it's propagation
              }
              let str = window.getSelection().toLocaleString();
              try {
                const letsScript = await System.import('src/client/vivide/vivide.js').then(m => m.letsScript);

                letsScript((await _recorder_._src_client_keys_js.boundEval(str)));
              } catch (e) {
                lively.handleError(e);
              }
            }],
            // #KeyboardShortcut ESC hide search widget
            ["Hide Search Widget", keyCode == 27, evt => {
              lively.hideSearchWidget();
            }],
            // #KeyboardShortcut F8 open generic search widget
            ["Generic Search Widget", keyCode === 119, async evt => {
              const search = document.body.querySelector('lively-generic-search') || (await lively.create('lively-generic-search'));
              document.body.appendChild(search);
              search.setFocus();
            }], ["Do It", ctrl && !altKey && char == "D", evt => {
              if (handledInCodeMirror(evt)) {
                return; // code mirror does not stop it's propagation
              }
              let str = window.getSelection().toLocaleString();
              lively.notify("eval: " + str);
              try {
                _recorder_._src_client_keys_js.boundEval(str);
              } catch (e) {
                lively.handleError(e);
              }
            }]];

            const [name, match, callback] = keyHandlers.find(([name, match]) => match) || [];
            if (callback) {
              callback(evt);
              evt.preventDefault();
            }
          } catch (err) {
            console.log("Error in handleKeyEvent" + err);
          }
        }

        static onKeyUp(evt) {
          if (evt.key == "Alt") {
            evt.stopPropagation();
            evt.preventDefault();
          }
        }
      }

      _export("default", Keys);

      _recorder_._src_client_keys_js.Keys = Keys;
    }
  };
});