"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.Password = void 0;
const string_1 = require("./string");
const codemodel_v3_1 = require("@azure-tools/codemodel-v3");
const codegen_1 = require("@azure-tools/codegen");
const codegen_csharp_1 = require("@azure-tools/codegen-csharp");
const clientruntime_1 = require("../clientruntime");
const primitive_1 = require("./primitive");
// Currently we assume password could only appear in the request body in json format
class Password extends string_1.String {
    constructor(schema, isRequired) {
        super(schema, isRequired);
    }
    get convertObjectMethod() {
        return '(object ss) => (System.Security.SecureString)ss';
    }
    get declaration() {
        return 'System.Security.SecureString';
    }
    deserializeFromContainerMember(mediaType, container, serializedName, defaultValue) {
        switch (mediaType) {
            case codemodel_v3_1.KnownMediaType.Json: {
                // container should be a JsonObject
                const tmpVar = `__${(0, codegen_1.camelCase)(['json', ...(0, codegen_1.deconstruct)(serializedName)])}`;
                return (0, codegen_csharp_1.toExpression)(`If( ${(0, codegen_csharp_1.valueOf)(container)}?.PropertyT<${clientruntime_1.ClientRuntime.JsonString}>("${serializedName}"), out var ${tmpVar}) ? new System.Net.NetworkCredential("",(string)${tmpVar}).SecurePassword : ${defaultValue}`);
            }
        }
        return (0, codegen_csharp_1.toExpression)(`${defaultValue} /* deserializeFromContainerMember doesn't support '${mediaType}' ${__filename}*/`);
    }
    deserializeFromNode(mediaType, node, defaultValue) {
        try {
            const tmp = (0, primitive_1.pushTempVar)();
            switch (mediaType) {
                case codemodel_v3_1.KnownMediaType.Json:
                    // node should be a JsonString
                    return (0, codegen_csharp_1.toExpression)(`${node} is ${clientruntime_1.ClientRuntime.JsonString} ${tmp} ? (${this.declaration})(${tmp}.ToString()) : ${defaultValue}`);
            }
        }
        finally {
            (0, primitive_1.popTempVar)();
        }
        return (0, codegen_csharp_1.toExpression)(`null /* deserializeFromContainer doesn't support '${mediaType}' ${__filename}*/`);
    }
    /** emits an expression serialize this to a HttpContent */
    serializeToContent(mediaType, value, mode) {
        return (0, codegen_csharp_1.toExpression)(`null /* serializeToContent doesn't support '${mediaType}' ${__filename}*/`);
    }
    serializeToNode(mediaType, value, serializedName, mode) {
        switch (mediaType) {
            case codemodel_v3_1.KnownMediaType.Json:
                return (0, codegen_csharp_1.toExpression)(`null != (((object)${value})?.ToString()) ? (${clientruntime_1.ClientRuntime.JsonNode}) new ${clientruntime_1.ClientRuntime.JsonString}(System.Runtime.InteropServices.Marshal.PtrToStringBSTR(System.Runtime.InteropServices.Marshal.SecureStringToBSTR(${value}))) : null`);
        }
        return (0, codegen_csharp_1.toExpression)(`null /* serializeToNode doesn't support '${mediaType}' ${__filename}*/`);
    }
    /** emits an expression to deserialize content from a string */
    deserializeFromString(mediaType, content, defaultValue) {
        switch (mediaType) {
            case codemodel_v3_1.KnownMediaType.Json: {
                return this.deserializeFromNode(mediaType, clientruntime_1.ClientRuntime.JsonNode.Parse(content), defaultValue);
            }
        }
        return (0, codegen_csharp_1.toExpression)(`null /* deserializeFromString doesn't support '${mediaType}' ${__filename}`);
    }
    /** emits an expression to deserialize content from a content/response */
    deserializeFromResponse(mediaType, content, defaultValue) {
        switch (mediaType) {
            case codemodel_v3_1.KnownMediaType.Json:
                return (0, codegen_csharp_1.toExpression)(`${content}.Content.ReadAsStringAsync().ContinueWith( body => ${this.deserializeFromString(mediaType, 'body.Result', defaultValue)})`);
        }
        return (0, codegen_csharp_1.toExpression)(`null /* deserializeFromResponse doesn't support '${mediaType}' ${__filename}*/`);
    }
    serializeToContainerMember(mediaType, value, container, serializedName, mode) {
        switch (mediaType) {
            case codemodel_v3_1.KnownMediaType.Json:
                return `AddIf( ${this.serializeToNode(mediaType, value, serializedName, mode)}, "${serializedName}" ,${container}.Add );`;
        }
        return (`/* serializeToContainerMember doesn't support '${mediaType}' ${__filename}*/`);
    }
    // Override the validateValue function for the parent and do not validate password 
    validateValue(eventListener, property) {
        return '';
    }
}
exports.Password = Password;
//# sourceMappingURL=password.js.map