text: /********************************************************* {COPYRIGHT-TOP} ***
   * Licensed Materials - Property of IBM
   * 5725-Z22, 5725-Z63, 5725-U33, 5725-Z63
   *
   * (C) Copyright IBM Corporation 2015, 2017
   *
   * All Rights Reserved.
   * US Government Users Restricted Rights - Use, duplication or disclosure
   * restricted by GSA ADP Schedule Contract with IBM Corp.
   ********************************************************** {COPYRIGHT-END} **/
  // Node module: apim-ui
  
  'use strict';
  
  angular.module('apim.apis').component('apiPathOperation', {
    bindings: {
      path: '<',
      operation: '<',
      onDelete: '&'
    },
    require: {
      listCtrl: '^apiPathOperationsList'
    },
    templateUrl: 'apim/apis/partials/api-path-operation.html',
    controller: function() {
      var $ctrl = this;
  
      this.$onInit = function() {
        $ctrl.tag = {};
        $ctrl.xIbmConfig = this.listCtrl.xIbmConfig;
        $ctrl.tagArray = this.listCtrl.tagArray;
        $ctrl.apiSecurityDefns = this.listCtrl.pathCtrl.apiSecurityDefns;
        if ($ctrl.operation.tags) {
          $ctrl.operation.tags.forEach(function(tag) {
            if ($ctrl.tagArray.indexOf(tag) < 0) {
              $ctrl.tagArray.push(tag);
            }
          });
        }
      };
  
      this.delete = function($event) {
        $event.stopPropagation();
        $ctrl.onDelete({
          operation: $ctrl.operation
        });
      };
  
      this.isExpanded = function() {
        return Boolean($ctrl._expanded);
      };
  
      this.setExpanded = function() {
        $ctrl._expanded = !($ctrl.isExpanded());
      };
  
      this.addTag = function($event) {
        if ($event) {
          $event.stopPropagation();
        }
        if ($ctrl.tag.newTag && (!$ctrl.operation.tags ||
          $ctrl.operation.tags.indexOf($ctrl.tag.newTag) < 0)) {
          if (!$ctrl.operation.tags) {
            $ctrl.operation.tags = [];
          }
          $ctrl.operation.tags.push($ctrl.tag.newTag);
        }
        $ctrl.tag.newTag = '';
      };
  
      this.removeTag = function($event, tag) {
        if ($event) {
          $event.stopPropagation();
        }
        var index = $ctrl.operation.tags.indexOf(tag);
        if (index >= 0) {
          $ctrl.operation.tags.splice(index, 1);
        }
      }
  
      this.$operationId = function(newValue) {
        if (newValue) {
          // Setter
          var oldValue = $ctrl.operation.operationId;
          $ctrl.operation.operationId = newValue;
          updateAssemblyModel(newValue, oldValue);
        }
        return $ctrl.operation.operationId;
      };
  
      function updateAssemblyModel(newValue, oldValue) {
        // ensure the assembly model is up to date
        if ($ctrl.xIbmConfig &&
          $ctrl.xIbmConfig.assembly &&
          $ctrl.xIbmConfig.assembly.execute) {
          var matches = _.filter($ctrl.xIbmConfig.assembly.execute, {
            'operation-switch': {
              'case': [{
                operations: [oldValue]
              }]
            }
          });
  
          matches.forEach(function(policy) {
            var caseMatches = _.filter(policy['operation-switch']['case'], {
              operations: [oldValue]
            });
  
            caseMatches.forEach(function(thisCase) {
              var operationMatches = _.filter(thisCase.operations, oldValue);
              for (var i = 0; i < thisCase.operations.length; i++) {
                if (typeof thisCase.operations[i] == 'string' && thisCase.operations[i] == oldValue) {
                  thisCase.operations[i] = newValue;
                }
              }
            });
          });
        }
      }
    }
  });
  