/*!
 Copyright (C) 2017 Google Inc., authors, and contributors
 Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
 */

(function (can, GGRC) {
  'use strict';

  var tag = 'object-state-toolbar';
  var tpl = can.view(GGRC.mustache_path +
    '/components/object-state-toolbar/object-state-toolbar.mustache');
  /**
   * Object State Toolbar Component allowing Object state modification
   */
  can.Component.extend('objectStateToolbar', {
    tag: tag,
    template: tpl,
    viewModel: {
      instance: null,
      hasVerifiers: false,
      isCurrentUserVerifier: false,
      isDisabled: function () {
        return !!this.attr('instance._disabled');
      },
      errorMsg: function () {
        return this.attr('instance._mandatory_msg');
      },
      isInProgressOrNotStarted: function () {
        return this.attr('instance.status') === 'In Progress' ||
          this.attr('instance.status') === 'Not Started';
      },
      isInProgress: function () {
        return this.attr('instance.status') === 'In Progress';
      },
      isInReview: function () {
        return this.attr('instance.status') === 'Ready for Review';
      },
      hasErrors: function () {
        return this.attr('instance._mandatory_msg');
      },
      checkIsCurrentUserVerifier: function (verifiers) {
        var isVerifier = false;
        verifiers.each(function (verifier) {
          if (verifier.instance.email === GGRC.current_user.email) {
            isVerifier = true;
            return false;
          }
        });
        return isVerifier;
      },
      refreshVerifiers: function () {
        this.attr('instance')
          .get_binding('related_verifiers')
          .refresh_instances()
          .done(function (verifiers) {
            this.attr('hasVerifiers', !!verifiers.length);
            this.attr('isCurrentUserVerifier',
              this.checkIsCurrentUserVerifier(verifiers));
          }.bind(this));
      }
    },
    init: function () {
      this.viewModel.refreshVerifiers();
    },
    events: {
      '{viewModel.instance} related_sources': function () {
        this.viewModel.refreshVerifiers();
      },
      '{viewModel.instance} related_destinations': function () {
        this.viewModel.refreshVerifiers();
      }
    }
  });
})(window.can, window.GGRC);

@@ -333,14 +333,285 @@
 ');%0A
+  // Helper function - might be some util/helpers method%0A  function checkIsCurrentUserVerifier(verifiers) %7B%0A    var isVerifier = verifiers.filter(function (verifier) %7B%0A      return verifier.email === GGRC.current_user.email;%0A    %7D);%0A    return !!(isVerifier.length);%0A  %7D%0A
   /**%0A
-
    *
@@ -685,19 +685,20 @@
    */%0A  
-can
+GGRC
 .Compone
@@ -703,15 +703,9 @@
 nent
-.extend
+s
 ('ob
@@ -785,42 +785,291 @@
     
-instance: null,%0A      hasVerifiers
+define: %7B%0A        isCurrentUserVerifier: %7B%0A          type: 'boolean',%0A          value: false,%0A          get: function () %7B%0A            return checkIsCurrentUserVerifier(this.attr('verifiers'));%0A          %7D%0A        %7D,%0A        hasVerifiers: %7B%0A          type: 'boolean',%0A          value
 : fa
@@ -1083,36 +1083,155 @@
     
+   
-isCurrentUserV
+ get: function () %7B%0A            return !!(this.attr('verifiers').length);%0A          %7D%0A        %7D%0A      %7D,%0A      v
 erifier
+s
 : 
-false
+%5B%5D,%0A      instance: %7B%7D
 ,%0A  
@@ -1368,44 +1368,42 @@
 urn 
-this.attr('instance._mandatory_msg')
+'Assessment has validation errors'
 ;%0A  
@@ -1872,1045 +1872,29 @@
 nce.
-_mandatory_msg');%0A      %7D,%0A      checkIsCurrentUserVerifier: function (verifiers) %7B%0A        var isVerifier = false;%0A        verifiers.each(function (verifier) %7B%0A          if (verifier.instance.email === GGRC.current_user.email) %7B%0A            isVerifier = true;%0A            return false;%0A          %7D%0A        %7D);%0A        return isVerifier;%0A      %7D,%0A      refreshVerifiers: function () %7B%0A        this.attr('instance')%0A          .get_binding('related_verifiers')%0A          .refresh_instances()%0A          .done(function (verifiers) %7B%0A            this.attr('hasVerifiers', !!verifiers.length);%0A            this.attr('isCurrentUserVerifier',%0A              this.checkIsCurrentUserVerifier(verifiers));%0A          %7D.bind(this));%0A      %7D%0A    %7D,%0A    init: function () %7B%0A      this.viewModel.refreshVerifiers();%0A    %7D,%0A    events: %7B%0A      '%7BviewModel.instance%7D related_sources': function () %7B%0A        this.viewModel.refreshVerifiers();%0A      %7D,%0A      '%7BviewModel.instance%7D related_destinations': function () %7B%0A        this.viewModel.refreshVerifiers(
+preconditions_failed'
 );%0A 

Simplify Assessment Object State Component