// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import AxiomError from 'axiom/core/error';

/** @typedef ExecuteContext$$module$axiom$bindings$fs$execute_context */
var ExecuteContext;

/** @typedef JsExecutable$$module$axiom$fs$js_executable */
var JsExecutable;

var IMPORT_CMD_USAGE_STRING = 'usage: script <url>';

/**
 * An executable to import cross origin script into the shell.
 *
 * @this {JsExecutable}
 * @param {ExecuteContext} cx
 */
var main = function(cx) {
  cx.ready();
  var arg = cx.arg;

  if (!arg['_'] || (arg['_'].length < 1) || arg['h'] || arg['help']) {
    cx.stdout(IMPORT_CMD_USAGE_STRING + '\n');
    return Promise.resolve(null);
  }

  var url = arg['_'][0];

  // TODO(grv): add timeout as a command line argument.

  var s = document.createElement('script');
  s.src = url;
  s.type = 'text/javascript';

  var state = 0;

  s.ready = function(callback) {
    if (!state) {
      callback(cx);
      cx.closeOk();
      state = 1;
    } else if (state == 1) {
      return cx.closeError(new AxiomError.Runtime(
          'Duplicate call to script callback.'));
    } else {
      cx.closeError();
      return cx.closeError(new AxiomError.Runtime(
          'Import script callback called after a timeout.'));
    }
  };

  document.head.appendChild(s);

  setTimeout(function() {
    // import script request timed out.
    if (!state) {
      state = 2;
      return cx.closeError(new AxiomError.Runtime(
          'Import script requet timed out.'));
    }
  }, 5000);

  return cx.ephemeralPromise;
};

export {main};
export default main;

/**
 * Accept any value for the execute context arg.
 */
main.argSigil = '%';

@@ -1046,65 +1046,61 @@
 ();%0A
+%0A
   var 
-arg
+list
  = cx.
-arg;%0A%0A  if (!arg%5B'_'%5D %7C%7C (arg%5B'_'%5D
+getArg('_', %5B%5D);%0A%0A  if (list
 .length 
 %3C 1)
@@ -1099,39 +1099,33 @@
 gth 
-%3C
+!=
  1
-)
  %7C%7C 
-arg%5B'h'%5D %7C%7C arg%5B
+cx.getArg(
 'help'
-%5D
+)
 ) %7B%0A
@@ -1226,16 +1226,12 @@
 l = 
-arg%5B'_'%5D
+list
 %5B0%5D;
@@ -1478,49 +1478,56 @@
     
-cx.closeOk();%0A      state = 1;%0A    %7D else
+state = 1;%0A      return cx.closeOk();%0A    %7D%0A%0A   
  if 
@@ -1651,45 +1651,14 @@
    %7D
- else %7B%0A      cx.closeError();%0A 
+%0A%0A
     
- 
 retu
@@ -1690,34 +1690,32 @@
 mError.Runtime(%0A
-  
         'Import 
@@ -1758,22 +1758,16 @@
 ut.'));%0A
-    %7D%0A
   %7D;%0A%0A  
@@ -1799,16 +1799,23 @@
 (s);%0A%0A  
+window.
 setTimeo
@@ -1903,39 +1903,32 @@
 tate = 2;%0A      
-return 
 cx.closeError(ne
@@ -1948,27 +1948,16 @@
 Runtime(
-%0A          
 'Import 
@@ -1968,16 +1968,17 @@
 pt reque
+s
 t timed 
@@ -2142,20 +2142,49 @@
 ain.
-argSigil = '%25'
+signature = %7B%0A  'help%7Ch': '?',%0A  '_': '@'%0A%7D
 ;%0A

update script.js to work with new cx arguments code