"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.NewModuleClass = void 0;
const codegen_csharp_1 = require("@azure-tools/codegen-csharp");
const powershell_declarations_1 = require("../internal/powershell-declarations");
const exports_1 = require("../llcsharp/exports");
class NewModuleClass extends codegen_csharp_1.Class {
    constructor(namespace, state, objectInitializer) {
        var _a, _b;
        super(namespace, 'Module');
        this.state = state;
        // get the name of the client API class
        this.TaskOfHttpResponseMessage = codegen_csharp_1.System.Threading.Tasks.Task(codegen_csharp_1.System.Net.Http.HttpResponseMessage);
        // lets the common code call the signal again (recursive! careful!)
        this.incomingSignalFunc = codegen_csharp_1.System.Func(codegen_csharp_1.dotnet.String, codegen_csharp_1.System.Threading.CancellationToken, codegen_csharp_1.System.Func(codegen_csharp_1.System.EventArgs), 
        /* returns */ codegen_csharp_1.System.Threading.Tasks.Task());
        this.eventListenerFunc = codegen_csharp_1.System.Func(codegen_csharp_1.dotnet.String, codegen_csharp_1.System.Threading.CancellationToken, codegen_csharp_1.System.Func(codegen_csharp_1.System.EventArgs), this.incomingSignalFunc, powershell_declarations_1.InvocationInfo, codegen_csharp_1.dotnet.String, codegen_csharp_1.dotnet.String, codegen_csharp_1.dotnet.String, codegen_csharp_1.System.Exception, 
        /* returns */ codegen_csharp_1.System.Threading.Tasks.Task());
        this.IEventListenerExpanded = [
            codegen_csharp_1.System.Threading.CancellationToken,
            codegen_csharp_1.System.Action(),
            this.incomingSignalFunc,
        ];
        this.nextStep = codegen_csharp_1.System.Func(codegen_csharp_1.System.Net.Http.HttpRequestMessage, ...this.IEventListenerExpanded, 
        /* returns */ this.TaskOfHttpResponseMessage);
        this.initMethod = this.add(new codegen_csharp_1.Method('Init', codegen_csharp_1.dotnet.Void, { description: 'Initialization steps performed after the module is loaded.' }));
        this.pInvocationInfo = new codegen_csharp_1.Parameter('invocationInfo', powershell_declarations_1.InvocationInfo, { description: 'The <see cref="System.Management.Automation.InvocationInfo" /> from the cmdlet' });
        this.pPipeline = new codegen_csharp_1.Parameter('pipeline', exports_1.ClientRuntime.HttpPipeline, { modifier: codegen_csharp_1.ParameterModifier.Ref, description: 'The HttpPipeline for the request' });
        this.pProxy = new codegen_csharp_1.Parameter('proxy', codegen_csharp_1.System.Uri, { description: 'The HTTP Proxy to use.' });
        this.pProxyCredential = new codegen_csharp_1.Parameter('proxyCredential', powershell_declarations_1.PSCredential, { description: 'The HTTP Proxy Credentials' });
        this.pUseDefaultCredentials = new codegen_csharp_1.Parameter('proxyUseDefaultCredentials', codegen_csharp_1.dotnet.Bool, { description: 'True if the proxy should use default credentials' });
        this.pCorrelationId = new codegen_csharp_1.Parameter('correlationId', codegen_csharp_1.dotnet.String, { description: 'the cmdlet\'s correlation id.' });
        this.pParameterName = new codegen_csharp_1.Parameter('parameterName', codegen_csharp_1.dotnet.String, { description: 'The name of the parameter to get the value for.' });
        this.pId = new codegen_csharp_1.Parameter('id', codegen_csharp_1.dotnet.String, { description: 'The ID of the event ' });
        this.pToken = new codegen_csharp_1.Parameter('token', codegen_csharp_1.System.Threading.CancellationToken, { description: 'The cancellation token for the event ' });
        this.pGetEventData = new codegen_csharp_1.Parameter('getEventData', codegen_csharp_1.System.Func(codegen_csharp_1.System.EventArgs), { description: 'A delegate to get the detailed event data' });
        this.pParameterSetName = new codegen_csharp_1.Parameter('parameterSetName', codegen_csharp_1.dotnet.String, { description: 'the cmdlet\'s parameterset name.' });
        this.pParameterSetNameWithDefault = new codegen_csharp_1.Parameter('parameterSetName', codegen_csharp_1.dotnet.String, { description: 'the cmdlet\'s parameterset name.', defaultInitializer: codegen_csharp_1.dotnet.Null });
        this.pExtensibleParameters = new codegen_csharp_1.Parameter('extensibleParameters', codegen_csharp_1.System.Collections.Generic.IDictionary(codegen_csharp_1.dotnet.String, codegen_csharp_1.dotnet.Object), { description: 'a dict for extensible parameters', defaultInitializer: codegen_csharp_1.dotnet.Null });
        this.pProcessRecordId = new codegen_csharp_1.Parameter('processRecordId', codegen_csharp_1.dotnet.String, { description: 'the cmdlet\'s process record correlation id.' });
        this.pException = new codegen_csharp_1.Parameter('exception', codegen_csharp_1.System.Exception, { description: 'the exception that is being thrown (if available)' });
        this.fPipeline = this.add(new codegen_csharp_1.Field('_pipeline', exports_1.ClientRuntime.HttpPipeline, { access: codegen_csharp_1.Access.Private, description: 'the ISendAsync pipeline instance' }));
        this.fPipelineWithProxy = this.add(new codegen_csharp_1.Field('_pipelineWithProxy', exports_1.ClientRuntime.HttpPipeline, { access: codegen_csharp_1.Access.Private, description: 'the ISendAsync pipeline instance (when proxy is enabled)' }));
        this.fHandler = this.add(new codegen_csharp_1.Field('_handler', codegen_csharp_1.System.Net.Http.HttpClientHandler, { initialValue: codegen_csharp_1.System.Net.Http.HttpClientHandler.new() }));
        this.fWebProxy = this.add(new codegen_csharp_1.Field('_webProxy', codegen_csharp_1.System.Net.WebProxy, { initialValue: codegen_csharp_1.System.Net.WebProxy.new() }));
        this.fUseProxy = this.add(new codegen_csharp_1.Field('_useProxy', codegen_csharp_1.dotnet.Bool, { initialValue: codegen_csharp_1.dotnet.False }));
        this.apply(objectInitializer);
        this.partial = true;
        this.description = 'A class that contains the module-common code and data.';
        const $this = this;
        // Lock for the singleton
        const singletonLock = new codegen_csharp_1.Field('_singletonLock', codegen_csharp_1.System.Object, { readonly: codegen_csharp_1.Modifier.ReadOnly, access: codegen_csharp_1.Access.Private, static: codegen_csharp_1.Modifier.Static, initialValue: codegen_csharp_1.System.Object.new() });
        this.add(singletonLock);
        const initLock = new codegen_csharp_1.Field('_initLock', codegen_csharp_1.System.Object, { readonly: codegen_csharp_1.Modifier.ReadOnly, access: codegen_csharp_1.Access.Private, static: codegen_csharp_1.Modifier.Static, initialValue: codegen_csharp_1.System.Object.new() });
        this.add(initLock);
        const fInstance = new codegen_csharp_1.Field('_instance', this, { access: codegen_csharp_1.Access.Private, static: codegen_csharp_1.Modifier.Static });
        const getInstanceFunc = `if (${fInstance.name} == null) { lock (${singletonLock.name}) { if (${fInstance.name} == null) { ${fInstance.name} = new Module(); }}} return ${fInstance.name};`;
        this.add(fInstance);
        this.add(new codegen_csharp_1.Field('_init', codegen_csharp_1.dotnet.Bool, { access: codegen_csharp_1.Access.Private, static: codegen_csharp_1.Modifier.Static, initialValue: codegen_csharp_1.dotnet.False }));
        this.add(new codegen_csharp_1.Property('Instance', this, {
            getAccess: codegen_csharp_1.Access.Public,
            static: codegen_csharp_1.Modifier.Static,
            get: getInstanceFunc,
            description: 'the singleton of this module class'
        }));
        const clientAPI = new codegen_csharp_1.ClassType((_a = this.state.model.language.csharp) === null || _a === void 0 ? void 0 : _a.namespace, ((_b = this.state.model.language.csharp) === null || _b === void 0 ? void 0 : _b.name) || '');
        const clientProperty = this.add(new codegen_csharp_1.Property('ClientAPI', clientAPI, { description: 'The instance of the Client API' }));
        if (this.state.project.azure) {
            this.createAzureInitAndPipeline(namespace);
        }
        else {
            this.createInitAndPipeline(namespace);
        }
        this.add(new codegen_csharp_1.Constructor(this, {
            access: codegen_csharp_1.Access.Private,
            description: 'Creates the module instance.',
            body: function* () {
                yield '// constructor';
                yield clientProperty.assignPrivate(clientAPI.new());
                yield `${$this.fHandler}.Proxy = ${$this.fWebProxy};`;
                yield $this.fPipeline.assignPrivate(exports_1.ClientRuntime.HttpPipeline.new(exports_1.ClientRuntime.HttpClientFactory.new(codegen_csharp_1.System.Net.Http.HttpClient.new())));
                yield $this.fPipelineWithProxy.assignPrivate(exports_1.ClientRuntime.HttpPipeline.new(exports_1.ClientRuntime.HttpClientFactory.new(codegen_csharp_1.System.Net.Http.HttpClient.new($this.fHandler))));
            }
        }));
        /* extensibility points */
        this.add(new codegen_csharp_1.PartialMethod('BeforeCreatePipeline', codegen_csharp_1.dotnet.Void, { parameters: [this.pInvocationInfo, this.pPipeline] }));
        this.add(new codegen_csharp_1.PartialMethod('AfterCreatePipeline', codegen_csharp_1.dotnet.Void, { parameters: [this.pInvocationInfo, this.pPipeline] }));
        this.add(new codegen_csharp_1.PartialMethod('CustomInit', codegen_csharp_1.dotnet.Void));
        /* Setting the Proxy */
        this.add(new codegen_csharp_1.Method('SetProxyConfiguration', codegen_csharp_1.dotnet.Void, {
            parameters: [this.pProxy, this.pProxyCredential, this.pUseDefaultCredentials],
            *body() {
                yield `${$this.fUseProxy} = proxy != null;`;
                yield (0, codegen_csharp_1.If)(`${$this.pProxy} == null`, function* () {
                    yield 'return;';
                });
                yield '// set the proxy configuration';
                yield `${$this.fWebProxy}.Address = proxy;`;
                yield `${$this.fWebProxy}.BypassProxyOnLocal = false;`;
                yield (0, codegen_csharp_1.If)(`${$this.pUseDefaultCredentials}`, function* () {
                    yield `${$this.fWebProxy}.Credentials = null;`;
                    yield `${$this.fWebProxy}.UseDefaultCredentials = true;`;
                });
                yield (0, codegen_csharp_1.Else)(function* () {
                    yield `${$this.fWebProxy}.UseDefaultCredentials = false;`;
                    yield `${$this.fWebProxy}.Credentials = proxyCredential ?.GetNetworkCredential();`;
                });
            }
        }));
    }
    createInitAndPipeline(namespace) {
        const $this = this;
        // Custom Event Listener without Azure Spefic concepts. (ProcessId and CorelationId)
        const customEventListenerFunc = codegen_csharp_1.System.Func(codegen_csharp_1.dotnet.String, codegen_csharp_1.System.Threading.CancellationToken, codegen_csharp_1.System.Func(codegen_csharp_1.System.EventArgs), this.incomingSignalFunc, powershell_declarations_1.InvocationInfo, codegen_csharp_1.dotnet.String, codegen_csharp_1.System.Exception, 
        /* returns */ codegen_csharp_1.System.Threading.Tasks.Task());
        const incomingSignalDelegate = namespace.add(new codegen_csharp_1.Alias('SignalDelegate', this.incomingSignalFunc));
        const eventListenerDelegate = namespace.add(new codegen_csharp_1.Alias('EventListenerDelegate', customEventListenerFunc));
        const EventListener = this.add(new codegen_csharp_1.Property('EventListener', eventListenerDelegate, { description: 'A delegate that gets called for each signalled event' }));
        // non-azure init method
        this.initMethod.add(function* () {
            yield '// called at module init time...';
            yield (0, codegen_csharp_1.If)('_init == false', function* () {
                yield `lock (_initLock) {
    if (_init == false) {
        CustomInit();
        _init = true;
    }
}`;
            });
        });
        this.createPipelineMethod = this.add(new codegen_csharp_1.Method('CreatePipeline', exports_1.ClientRuntime.HttpPipeline, {
            parameters: [this.pInvocationInfo, this.pParameterSetNameWithDefault, this.pExtensibleParameters],
            description: 'Creates an instance of the HttpPipeline for each call.',
            returnsDescription: `An instance of ${exports_1.ClientRuntime.HttpPipeline} for the remote call.`
        }));
        // non-azure createPipeline method
        this.createPipelineMethod.add(function* () {
            const pip = new codegen_csharp_1.LocalVariable('pipeline', exports_1.ClientRuntime.HttpPipeline, { initializer: 'null' });
            yield pip.declarationStatement;
            yield `BeforeCreatePipeline(${$this.pInvocationInfo.use}, ref ${pip});`;
            yield pip.assign(`(${pip} ?? (${$this.fUseProxy} ? ${$this.fPipelineWithProxy} : ${$this.fPipeline})).Clone()`);
            yield `AfterCreatePipeline(${$this.pInvocationInfo.use}, ref ${pip});`;
            yield (0, codegen_csharp_1.Return)(pip);
        });
        this.add(new codegen_csharp_1.LambdaProperty('Name', codegen_csharp_1.dotnet.String, new codegen_csharp_1.StringExpression(this.state.project.moduleName), { description: 'The Name of this module ' }));
        // Add Signal extensibility point
        const pSignal = new codegen_csharp_1.Parameter('signal', incomingSignalDelegate, { description: 'The callback for the event dispatcher ' });
        // Emit signal extensibility points that called EventListenerDelegate, allowing us to handle Signals emitted by the Pipeline in the Auth Module
        const signalImpl = this.add(new codegen_csharp_1.Method('Signal', codegen_csharp_1.System.Threading.Tasks.Task(), {
            parameters: [this.pId, this.pToken, this.pGetEventData, pSignal, this.pInvocationInfo, this.pParameterSetName, this.pException], async: codegen_csharp_1.Modifier.Async,
            description: 'Called to dispatch events to the common module listener',
            returnsDescription: `A <see cref="${codegen_csharp_1.System.Threading.Tasks.Task()}" /> that will be complete when handling of the event is completed.`
        }));
        signalImpl.push((0, codegen_csharp_1.Using)('NoSynchronizationContext', ''));
        signalImpl.add(function* () {
            // Emit call to EventListener after explicit null check.
            // Not using Null-Conditional operator causes Null Reference exception when Func<Task> is null, due to awaiting null Task.
            yield (0, codegen_csharp_1.If)(`${EventListener.value} != null`, `await ${EventListener.value}.Invoke(${$this.pId.value},${$this.pToken.value},${$this.pGetEventData.value}, ${pSignal.value}, ${$this.pInvocationInfo}, ${$this.pParameterSetName},${$this.pException});`);
        });
    }
    createAzureInitAndPipeline(namespace) {
        const $this = this;
        const sendAsyncStep = namespace.add(new codegen_csharp_1.Alias('SendAsyncStepDelegate', codegen_csharp_1.System.Func(codegen_csharp_1.System.Net.Http.HttpRequestMessage, ...this.IEventListenerExpanded, this.nextStep, /* Next( ...) */ 
        /* returns */ this.TaskOfHttpResponseMessage)));
        const isDataPlane = !!this.state.project.endpointResourceIdKeyName;
        const pipelineChangeDelegate = namespace.add(new codegen_csharp_1.Alias('PipelineChangeDelegate', codegen_csharp_1.System.Action(sendAsyncStep.fullDefinition)));
        const getParameterDelegate = namespace.add(new codegen_csharp_1.Alias('GetParameterDelegate', codegen_csharp_1.System.Func(codegen_csharp_1.dotnet.String, /* resourceId */ codegen_csharp_1.dotnet.String, /* moduleName */ powershell_declarations_1.InvocationInfo, /* invocationInfo */ codegen_csharp_1.dotnet.String, /* correlationId */ codegen_csharp_1.dotnet.String, /* parameterName */ 
        /* returns */ codegen_csharp_1.dotnet.Object)));
        const moduleLoadPipelineDelegate = namespace.add(new codegen_csharp_1.Alias('ModuleLoadPipelineDelegate', codegen_csharp_1.System.Action(codegen_csharp_1.dotnet.String, /* resourceId */ codegen_csharp_1.dotnet.String, /* moduleName */ pipelineChangeDelegate.fullDefinition, /* prependStep */ pipelineChangeDelegate.fullDefinition))); /* appendStep */
        const newRequestPipelineDelegate = new codegen_csharp_1.Alias('NewRequestPipelineDelegate', codegen_csharp_1.System.Action(powershell_declarations_1.InvocationInfo, /* invocationInfo */ codegen_csharp_1.dotnet.String, /* correlationId */ codegen_csharp_1.dotnet.String, /* processRecordId */ pipelineChangeDelegate.fullDefinition, /* prependStep */ pipelineChangeDelegate.fullDefinition)); /* appendStep */
        const argumentCompleterDelegate = namespace.add(new codegen_csharp_1.Alias('ArgumentCompleterDelegate', codegen_csharp_1.System.Func(codegen_csharp_1.dotnet.String, /* completerName */ powershell_declarations_1.InvocationInfo, /* invocationInfo */ codegen_csharp_1.dotnet.String, /* correlationId */ codegen_csharp_1.dotnet.StringArray, /* resourceTypes */ codegen_csharp_1.dotnet.StringArray, /* parentResourceParameterNames */ 
        /* returns */ codegen_csharp_1.dotnet.StringArray)));
        const getTelemetryIdDelegate = namespace.add(new codegen_csharp_1.Alias('GetTelemetryIdDelegate', codegen_csharp_1.System.Func(
        /* returns */ codegen_csharp_1.dotnet.String)));
        const telemetryDelegate = namespace.add(new codegen_csharp_1.Alias('TelemetryDelegate', codegen_csharp_1.System.Action(codegen_csharp_1.dotnet.String, /* operation name */ powershell_declarations_1.InvocationInfo, /* invocationInfo */ codegen_csharp_1.dotnet.String, /* parameter set name */ powershell_declarations_1.PSCmdlet)));
        const sanitizerDelegate = new codegen_csharp_1.Alias('SanitizerDelegate', codegen_csharp_1.System.Action(codegen_csharp_1.dotnet.Object, /* sendToPipeline */ codegen_csharp_1.dotnet.String /* telemetryId */));
        const getTelemetryInfoDelegate = new codegen_csharp_1.Alias('GetTelemetryInfoDelegate', codegen_csharp_1.System.Func(codegen_csharp_1.dotnet.String /* telemetryId */, 
        /* returns */ codegen_csharp_1.System.Collections.Generic.Dictionary(codegen_csharp_1.System.String, codegen_csharp_1.System.String)));
        const tokenAudienceConverterDelegate = new codegen_csharp_1.Alias('TokenAudienceConverterDelegate', codegen_csharp_1.System.Func(codegen_csharp_1.dotnet.String, codegen_csharp_1.dotnet.String, codegen_csharp_1.dotnet.String, codegen_csharp_1.dotnet.String, codegen_csharp_1.System.Uri, 
        /* returns */ codegen_csharp_1.dotnet.String));
        const authorizeRequestDelegate = new codegen_csharp_1.Alias('AuthorizeRequestDelegate', codegen_csharp_1.System.Action(powershell_declarations_1.InvocationInfo, codegen_csharp_1.dotnet.String, codegen_csharp_1.dotnet.String, codegen_csharp_1.System.Action(sendAsyncStep.fullDefinition), codegen_csharp_1.System.Action(sendAsyncStep.fullDefinition), tokenAudienceConverterDelegate.fullDefinition, codegen_csharp_1.System.Collections.Generic.IDictionary(codegen_csharp_1.dotnet.String, codegen_csharp_1.dotnet.Object)));
        if (isDataPlane) {
            namespace.add(tokenAudienceConverterDelegate);
            namespace.add(authorizeRequestDelegate);
            namespace.add(new codegen_csharp_1.ImportDirective('System.Collections.Generic'));
        }
        namespace.add(newRequestPipelineDelegate);
        const incomingSignalDelegate = namespace.add(new codegen_csharp_1.Alias('SignalDelegate', this.incomingSignalFunc));
        const eventListenerDelegate = namespace.add(new codegen_csharp_1.Alias('EventListenerDelegate', this.eventListenerFunc));
        namespace.add(new codegen_csharp_1.Alias('NextDelegate', this.nextStep));
        /* AzAccounts VTable properties  */
        const OnModuleLoad = this.add(new codegen_csharp_1.Property('OnModuleLoad', moduleLoadPipelineDelegate, { description: 'The delegate to call when this module is loaded (supporting a commmon module).' }));
        const OnNewRequest = new codegen_csharp_1.Property('OnNewRequest', newRequestPipelineDelegate, { description: 'The delegate to call before each new request (supporting a commmon module).' });
        const AddRequestUserAgentHandler = new codegen_csharp_1.Property('AddRequestUserAgentHandler', newRequestPipelineDelegate, { description: 'The delegate to call before each new request to add request user agent.' });
        const AddPatchRequestUriHandler = new codegen_csharp_1.Property('AddPatchRequestUriHandler', newRequestPipelineDelegate, { description: 'The delegate to call before each new request to patch request uri.' });
        const AddAuthorizeRequestHandler = new codegen_csharp_1.Property('AddAuthorizeRequestHandler', authorizeRequestDelegate, { description: 'The delegate to call before each new request to add authorization.' });
        this.add(new codegen_csharp_1.Property('GetTelemetryId', getTelemetryIdDelegate, { description: 'The delegate to get the telemetry Id.' }));
        this.add(new codegen_csharp_1.Property('Telemetry', telemetryDelegate, { description: 'The delegate for creating a telemetry.' }));
        const SanitizeOutput = this.add(new codegen_csharp_1.Property('SanitizeOutput', sanitizerDelegate, { description: 'The delegate to call in WriteObject to sanitize the output object.' }));
        namespace.add(sanitizerDelegate);
        const GetTelemetryInfo = this.add(new codegen_csharp_1.Property('GetTelemetryInfo', getTelemetryInfoDelegate, { description: 'The delegate to get the telemetry info.' }));
        namespace.add(getTelemetryInfoDelegate);
        if (isDataPlane) {
            this.add(AddRequestUserAgentHandler);
            this.add(AddPatchRequestUriHandler);
            this.add(AddAuthorizeRequestHandler);
        }
        else {
            this.add(OnNewRequest);
        }
        const GetParameterValue = this.add(new codegen_csharp_1.Property('GetParameterValue', getParameterDelegate, { description: 'The delegate to call to get parameter data from a common module.' }));
        const EventListener = this.add(new codegen_csharp_1.Property('EventListener', eventListenerDelegate, { description: 'A delegate that gets called for each signalled event' }));
        const ArgumentCompleter = this.add(new codegen_csharp_1.Property('ArgumentCompleter', argumentCompleterDelegate, { description: 'Gets completion data for azure specific fields' }));
        const ProfileName = this.add(new codegen_csharp_1.Property('ProfileName', codegen_csharp_1.System.String, { description: 'The name of the currently selected Azure profile' }));
        const moduleIdentity = this.add(new codegen_csharp_1.LambdaProperty('Name', codegen_csharp_1.dotnet.String, new codegen_csharp_1.StringExpression(this.state.project.moduleName), { description: 'The Name of this module ' }));
        const currentProfile = this.add(new codegen_csharp_1.Field('Profile', codegen_csharp_1.dotnet.String, { initialValue: codegen_csharp_1.System.String.Empty, description: 'The currently selected profile.' }));
        const moduleResourceId = this.add(new codegen_csharp_1.LambdaProperty('ResourceId', codegen_csharp_1.dotnet.String, new codegen_csharp_1.StringExpression(this.state.project.moduleName), { description: 'The ResourceID for this module (azure arm).' }));
        /* get parameter method (calls azAccounts) */
        this.add(new codegen_csharp_1.LambdaMethod('GetParameter', codegen_csharp_1.dotnet.Object, new codegen_csharp_1.LiteralExpression(`${GetParameterValue.value}?.Invoke( ${moduleResourceId.value}, ${moduleIdentity.value}, ${$this.pInvocationInfo.value}, ${$this.pCorrelationId.value},${$this.pParameterName.value} )`), {
            parameters: [this.pInvocationInfo, this.pCorrelationId, this.pParameterName],
            description: 'Gets parameters from a common module.',
            returnsDescription: 'The parameter value from the common module. (Note: this should be type converted on the way back)'
        }));
        /* signal method (calls azAccounts) */
        const pSignal = new codegen_csharp_1.Parameter('signal', incomingSignalDelegate, { description: 'The callback for the event dispatcher ' });
        const signalImpl = this.add(new codegen_csharp_1.Method('Signal', codegen_csharp_1.System.Threading.Tasks.Task(), {
            parameters: [this.pId, this.pToken, this.pGetEventData, pSignal, this.pInvocationInfo, this.pParameterSetName, this.pCorrelationId, this.pProcessRecordId, this.pException], async: codegen_csharp_1.Modifier.Async,
            description: 'Called to dispatch events to the common module listener',
            returnsDescription: `A <see cref="${codegen_csharp_1.System.Threading.Tasks.Task()}" /> that will be complete when handling of the event is completed.`
        }));
        signalImpl.push((0, codegen_csharp_1.Using)('NoSynchronizationContext', ''));
        signalImpl.add(function* () {
            yield `await ${EventListener.value}?.Invoke(${$this.pId.value},${$this.pToken.value},${$this.pGetEventData.value}, ${pSignal.value}, ${$this.pInvocationInfo}, ${$this.pParameterSetName}, ${$this.pCorrelationId},${$this.pProcessRecordId},${$this.pException});`;
        });
        /* init method */
        this.initMethod.add(function* () {
            yield (0, codegen_csharp_1.If)('_init == false', function* () {
                yield `lock (_initLock) {
    if (_init == false) {
        ${OnModuleLoad.value}?.Invoke( ${moduleResourceId.value}, ${moduleIdentity.value} ,(step)=> { ${$this.fPipeline.value}.Prepend(step); } , (step)=> { ${$this.fPipeline.value}.Append(step); } );
        ${OnModuleLoad.value}?.Invoke( ${moduleResourceId.value}, ${moduleIdentity.value} ,(step)=> { ${$this.fPipelineWithProxy.value}.Prepend(step); } , (step)=> { ${$this.fPipelineWithProxy.value}.Append(step); } );
        CustomInit();
        _init = true;
    }
}`;
            });
        });
        this.createPipelineMethod = this.add(new codegen_csharp_1.Method('CreatePipeline', exports_1.ClientRuntime.HttpPipeline, {
            parameters: [this.pInvocationInfo, this.pCorrelationId, this.pProcessRecordId, this.pParameterSetNameWithDefault, this.pExtensibleParameters],
            description: 'Creates an instance of the HttpPipeline for each call.',
            returnsDescription: `An instance of ${exports_1.ClientRuntime.HttpPipeline} for the remote call.`
        }));
        /* Add following three fields for data plane */
        const fendpointResourceIdKeyName = new codegen_csharp_1.Field('_endpointResourceIdKeyName', codegen_csharp_1.dotnet.String, { access: codegen_csharp_1.Access.Private, initialValue: new codegen_csharp_1.StringExpression(this.state.project.endpointResourceIdKeyName) });
        const fEndpointSuffixKeyName = new codegen_csharp_1.Field('_endpointSuffixKeyName', codegen_csharp_1.dotnet.String, { access: codegen_csharp_1.Access.Private, initialValue: new codegen_csharp_1.StringExpression(this.state.project.endpointSuffixKeyName) });
        const fTokenAudienceConverter = new codegen_csharp_1.Field('_tokenAudienceConverter', tokenAudienceConverterDelegate, { access: codegen_csharp_1.Access.Private, initialValue: 'null' });
        if (isDataPlane) {
            this.add(fendpointResourceIdKeyName);
            this.add(fEndpointSuffixKeyName);
            this.add(fTokenAudienceConverter);
        }
        /* pipeline create method */
        this.createPipelineMethod.add(function* () {
            const pip = new codegen_csharp_1.LocalVariable('pipeline', exports_1.ClientRuntime.HttpPipeline, { initializer: 'null' });
            yield pip.declarationStatement;
            yield `BeforeCreatePipeline(${$this.pInvocationInfo.use}, ref ${pip});`;
            yield pip.assign(`(${pip} ?? (${$this.fUseProxy} ? ${$this.fPipelineWithProxy} : ${$this.fPipeline})).Clone()`);
            yield `AfterCreatePipeline(${$this.pInvocationInfo.use}, ref ${pip});`;
            yield `pipeline.Append(new Runtime.CmdInfoHandler(${$this.pProcessRecordId}, ${$this.pInvocationInfo.use}, ${$this.pParameterSetName}).SendAsync);`;
            if (isDataPlane) {
                yield `${AddRequestUserAgentHandler.value}?.Invoke( ${$this.pInvocationInfo.use}, ${$this.pCorrelationId},${$this.pProcessRecordId}, (step)=> { ${pip}.Prepend(step); } , (step)=> { ${pip}.Append(step); } );`;
                yield `${AddPatchRequestUriHandler.value}?.Invoke( ${$this.pInvocationInfo.use}, ${$this.pCorrelationId},${$this.pProcessRecordId}, (step)=> { ${pip}.Prepend(step); } , (step)=> { ${pip}.Append(step); } );`;
                yield `${AddAuthorizeRequestHandler.value}?.Invoke( ${$this.pInvocationInfo.use}, ${fendpointResourceIdKeyName},${fEndpointSuffixKeyName}, (step)=> { ${pip}.Prepend(step); } , (step)=> { ${pip}.Append(step); }, ${fTokenAudienceConverter}, ${$this.pExtensibleParameters} );`;
            }
            else {
                yield `${OnNewRequest.value}?.Invoke( ${$this.pInvocationInfo.use}, ${$this.pCorrelationId},${$this.pProcessRecordId}, (step)=> { ${pip}.Prepend(step); } , (step)=> { ${pip}.Append(step); } );`;
            }
            yield (0, codegen_csharp_1.Return)(pip);
        });
    }
}
exports.NewModuleClass = NewModuleClass;
//# sourceMappingURL=module-class.js.map