"use strict";
/* eslint-disable no-useless-escape */
/*---------------------------------------------------------------------------------------------
 *  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.generateScriptCmdlets = void 0;
const codegen_1 = require("@azure-tools/codegen");
const linq_1 = require("@azure-tools/linq");
const psscript_file_1 = require("../file-formats/psscript-file");
const exports_1 = require("../llcsharp/exports");
function getType(type) {
    if (type) {
        switch (type.toLowerCase()) {
            case 'string':
                return 'System.String';
            case 'int':
            case 'number':
                return 'System.Int';
            case 'boolean':
            case 'bool':
            case 'switch':
                return 'System.Management.Automation.SwitchParameter';
            case 'object':
                return 'System.Object';
        }
    }
    return type;
}
async function generateScriptCmdlets(project) {
    const commands = await project.state.getValue('command', []);
    for (const command of (0, linq_1.values)(commands)) {
        if (!command.action) {
            await project.state.error(`Scenario command is missing 'action' : ${(0, codegen_1.serialize)(command)}`, []);
            continue;
        }
        if (!command.subject) {
            await project.state.error(`Scenario command is missing 'subject' : ${(0, codegen_1.serialize)(command)}`, []);
            continue;
        }
        const verbMap = await project.state.getValue('verb-mapping', new linq_1.Dictionary());
        command.verb = verbMap[command.action] || command.action.capitalize();
        command.variant = command.variant ? `_${(0, codegen_1.pascalCase)(command.variant)}` : '';
        command.subject = (0, codegen_1.pascalCase)(command.subject);
        const filename = `${project.customFolder}/${command.verb}-${command.subject}${command.variant}.ps1`;
        const content = await project.state.readFile(filename);
        if (content) {
            project.state.writeFile(filename, content, undefined, 'source-file-powershell');
        }
        else {
            (0, codegen_1.setIndentation)(2);
            const script = new psscript_file_1.PSScriptFile();
            script.add(function* () {
                yield `<#
.Synopsis
${command.description || ''}
.Description
${command.description || ''}
.Link
${command.link || ''}
#>`;
                yield `function ${command.verb}-${command.subject}${command.variant} {`;
                if (command.output) {
                    yield `[OutputType('${getType(command.output)}')]`;
                }
                yield command.writeable ? '[CmdletBinding(PositionalBinding=$false, SupportsShouldProcess, ConfirmImpact=\'Medium\')]' : '[CmdletBinding(PositionalBinding=$false)]';
                yield 'param(';
                if (command.parameters) {
                    for (const { key: name, value: parameter } of (0, linq_1.items)(command.parameters)) {
                        yield (0, codegen_1.indent)(`[Parameter(${parameter.required === false ? '' : 'Mandatory'})]`);
                        yield (0, codegen_1.indent)(`[${getType(parameter.type)}]`);
                        if (parameter.description) {
                            yield (0, codegen_1.indent)(`# ${parameter.description}`);
                        }
                        yield (0, codegen_1.indent)(`$\{${(0, codegen_1.pascalCase)(name)}\},`);
                        yield '';
                    }
                }
                if (project.azure) {
                    yield (0, codegen_1.indent)(`
[Parameter()]
[Alias('AzureRMContext', 'AzureCredential')]
[ValidateNotNull()]
[System.Management.Automation.PSObject]
# The DefaultProfile parameter is not functional. Use the SubscriptionId parameter when available if executing the cmdlet against a different subscription.
$\{DefaultProfile\},`);
                    yield '';
                }
                if (command['as-job']) {
                    yield (0, codegen_1.indent)(`
[Parameter()]
[System.Management.Automation.SwitchParameter]
# Run the command as a job
$\{AsJob\},`);
                    yield '';
                }
                yield (0, codegen_1.indent)(`
[Parameter(DontShow)]
[System.Management.Automation.SwitchParameter]
# Wait for .NET debugger to attach
$\{Break\},

[Parameter(DontShow)]
[ValidateNotNull()]
[${exports_1.ClientRuntime.SendAsyncSteps}
# SendAsync Pipeline Steps to be appended to the front of the pipeline
$\{HttpPipelineAppend\},

[Parameter(DontShow)]
[ValidateNotNull()]
[${exports_1.ClientRuntime.SendAsyncSteps}]
# SendAsync Pipeline Steps to be prepended to the front of the pipeline
$\{HttpPipelinePrepend\},

[Parameter(DontShow)]
[System.Uri]
# The URI for the proxy server to use
$\{Proxy\},

[Parameter(DontShow)]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
# Credentials for a proxy server to use for the remote call
$\{ProxyCredential\},

[Parameter(DontShow)]
[System.Management.Automation.SwitchParameter]
# Use the default credentials for the proxy
$\{ProxyUseDefaultCredentials\}
`);
                yield ')';
                yield (0, codegen_1.indent)(`process {
  try {
    # do something with your custom parameters
    # $PSBoundParameters.Add("Filter", "appId eq '$ApplicationId'") | Out-Null
    # $PSBoundParameters.Remove("ApplicationId") | Out-Null

    # Examples
    # To call a specific variant in the private module
    # ${project.prefix}${project.moduleName}.private\\${command.verb}-${command.subject}_SomeVariant @PSBoundParameters

    # To call back to the same public module (and call the exported cmdlet)
    # ${project.prefix}${project.moduleName}\\${command.verb}-${command.subject} @PSBoundParameters

    # To call something in the internal module
    # ${project.prefix}${project.moduleName}.internal\\${command.verb}-${command.subject} @PSBoundParameters

  } catch {
    throw
  }
}`);
                yield '}';
            });
            project.state.writeFile(filename, (0, codegen_1.applyOverrides)(`${script}`, project.overrides), undefined, 'source-file-powershell');
        }
    }
    /*
    <#
    .Synopsis
    Export data from the redis cache to blobs in a container.
    .Description
    Export data from the redis cache to blobs in a container.
    .Link
    https://learn.microsoft.com/powershell/module/az.redis/export-azredisredisdata
    #>
    function Export-AzRedisRedisData {
    [OutputType('System.Boolean')]
    [CmdletBinding(DefaultParameterSetName='ExportSubscriptionIdViaHost', SupportsShouldProcess, ConfirmImpact='Medium')]
    param(
        [Parameter(Mandatory)]
        [System.String]
        # The name of the Redis cache.
        ${Name},
    
        [Parameter(Mandatory)]
        [System.String]
        # The name of the resource group.
        ${ResourceGroupName},
    
        [Parameter(ParameterSetName='Export', Mandatory)]
        [Parameter(ParameterSetName='ExportExpanded', Mandatory)]
        [System.String]
        # Gets subscription credentials which uniquely identify the Microsoft Azure subscription. The subscription ID forms part of the URI for every service call.
        ${SubscriptionId},
    
        [Parameter(ParameterSetName='Export', ValueFromPipeline)]
        [Parameter(ParameterSetName='ExportSubscriptionIdViaHost', ValueFromPipeline)]
        [Microsoft.Azure.PowerShell.Cmdlets.Redis.Models.Api20180301.IExportRdbParameters]
        # Parameters for Redis export operation.
        ${Parameters},
    
        [Parameter()]
        [System.Management.Automation.SwitchParameter]
        # When specified, PassThru will force the cmdlet return a 'bool' given that there isn't a return type by default.
        ${PassThru},
    
        [Parameter(ParameterSetName='ExportExpanded', Mandatory)]
        [Parameter(ParameterSetName='ExportSubscriptionIdViaHostExpanded', Mandatory)]
        [System.String]
        # Container name to export to.
        ${Container},
    
        [Parameter(ParameterSetName='ExportExpanded')]
        [Parameter(ParameterSetName='ExportSubscriptionIdViaHostExpanded')]
        [System.String]
        # File format.
        ${Format},
    
        [Parameter(ParameterSetName='ExportExpanded', Mandatory)]
        [Parameter(ParameterSetName='ExportSubscriptionIdViaHostExpanded', Mandatory)]
        [System.String]
        # Prefix to use for exported files.
        ${Prefix},
    
        [Parameter()]
        [Alias('AzureRMContext', 'AzureCredential')]
        [ValidateNotNull()]
        [System.Management.Automation.PSObject]
        # The DefaultProfile parameter is not functional. Use the SubscriptionId parameter when available if executing the cmdlet against a different subscription.
        ${DefaultProfile},
    
        [Parameter()]
        [System.Management.Automation.SwitchParameter]
        # Run the command as a job
        ${AsJob},
    
        [Parameter(DontShow)]
        [System.Management.Automation.SwitchParameter]
        # Wait for .NET debugger to attach
        ${Break},
    
        [Parameter(DontShow)]
        [ValidateNotNull()]
        [Microsoft.Azure.PowerShell.Cmdlets.Redis.Runtime.SendAsyncStep[]]
        # SendAsync Pipeline Steps to be appended to the front of the pipeline
        ${HttpPipelineAppend},
    
        [Parameter(DontShow)]
        [ValidateNotNull()]
        [Microsoft.Azure.PowerShell.Cmdlets.Redis.Runtime.SendAsyncStep[]]
        # SendAsync Pipeline Steps to be prepended to the front of the pipeline
        ${HttpPipelinePrepend},
    
        [Parameter(DontShow)]
        [System.Uri]
        # The URI for the proxy server to use
        ${Proxy},
    
        [Parameter(DontShow)]
        [ValidateNotNull()]
        [System.Management.Automation.PSCredential]
        # Credentials for a proxy server to use for the remote call
        ${ProxyCredential},
    
        [Parameter(DontShow)]
        [System.Management.Automation.SwitchParameter]
        # Use the default credentials for the proxy
        ${ProxyUseDefaultCredentials}
    )
    
    begin {
        try {
            $outBuffer = $null
            if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) {
                $PSBoundParameters['OutBuffer'] = 1
            }
            $parameterSet = $PsCmdlet.ParameterSetName
            $mapping = @{
                Export = 'Az.Redis.private\Export-AzRedisRedisData_Export';
                ExportExpanded = 'Az.Redis.private\Export-AzRedisRedisData_ExportExpanded';
                ExportSubscriptionIdViaHost = 'Az.Redis.private\Export-AzRedisRedisData_ExportSubscriptionIdViaHost';
                ExportSubscriptionIdViaHostExpanded = 'Az.Redis.private\Export-AzRedisRedisData_ExportSubscriptionIdViaHostExpanded';
            }
            $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand(($mapping[$parameterSet]), [System.Management.Automation.CommandTypes]::Cmdlet)
            if ($wrappedCmd -eq $null) {
                $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand(($mapping[$parameterSet]), [System.Management.Automation.CommandTypes]::Function)
            }
            $scriptCmd = {& $wrappedCmd @PSBoundParameters}
            $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
            $steppablePipeline.Begin($PSCmdlet)
        } catch {
            throw
        }
    }
    
    process {
        try {
            $steppablePipeline.Process($_)
        } catch {
            throw
        }
    }
    
    end {
        try {
            $steppablePipeline.End()
        } catch {
            throw
        }
    }
    }
    
    */
}
exports.generateScriptCmdlets = generateScriptCmdlets;
//# sourceMappingURL=script-cmdlet.js.map