"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.ByteArray = void 0;
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 codegen_csharp_2 = require("@azure-tools/codegen-csharp");
const primitive_1 = require("./primitive");
const clientruntime_1 = require("../clientruntime");
class ByteArray {
    get declaration() {
        return 'byte[]';
    }
    get convertObjectMethod() {
        return 'i => i';
    }
    get defaultOfType() {
        return (0, codegen_csharp_1.toExpression)('null /* byte array */');
    }
    /** emits an expression to deserialize a property from a member inside a container */
    deserializeFromContainerMember(mediaType, container, serializedName, defaultValue) {
        try {
            const tmp = (0, primitive_1.pushTempVar)();
            switch (mediaType) {
                case codemodel_v3_1.KnownMediaType.Xml: {
                    return (0, codegen_csharp_1.toExpression)(`If( ${(0, codegen_csharp_1.valueOf)(container)}?.Element("${serializedName}")?.Value, out var ${tmp}) ? System.Convert.FromBase64String(${tmp}.Replace("_","/").Replace("-","+").PadRight(  ${tmp}.Length  + ${tmp}.Length * 3 % 4, '=')) : ${defaultValue}`);
                }
                case codemodel_v3_1.KnownMediaType.Json: {
                    return (0, codegen_csharp_1.toExpression)(`If( ${(0, codegen_csharp_1.valueOf)(container)}?.PropertyT<${clientruntime_1.ClientRuntime.JsonString}>("${serializedName}"), out var ${tmp}) ?  System.Convert.FromBase64String( ((string)${tmp}).Replace("_","/").Replace("-","+").PadRight(  ((string)${tmp}).Length  + ((string)${tmp}).Length * 3 % 4, '=') ) : null`);
                }
                case codemodel_v3_1.KnownMediaType.Header: {
                    //const tmp = `__${camelCase(['header', ...deconstruct(serializedName)])}`;
                    return (0, codegen_csharp_1.toExpression)(`System.Linq.Enumerable.FirstOrDefault(${serializedName}) is string ${tmp} ? System.Convert.FromBase64String(${tmp}) : ${defaultValue}`);
                }
            }
        }
        finally {
            (0, primitive_1.popTempVar)();
        }
        return (0, codegen_csharp_1.toExpression)(`null /* deserializeFromContainerMember doesn't support '${mediaType}' ${__filename}*/`);
    }
    /** emits an expression to deserialze a container as the value itself. */
    deserializeFromNode(mediaType, node, defaultValue) {
        return (0, codegen_csharp_1.toExpression)(`null /* deserializeFromNode doesn't support '${mediaType}' ${__filename}*/`);
    }
    /** emits an expression serialize this to the value required by the container */
    serializeToNode(mediaType, value, serializedName, mode) {
        switch (mediaType) {
            case codemodel_v3_1.KnownMediaType.Json:
                return (0, codegen_csharp_1.toExpression)(`${clientruntime_1.ClientRuntime.JsonString.new(`System.Convert.ToBase64String(${value})`)}`);
        }
        return (0, codegen_csharp_1.toExpression)(`null /* serializeToNode doesn't support '${mediaType}' ${__filename}*/`);
    }
    /** emits an expression serialize this to the value required by the container */
    serializeToContent(mediaType, value, mode) {
        return (0, codegen_csharp_1.toExpression)(`null /* serializeToContent doesn't support '${mediaType}' ${__filename}*/`);
    }
    /** emits an expression to deserialize content from a string */
    deserializeFromString(mediaType, 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) {
        return (0, codegen_csharp_1.toExpression)(`null /* deserializeFromResponse doesn't support '${mediaType}' ${__filename}*/`);
    }
    /** emits the code required to serialize this into a container */
    serializeToContainerMember(mediaType, value, container, serializedName, mode) {
        try {
            const b = (0, primitive_1.pushTempVar)();
            switch (mediaType) {
                case codemodel_v3_1.KnownMediaType.Xml: {
                    return `AddIf( null != ${value} ? ${codegen_csharp_1.System.Xml.Linq.XElement.new(serializedName, codegen_csharp_1.System.Convert.ToBase64String(value))}  : null, ${container}.Add);`;
                }
                case codemodel_v3_1.KnownMediaType.Json: {
                    if (this.schema.format == 'base64url') {
                        return `AddIf( null != ${value} ? ${codegen_csharp_1.System.Convert.ToBase64String(value)}.TrimEnd(new char[] {'='}).Replace('+', '-').Replace('/', '_') : null ,(v)=> ${container}.Add( "${serializedName}",v) );`;
                    }
                    else {
                        return `AddIf( null != ${value} ? ${codegen_csharp_1.System.Convert.ToBase64String(value)} : null ,(v)=> ${container}.Add( "${serializedName}",v) );`;
                    }
                }
                case codemodel_v3_1.KnownMediaType.Header: {
                    if (this.schema.format == 'base64url') {
                        return (0, codegen_csharp_2.If)(`null != ${value}`, `${(0, codegen_csharp_1.valueOf)(container)}.Add("${serializedName}", ${codegen_csharp_1.System.Convert.ToBase64String(value)}.TrimEnd(new char[] {'='}).Replace('+', '-').Replace('/', '_'));`);
                    }
                    else {
                        return (0, codegen_csharp_2.If)(`null != ${value}`, `${(0, codegen_csharp_1.valueOf)(container)}.Add("${serializedName}", ${codegen_csharp_1.System.Convert.ToBase64String(value)});`);
                    }
                }
            }
        }
        finally {
            (0, primitive_1.popTempVar)();
        }
        return `/* serializeToContainerMember doesn't support '${mediaType}' ${__filename}*/`;
    }
    constructor(schema, isRequired) {
        this.schema = schema;
        this.isRequired = isRequired;
        this.isXmlAttribute = false;
        this.isNullable = true;
    }
    validateValue(eventListener, property) {
        return '';
    }
    validatePresence(eventListener, property) {
        if (this.isRequired) {
            return `await ${eventListener}.AssertNotNull(${(0, codegen_1.nameof)(property.value)},${property});`.trim();
        }
        return '';
    }
}
exports.ByteArray = ByteArray;
//# sourceMappingURL=byte-array.js.map