text: 'use strict';
  
  const resolveLambdaTarget = require('../../../../../utils/resolveLambdaTarget');
  
  module.exports = {
    compileTargetGroups() {
      this.validated.events.forEach(event => {
        const { functionName, albId, multiValueHeaders = false } = event;
  
        const targetGroupLogicalId = this.provider.naming.getAlbTargetGroupLogicalId(
          functionName,
          albId,
          multiValueHeaders
        );
        const registerTargetPermissionLogicalId = this.provider.naming.getLambdaRegisterTargetPermissionLogicalId(
          functionName
        );
  
        const functionObj = this.serverless.service.getFunction(functionName);
        const TargetGroup = {
          Type: 'AWS::ElasticLoadBalancingV2::TargetGroup',
          Properties: {
            TargetType: 'lambda',
            Targets: [
              {
                Id: resolveLambdaTarget(functionName, functionObj),
              },
            ],
            Name: this.provider.naming.getAlbTargetGroupName(functionName, albId, multiValueHeaders),
            Tags: [
              {
                Key: 'Name',
                Value: this.provider.naming.getAlbTargetGroupNameTagValue(functionName, albId),
              },
            ],
            TargetGroupAttributes: [
              {
                Key: 'lambda.multi_value_headers.enabled',
                Value: multiValueHeaders,
              },
            ],
          },
          DependsOn: [registerTargetPermissionLogicalId],
        };
        Object.assign(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
          [targetGroupLogicalId]: TargetGroup,
        });
      });
    },
  };
  