parameters:
  - name: SubscriptionConfiguration
    type: string
    default: '{}'
  - name: SubscriptionConfigurations
    type: object
    default: null
  # EnvVars is used to help diagnose variable conflict issues early
  - name: EnvVars
    type: object
    default: {}
  - name: SubscriptionConfigurationFilePaths
    type: object
    default: null

steps:
  - task: AzurePowerShell@5
    displayName: Set Pipeline Subnet Info
    condition: and(succeeded(), ne(variables['Pool'], ''))
    env: ${{ parameters.EnvVars }}
    inputs:
      azureSubscription: azure-sdk-tests
      azurePowerShellVersion: LatestVersion
      pwsh: true
      ScriptType: InlineScript
      Inline: |
        Set-AzContext 'Azure SDK Engineering System'
        if ($env:Pool -eq 'Azure Pipelines') {
          Write-Host "Skipping pool subnet lookup when running on an Azure Pipelines hosted pool"
          Write-Host "##vso[task.setvariable variable=PoolSubnet;]"
          exit 0
        }
        $poolSubnet = (Get-AzResource -ResourceGroupName azsdk-pools -Name $env:Pool -ExpandProperties).Properties.networkProfile.subnetId
        Write-Host "Setting pipeline subnet env variable PoolSubnet to '$poolSubnet'"
        Write-Host "##vso[task.setvariable variable=PoolSubnet;]$poolSubnet"

  - pwsh: |
      . ./eng/common/TestResources/SubConfig-Helpers.ps1

      $baseSubConfigJson = @'
        ${{ parameters.SubscriptionConfiguration }}
      '@.Trim()

      $additionalSubConfigsJson = @'
        ${{ convertToJson(parameters.SubscriptionConfigurations) }}
      '@.Trim() -replace '"{', '{' -replace '}"', '}'

      $subConfigFilesJson = @'
        ${{ convertToJson(parameters.SubscriptionConfigurationFilePaths) }}
      '@.Trim()

      BuildAndSetSubscriptionConfig $baseSubConfigJson $additionalSubConfigsJson $subConfigFilesJson
    displayName: Merge subscription configurations
