"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CmdletNamespace = void 0;
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
const linq_1 = require("@azure-tools/linq");
const codegen_csharp_1 = require("@azure-tools/codegen-csharp");
const exports_1 = require("../llcsharp/exports");
const class_1 = require("./class");
class CmdletNamespace extends codegen_csharp_1.Namespace {
    get outputFolder() {
        return this.state.project.cmdletFolder;
    }
    constructor(parent, state, objectInitializer) {
        super('Cmdlets', parent);
        this.state = state;
        this.inputModels = new Array();
        this.apply(objectInitializer);
    }
    async init() {
        var _a;
        this.add(new codegen_csharp_1.ImportDirective(`static ${exports_1.ClientRuntime.Extensions}`));
        this.add(new codegen_csharp_1.ImportDirective(`${exports_1.ClientRuntime.PowerShell}`));
        this.add(new codegen_csharp_1.ImportDirective(`${exports_1.ClientRuntime.Cmdlets}`));
        this.add(new codegen_csharp_1.ImportDirective('System'));
        // generate cmdlet classes on top of the SDK
        for (const { key: index, value: operation } of (0, linq_1.items)(this.state.model.commands.operations)) {
            // skip ViaIdentity for set-* cmdlets.
            if (this.state.project.azure && operation.details.csharp.verb === 'Set' && operation.details.csharp.name.indexOf('ViaIdentity') > 0) {
                continue;
            }
            if (operation.variant.includes('ViaJsonString') || operation.variant.includes('ViaJsonFilePath')) {
                if (operation.details.default.virtualParameters) {
                    operation.details.default.virtualParameters.body = [];
                }
                const callGraph = operation.callGraph[operation.callGraph.length - 1];
                if (callGraph.requests && callGraph.requests.length > 0) {
                    callGraph.requests[0].parameters = (_a = callGraph.requests[0].parameters) === null || _a === void 0 ? void 0 : _a.filter(element => { var _a; return ((_a = element.protocol.http) === null || _a === void 0 ? void 0 : _a.in) !== 'body'; });
                }
                if (operation.details.csharp.virtualParameters) {
                    operation.details.csharp.virtualParameters.body = [];
                }
                operation.parameters = operation.parameters.filter(element => element.required === true);
            }
            const newClass = await new class_1.CmdletClass(this, operation, this.state.path('commands', 'operations', index)).init();
            const refCopyPropertyNames = ['parameters', 'requests', 'responses', 'exceptions', 'requestMediaTypes'];
            if (operation.variant.includes('ViaJsonString')) {
                const name = 'JsonString';
                operation.details.csharp.name = `${operation.variant}Via${name}`;
                operation.callGraph[operation.callGraph.length - 1] = (0, linq_1.clone)(operation.callGraph[operation.callGraph.length - 1], false, undefined, undefined, refCopyPropertyNames);
                operation.callGraph[operation.callGraph.length - 1].language.csharp.name = `${operation.callGraph[operation.callGraph.length - 1].language.csharp.name}ViaJsonString`;
            }
            if (operation.variant.includes('ViaJsonFilePath')) {
                const name = 'JsonFilePath';
                operation.details.csharp.name = `${operation.variant}Via${name}`;
                const jsonFilePath = newClass.properties.filter(p => p.name === 'JsonFilePath');
                if (jsonFilePath.length > 0) {
                    jsonFilePath[0].set = 'if (!System.IO.File.Exists(value)) { throw new Exception("Cannot find File " + value); } this._jsonString = System.IO.File.ReadAllText(value); this._jsonFilePath = value;';
                }
                const jsonStringField = new codegen_csharp_1.Field('_jsonString', codegen_csharp_1.System.String);
                newClass.add(jsonStringField);
                operation.callGraph[operation.callGraph.length - 1] = (0, linq_1.clone)(operation.callGraph[operation.callGraph.length - 1], false, undefined, undefined, refCopyPropertyNames);
                operation.callGraph[operation.callGraph.length - 1].language.csharp.name = `${operation.callGraph[operation.callGraph.length - 1].language.csharp.name}ViaJsonString`;
            }
            this.addClass(newClass);
        }
        return this;
    }
}
exports.CmdletNamespace = CmdletNamespace;
//# sourceMappingURL=namespace.js.map