import type { ApplicationInsightsComponent , ApplicationInsightsComponentListResult } from '@azure/arm-appinsights/esm/models' ; import { AzureWizardPromptStep } from 'vscode-azureextensionui' ; import { localize } from '../localize' ; import { AppInsightsListStep , appInsightsNamingRules } from './AppInsightsListStep' ; import { IAppServiceWizardContext } from './IAppServiceWizardContext' ; export class AppInsightsNameStep extends AzureWizardPromptStep < IAppServiceWizardContext > { public async isNameAvailable ( context , name ) { const appInsightsComponents = await AppInsightsListStep . getAppInsightsComponents ( context ) ; return ! appInsightsComponents . some ( ( ai ) => ai . name !== undefined && ai . name . toLowerCase ( ) === name . toLowerCase ( ) ) ; } public async prompt ( context ) { const suggestedName = context . relatedNameTask ? await context . relatedNameTask : undefined ; context . newAppInsightsName = ( await context . ui . showInputBox ( { value : suggestedName , prompt : 'Enter the name of the new Application Insights resource.' , validateInput : async ( value ) : Promise < string | undefined > => await this . validateApplicationInsightName ( context , value ) } ) ) . trim ( ) ; context . valuesToMask . push ( context . newAppInsightsName ) ; } public shouldPrompt ( context ) { return ! context . newAppInsightsName ; } private async validateApplicationInsightName ( context , name ) { name = name . trim ( ) ; if ( name . length < appInsightsNamingRules . minLength || name . length > appInsightsNamingRules . maxLength ) { return localize ( 'invalidLength' , 'The name must be between {0} and {1} characters.' , appInsightsNamingRules . minLength , appInsightsNamingRules . maxLength ) ; } else if ( appInsightsNamingRules . invalidCharsRegExp . test ( name ) ) { return localize ( 'invalidChars' , "The name can only contain alphanumeric characters or the symbols ._-()" ) ; } else if ( name . endsWith ( '.' ) ) { return localize ( 'invalidEndingChar' , "The name cannot end in a period." ) ; } else if ( ! await this . isNameAvailable ( context , name ) ) { return localize ( 'nameAlreadyExists' , 'Application Insights resource "{0}" already exists in subscription "{1}".' , name , context . subscriptionDisplayName ) ; } else { return undefined ; } } }