'use strict';

import _ from 'lodash';
import { hri as HumanReadableIds } from 'human-readable-ids';

import { PipelineTemplateReader } from './templates/PipelineTemplateReader';
import { PipelineTemplateV2Service } from 'core/pipeline';

const angular = require('angular');

module.exports = angular
  .module('spinnaker.core.pipeline.config.controller', [require('@uirouter/angularjs').default])
  .controller('PipelineConfigCtrl', [
    '$scope',
    '$state',
    '$stateParams',
    'app',
    function($scope, $state, $stateParams, app) {
      this.application = app;
      this.state = {
        pipelinesLoaded: false,
      };

      this.containsJinja = source => source && (source.includes('{{') || source.includes('{%'));

      this.initialize = () => {
        this.pipelineConfig = _.find(app.pipelineConfigs.data, { id: $stateParams.pipelineId });

        if (this.pipelineConfig && PipelineTemplateV2Service.isV2PipelineConfig(this.pipelineConfig)) {
          return $state.go('home.applications.application.pipelines.executions', null, { location: 'replace' });
        }

        if (this.pipelineConfig && this.pipelineConfig.expectedArtifacts) {
          for (const artifact of this.pipelineConfig.expectedArtifacts) {
            if (!artifact.displayName || artifact.displayName.length === 0) {
              // default display names are created for the sake of readability for pipelines that existed
              // before display names were introduced to expected artifacts
              const { kind, name } = artifact.matchArtifact;

              const dockerDisplayName = () => {
                const [, project = 'no-project', image = 'no-image'] = name.split('/');
                return `DOCKER-IMAGE-${project}/${image}`;
              };

              const bucketDisplayName = () => {
                const [, bucket = 'no-bucket', ...path] = name.split(/\/+/);
                return `${kind.toUpperCase()}-${bucket}/${path.join('/') || 'no-file'}`;
              };

              const httpDisplayName = () => {
                const [, host = 'no-host', ...file] = name.split(/\/+/);
                return `HTTP-${host}/${file.join('/') || 'no-file'}`;
              };

              switch (kind) {
                case 'docker':
                  artifact.displayName = dockerDisplayName();
                  break;
                case 'gcs':
                case 's3':
                  artifact.displayName = bucketDisplayName();
                  break;
                case 'http':
                  artifact.displayName = httpDisplayName();
                  break;
                case 'bitbucket':
                case 'github':
                  artifact.displayName = `${kind.toUpperCase()}-${name}`;
                  break;
                case 'helm':
                  artifact.displayName = `HELM-${name}:${artifact.matchArtifact.version}`;
                  break;
                case 'maven':
                case 'ivy':
                  artifact.displayName = `${kind.toUpperCase()}-${name}`;
                  break;
                default:
                  artifact.displayName = HumanReadableIds.random();
              }
            }
          }
        }

        if (this.pipelineConfig && this.pipelineConfig.type === 'templatedPipeline') {
          this.isTemplatedPipeline = true;
          this.hasDynamicSource = this.containsJinja(this.pipelineConfig.config.pipeline.template.source);
          if (!this.pipelineConfig.isNew) {
            return PipelineTemplateReader.getPipelinePlan(this.pipelineConfig, $stateParams.executionId)
              .then(plan => (this.pipelinePlan = plan))
              .catch(error => {
                this.templateError = error;
                this.pipelineConfig.isNew = true;
              });
          }
        } else if (!this.pipelineConfig) {
          this.pipelineConfig = _.find(app.strategyConfigs.data, { id: $stateParams.pipelineId });
          if (!this.pipelineConfig) {
            this.state.notFound = true;
          }
        }
        if (this.pipelineConfig) {
          this.pipelineConfig = _.cloneDeep(this.pipelineConfig);
        }
      };

      if (!app.notFound) {
        app.pipelineConfigs.activate();
        app.pipelineConfigs
          .ready()
          .then(this.initialize)
          .then(() => (this.state.pipelinesLoaded = true));
      }
    },
  ]);

@@ -1319,190 +1319,8 @@
 ) %7B%0A
-              // default display names are created for the sake of readability for pipelines that existed%0A              // before display names were introduced to expected artifacts%0A
     
@@ -1340,14 +1340,8 @@
 st %7B
- kind,
  nam
@@ -1370,17 +1370,16 @@
 tifact;%0A
-%0A
         
@@ -1388,1023 +1388,63 @@
     
-const dockerDisplayName = () =%3E %7B%0A                const %5B, project = 'no-project', image = 'no-image'%5D = name.split('/');%0A                return %60DOCKER-IMAGE-$%7Bproject%7D/$%7Bimage%7D%60;%0A              %7D;%0A%0A              const bucketDisplayName = () =%3E %7B%0A                const %5B, bucket = 'no-bucket', ...path%5D = name.split(/%5C/+/);%0A                return %60$%7Bkind.toUpperCase()%7D-$%7Bbucket%7D/$%7Bpath.join('/') %7C%7C 'no-file'%7D%60;%0A              %7D;%0A%0A              const httpDisplayName = () =%3E %7B%0A                const %5B, host = 'no-host', ...file%5D = name.split(/%5C/+/);%0A                return %60HTTP-$%7Bhost%7D/$%7Bfile.join('/') %7C%7C 'no-file'%7D%60;%0A              %7D;%0A%0A              switch (kind) %7B%0A                case 'docker':%0A                  artifact.displayName = dockerDisplayName();%0A                  break;%0A                case 'gcs':%0A                case 's3':%0A                  artifact.displayName = bucketDisplayName();%0A                  break;%0A                case 'http':%0A                  artifact.displayName = httpDisplayN
+if (name) %7B%0A                artifact.displayName = n
 ame
-()
 ;%0A  
@@ -1459,512 +1459,17 @@
     
-    break;%0A                case 'bitbucket':%0A                case 'github':%0A                  artifact.displayName = %60$%7Bkind.toUpperCase()%7D-$%7Bname%7D%60;%0A                  break;%0A                case 'helm':%0A                  artifact.displayName = %60HELM-$%7Bname%7D:$%7Bartifact.matchArtifact.version%7D%60;%0A                  break;%0A                case 'maven':%0A                case 'ivy':%0A                  artifact.displayName = %60$%7Bkind.toUpperCase()%7D-$%7Bname%7D%60;%0A                  break;%0A                default:%0A  
+%7D else %7B%0A
     

Simplify display name generation (#6670)