"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.UnixTime = exports.DateTime1123 = exports.DateTime = void 0;
const codemodel_v3_1 = require("@azure-tools/codemodel-v3");
const codegen_csharp_1 = require("@azure-tools/codegen-csharp");
const codegen_csharp_2 = require("@azure-tools/codegen-csharp");
const codegen_csharp_3 = require("@azure-tools/codegen-csharp");
const clientruntime_1 = require("../clientruntime");
const primitive_1 = require("./primitive");
class DateTime extends primitive_1.NewPrimitive {
    get encode() {
        return (this.schema.extensions && this.schema.extensions['x-ms-skip-url-encoding']) ? '' : 'global::System.Uri.EscapeDataString';
    }
    get declaration() {
        return `global::System.DateTime${this.isRequired ? '' : '?'}`;
    }
    castJsonTypeToPrimitive(tmpValue, defaultValue) {
        return `global::System.DateTime.TryParse((string)${tmpValue}, global::System.Globalization.CultureInfo.InvariantCulture, global::System.Globalization.DateTimeStyles.AdjustToUniversal, out var ${tmpValue}Value) ? ${tmpValue}Value : ${defaultValue}`;
    }
    castXmlTypeToPrimitive(tmpValue, defaultValue) {
        return `global::System.DateTime.TryParse((string)${tmpValue}, global::System.Globalization.CultureInfo.InvariantCulture, global::System.Globalization.DateTimeStyles.AdjustToUniversal, out var ${tmpValue}Value) ? ${tmpValue}Value : ${defaultValue}`;
    }
    get convertObjectMethod() {
        return '(v) => v is global::System.DateTime _v ? _v : global::System.Xml.XmlConvert.ToDateTime( v.ToString() , global::System.Xml.XmlDateTimeSerializationMode.Unspecified)';
    }
    serializeToNode(mediaType, value, serializedName, mode) {
        switch (mediaType) {
            case codemodel_v3_1.KnownMediaType.Json:
                return this.isRequired ?
                    (0, codegen_csharp_2.toExpression)(`(${clientruntime_1.ClientRuntime.JsonNode}) new ${this.jsonType}(${value}.ToString(${this.DateTimeFormat},global::System.Globalization.CultureInfo.InvariantCulture))`) :
                    (0, codegen_csharp_2.toExpression)(`null != ${value} ? (${clientruntime_1.ClientRuntime.JsonNode}) new ${this.jsonType}(${value}?.ToString(${this.DateTimeFormat},global::System.Globalization.CultureInfo.InvariantCulture)) : null`);
            case codemodel_v3_1.KnownMediaType.Xml:
                return this.isRequired ?
                    (0, codegen_csharp_2.toExpression)(`new ${codegen_csharp_1.System.Xml.Linq.XElement}("${serializedName}",${value}.ToString(${this.DateTimeFormat},global::System.Globalization.CultureInfo.InvariantCulture))`) :
                    (0, codegen_csharp_2.toExpression)(`null != ${value} ? new ${codegen_csharp_1.System.Xml.Linq.XElement}("${serializedName}",${value}?.ToString(${this.DateTimeFormat},global::System.Globalization.CultureInfo.InvariantCulture)) : null`);
            case codemodel_v3_1.KnownMediaType.Cookie:
            case codemodel_v3_1.KnownMediaType.QueryParameter:
            case codemodel_v3_1.KnownMediaType.Header:
            case codemodel_v3_1.KnownMediaType.Text:
            case codemodel_v3_1.KnownMediaType.UriParameter: {
                const formatSerializedName = serializedName ? `${serializedName}=` : '';
                return (0, codegen_csharp_2.toExpression)(this.isRequired ?
                    `"${formatSerializedName}" + ${value}.ToString(${this.DateTimeFormat},global::System.Globalization.CultureInfo.InvariantCulture)` :
                    `(null == ${value} ? ${codegen_csharp_1.System.String.Empty} : "${formatSerializedName}" + ${value}?.ToString(${this.DateTimeFormat},global::System.Globalization.CultureInfo.InvariantCulture))`);
            }
        }
        return (0, codegen_csharp_2.toExpression)(`null /* serializeToNode doesn't support '${mediaType}' ${__filename}*/`);
    }
    serializeToContainerMember(mediaType, value, container, serializedName, mode) {
        const formatSerializedName = serializedName ? `${serializedName}=` : '';
        switch (mediaType) {
            case codemodel_v3_1.KnownMediaType.Json:
                // container : JsonObject
                return `AddIf( ${this.serializeToNode(mediaType, value, serializedName, mode)}, "${serializedName}" ,${(0, codegen_csharp_2.valueOf)(container)}.Add );`;
            case codemodel_v3_1.KnownMediaType.Xml:
                // container : XElement
                return `AddIf( ${this.serializeToNode(mediaType, value, serializedName, mode)}, ${(0, codegen_csharp_2.valueOf)(container)}.Add );`;
            case codemodel_v3_1.KnownMediaType.Header:
                // container : HttpRequestHeaders
                return this.isRequired ?
                    `${(0, codegen_csharp_2.valueOf)(container)}.Add("${serializedName}",${value}.ToString(${this.DateTimeFormat},global::System.Globalization.CultureInfo.InvariantCulture));` :
                    (0, codegen_csharp_3.If)(`null != ${value}`, `${(0, codegen_csharp_2.valueOf)(container)}.Add("${serializedName}",${value}?.ToString(${this.DateTimeFormat},global::System.Globalization.CultureInfo.InvariantCulture));`);
            case codemodel_v3_1.KnownMediaType.QueryParameter:
                // gives a name=value for use inside a c# template string($"foo{someProperty}") as a query parameter
                return this.isRequired ?
                    `${formatSerializedName}{${value}.ToString(${this.DateTimeFormat},global::System.Globalization.CultureInfo.InvariantCulture)}` :
                    `{null == ${value} ? ${codegen_csharp_1.System.String.Empty} : $"${formatSerializedName}{${value}?.ToString(${this.DateTimeFormat},global::System.Globalization.CultureInfo.InvariantCulture)}"}`;
            case codemodel_v3_1.KnownMediaType.UriParameter:
                // gives a name=value for use inside a c# template string($"foo{someProperty}") as a query parameter
                return this.isRequired ?
                    `${formatSerializedName}{${value}.ToString(${this.DateTimeFormat},global::System.Globalization.CultureInfo.InvariantCulture)}` :
                    `{null == ${value} ? ${codegen_csharp_1.System.String.Empty}: $"${formatSerializedName}{${value}?.ToString(${this.DateTimeFormat},global::System.Globalization.CultureInfo.InvariantCulture)}"}`;
        }
        return (`/* serializeToContainerMember doesn't support '${mediaType}' ${__filename}*/`);
    }
    constructor(schema, isRequired) {
        super(schema);
        this.isRequired = isRequired;
        this.isXmlAttribute = false;
        this.jsonType = clientruntime_1.ClientRuntime.JsonString;
        // public DateFormat = new StringExpression('yyyy-MM-dd');
        this.DateTimeFormat = new codegen_csharp_2.StringExpression('yyyy\'-\'MM\'-\'dd\'T\'HH\':\'mm\':\'ss.fffffffK');
    }
    // public static string DateFormat = "yyyy-MM-dd";
    // public static string DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK";
    // public static string DateTimeRfc1123Format = "R";
    // public static JsonString CreateDate(DateTime? value) => value is DateTime date ? new JsonString(date.ToString(DateFormat, CultureInfo.InvariantCulture)) : null;
    // public static JsonString CreateDateTime(DateTime? value) => value is DateTime date ? new JsonString(date.ToString(DateTimeFormat, CultureInfo.InvariantCulture)) : null;
    // public static JsonString CreateDateTimeRfc1123(DateTime ? value) => value is DateTime date ? new JsonString(date.ToString(DateTimeRfc1123Format, CultureInfo.InvariantCulture)) : null;
    validateValue(eventListener, property) {
        return '';
    }
}
exports.DateTime = DateTime;
class DateTime1123 extends DateTime {
    constructor(schema, isRequired) {
        super(schema, isRequired);
        this.DateTimeFormat = new codegen_csharp_2.StringExpression('R');
    }
}
exports.DateTime1123 = DateTime1123;
class UnixTime extends primitive_1.NewPrimitive {
    get encode() {
        return (this.schema.extensions && this.schema.extensions['x-ms-skip-url-encoding']) ? '' : 'global::System.Uri.EscapeDataString';
    }
    castJsonTypeToPrimitive(tmpValue, defaultValue) {
        return `long.TryParse((string)${tmpValue}, out var ${tmpValue}Value) ? ${this.EpochDate}.AddSeconds(${tmpValue}Value) : ${defaultValue}`;
    }
    castXmlTypeToPrimitive(tmpValue, defaultValue) {
        return `long.TryParse((string)${tmpValue}, out var ${tmpValue}Value) ? ${this.EpochDate}.AddSeconds(${tmpValue}Value) : ${defaultValue}`;
    }
    serializeToNode(mediaType, value, serializedName, mode) {
        const formatSerializedName = serializedName ? `${serializedName}=` : '';
        switch (mediaType) {
            case codemodel_v3_1.KnownMediaType.Json:
                return this.isRequired ?
                    this.jsonType.new(`((${this.longType})(${value}${this.q}.Subtract(${(0, codegen_csharp_2.valueOf)(this.EpochDate)}).TotalSeconds))`).Cast(clientruntime_1.ClientRuntime.JsonNode) :
                    (0, codegen_csharp_1.Ternery)((0, codegen_csharp_1.IsNotNull)(value), this.jsonType.new(`((${this.longType})(${value}${this.q}.Subtract(${(0, codegen_csharp_2.valueOf)(this.EpochDate)}).TotalSeconds)??0)`).Cast(clientruntime_1.ClientRuntime.JsonNode), codegen_csharp_1.dotnet.Null);
            case codemodel_v3_1.KnownMediaType.Xml:
                return this.isRequired ?
                    (0, codegen_csharp_2.toExpression)(`new ${codegen_csharp_1.System.Xml.Linq.XElement}("${serializedName}",${value})`) :
                    (0, codegen_csharp_2.toExpression)(`null != ${value} ? new ${codegen_csharp_1.System.Xml.Linq.XElement}("${serializedName}",${value}) : null`);
            case codemodel_v3_1.KnownMediaType.QueryParameter:
                if (this.isRequired) {
                    return (0, codegen_csharp_2.toExpression)(`"${formatSerializedName}" + ${this.encode}(${value}.ToString())`);
                }
                else {
                    return (0, codegen_csharp_2.toExpression)(`(null == ${value} ? ${codegen_csharp_1.System.String.Empty} : "${formatSerializedName}" + ${this.encode}(${value}.ToString()))`);
                }
            // return toExpression(`if (${value} != null) { queryParameters.Add($"${value}={${value}}"); }`);
            case codemodel_v3_1.KnownMediaType.Cookie:
            case codemodel_v3_1.KnownMediaType.Header:
            case codemodel_v3_1.KnownMediaType.Text:
            case codemodel_v3_1.KnownMediaType.UriParameter:
                return (0, codegen_csharp_2.toExpression)(this.isRequired ?
                    `(${value}.ToString())` :
                    `(null == ${value} ? ${codegen_csharp_1.System.String.Empty} : ${value}.ToString())`);
        }
        return (0, codegen_csharp_2.toExpression)(`null /* serializeToNode doesn't support '${mediaType}' ${__filename}*/`);
    }
    /** emits an expression serialize this to the value required by the container */
    _serializeToNode(mediaType, value, serializedName, mode) {
        return super.serializeToNode(mediaType, new codegen_csharp_2.LiteralExpression(`((${this.longType})(${value}${this.q}.Subtract(${(0, codegen_csharp_2.valueOf)(this.EpochDate)}).TotalSeconds))`), serializedName, mode);
    }
    get q() {
        return this.isRequired ? '' : '?';
    }
    get longType() {
        return this.isRequired ? 'long' : 'long?';
    }
    constructor(schema, isRequired) {
        super(schema);
        this.isRequired = isRequired;
        this.isXmlAttribute = false;
        this.jsonType = clientruntime_1.ClientRuntime.JsonNumber;
        this.EpochDate = codegen_csharp_1.System.DateTime.new('1970', '1', '1', '0', '0', '0', codegen_csharp_1.System.DateTimeKind.Utc);
    }
    validateValue(eventListener, property) {
        return '';
    }
    get declaration() {
        return `global::System.DateTime${this.isRequired ? '' : '?'}`;
    }
}
exports.UnixTime = UnixTime;
//# sourceMappingURL=date-time.js.map