// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';

foam.CLASS({
  package: 'org.chromium.webidl',
  name: 'PipelineRunner',
  extends: 'foam.box.Runnable',

  requires: [
    'foam.box.LogBox',
    'foam.log.LogLevel',
    'org.chromium.webidl.PipelineMessage',
  ],

  constants: {
    INVALID_ARG: `run() expects a PipelineMessage object!`,
    MISSING_PROPS: 'Missing required properties in PipelineMessage!',
  },

  classes: [
    {
      name: 'ErrorBox',
      extends: 'foam.box.LogBox',

      requires: ['foam.log.LogLevel'],

      properties: [
        {
          name: 'logLevel',
          factory: function() { return this.LogLevel.ERROR; },
        }
      ],

      methods: [
        function send(message) {
          var output = message.object;
          this[this.logLevel.consoleMethodName].apply(this, [
            this.name,
            output instanceof Error ? output.toString() :
                foam.json.Pretty.stringify(message)
          ]);
          this.delegate && this.delegate.send(message);
        }
      ],
    },
  ],

  properties: [
    {
      name: 'errorBox',
      factory: function() {
        return this.ErrorBox.create({
          name: `LogBox:${this.cls_.id}:${this.$UID}`,
        });
      },
    },
  ],

  methods: [
    function fmtErrorMsg(msg) {
      return new Error(`${this.cls_.name}: ${msg}`);
    },
    function run(message) {
      if (!this.PipelineMessage.isInstance(message)) {
        var errorMsg = this.fmtErrorMsg(this.INVALID_ARG);
        this.error(errorMsg);
        return errorMsg;
      }
    },
  ],
});

@@ -545,653 +545,8 @@
 %7D,%0A%0A
-  classes: %5B%0A    %7B%0A      name: 'ErrorBox',%0A      extends: 'foam.box.LogBox',%0A%0A      requires: %5B'foam.log.LogLevel'%5D,%0A%0A      properties: %5B%0A        %7B%0A          name: 'logLevel',%0A          factory: function() %7B return this.LogLevel.ERROR; %7D,%0A        %7D%0A      %5D,%0A%0A      methods: %5B%0A        function send(message) %7B%0A          var output = message.object;%0A          this%5Bthis.logLevel.consoleMethodName%5D.apply(this, %5B%0A            this.name,%0A            output instanceof Error ? output.toString() :%0A                foam.json.Pretty.stringify(message)%0A          %5D);%0A          this.delegate && this.delegate.send(message);%0A        %7D%0A      %5D,%0A    %7D,%0A  %5D,%0A%0A
   pr
@@ -635,21 +635,19 @@
 rn this.
-Error
+Log
 Box.crea
@@ -706,16 +706,57 @@
 $UID%7D%60,%0A
+          logLevel: this.LogLevel.ERROR,%0A
         

Drop non-serializable inner class: ErrorBox