"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.identity = exports.nameof = exports.isReflectMetadataSupported = exports.isInstanceOf = exports.isValueDefined = exports.logWarning = exports.logMessage = exports.logError = exports.isSubtypeOf = exports.parseToJSObject = exports.shouldOmitParseString = exports.isObject = exports.isTypeTypedArray = exports.isDirectlyDeserializableNativeType = exports.isDirectlySerializableNativeType = exports.MISSING_REFLECT_CONF_MSG = exports.LAZY_TYPE_EXPLANATION = void 0;
var type_descriptor_1 = require("./type-descriptor");
exports.LAZY_TYPE_EXPLANATION = "If the type is not yet defined, for example due to circular references, add '() => ' before it. E.g. @jsonMember(() => Foo)";
exports.MISSING_REFLECT_CONF_MSG = 'Make sure that you have both "experimentalDecorators"'
    + ' and "emitDecoratorMetadata" enabled in your tsconfig.json';
function isDirectlySerializableNativeType(type) {
    return [Date, Number, String, Boolean].indexOf(type) !== -1;
}
exports.isDirectlySerializableNativeType = isDirectlySerializableNativeType;
function isDirectlyDeserializableNativeType(type) {
    return [Number, String, Boolean].indexOf(type) !== -1;
}
exports.isDirectlyDeserializableNativeType = isDirectlyDeserializableNativeType;
function isTypeTypedArray(type) {
    return [
        Float32Array,
        Float64Array,
        Int8Array,
        Uint8Array,
        Uint8ClampedArray,
        Int16Array,
        Uint16Array,
        Int32Array,
        Uint32Array,
    ].indexOf(type) !== -1;
}
exports.isTypeTypedArray = isTypeTypedArray;
function isObject(value) {
    return typeof value === 'object';
}
exports.isObject = isObject;
function shouldOmitParseString(jsonStr, expectedType) {
    var expectsTypesSerializedAsStrings = expectedType === String
        || expectedType === ArrayBuffer
        || expectedType === DataView;
    var hasQuotes = jsonStr.length >= 2
        && jsonStr[0] === '"'
        && jsonStr[jsonStr.length - 1] === '"';
    if (expectedType === Date) {
        var isNumber = !isNaN(Number(jsonStr.trim()));
        return !hasQuotes && !isNumber;
    }
    return expectsTypesSerializedAsStrings && !hasQuotes;
}
exports.shouldOmitParseString = shouldOmitParseString;
function parseToJSObject(json, expectedType) {
    if (typeof json !== 'string' || shouldOmitParseString(json, expectedType)) {
        return json;
    }
    return JSON.parse(json);
}
exports.parseToJSObject = parseToJSObject;
function isSubtypeOf(A, B) {
    return A === B || A.prototype instanceof B;
}
exports.isSubtypeOf = isSubtypeOf;
function logError(message) {
    var optionalParams = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        optionalParams[_i - 1] = arguments[_i];
    }
    if (typeof console === 'object' && typeof console.error === 'function') {
        console.error.apply(console, __spreadArrays([message], optionalParams));
    }
    else if (typeof console === 'object' && typeof console.log === 'function') {
        console.log.apply(console, __spreadArrays(["ERROR: " + message], optionalParams));
    }
}
exports.logError = logError;
function logMessage(message) {
    var optionalParams = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        optionalParams[_i - 1] = arguments[_i];
    }
    if (typeof console === 'object' && typeof console.log === 'function') {
        console.log.apply(console, __spreadArrays([message], optionalParams));
    }
}
exports.logMessage = logMessage;
function logWarning(message) {
    var optionalParams = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        optionalParams[_i - 1] = arguments[_i];
    }
    if (typeof console === 'object' && typeof console.warn === 'function') {
        console.warn.apply(console, __spreadArrays([message], optionalParams));
    }
    else if (typeof console === 'object' && typeof console.log === 'function') {
        console.log.apply(console, __spreadArrays(["WARNING: " + message], optionalParams));
    }
}
exports.logWarning = logWarning;
function isValueDefined(value) {
    return !(typeof value === 'undefined' || value === null);
}
exports.isValueDefined = isValueDefined;
function isInstanceOf(value, constructor) {
    if (constructor === type_descriptor_1.AnyT.ctor) {
        return true;
    }
    else if (typeof value === 'number') {
        return constructor === Number;
    }
    else if (typeof value === 'string') {
        return constructor === String;
    }
    else if (typeof value === 'boolean') {
        return constructor === Boolean;
    }
    else if (isObject(value)) {
        return value instanceof constructor;
    }
    return false;
}
exports.isInstanceOf = isInstanceOf;
exports.isReflectMetadataSupported = typeof Reflect === 'object' && typeof Reflect.getMetadata === 'function';
function nameof(fn) {
    if (typeof fn.name === 'string') {
        return fn.name;
    }
    return 'undefined';
}
exports.nameof = nameof;
function identity(arg) {
    return arg;
}
exports.identity = identity;
//# sourceMappingURL=helpers.js.map