"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Util = /** @class */ (function () {
    function Util() {
    }
    Util.copy = function (obj) {
        return JSON.parse(JSON.stringify(obj));
    };
    Util.deepAssign = function (target) {
        var sources = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            sources[_i - 1] = arguments[_i];
        }
        if (!sources.length) {
            return target;
        }
        var source = sources.shift();
        if (source === undefined) {
            return target;
        }
        if (Util.isMergebleObject(target) && Util.isMergebleObject(source)) {
            Object.keys(source).forEach(function (key) {
                if (Util.isMergebleObject(source[key])) {
                    if (!target[key]) {
                        target[key] = {};
                    }
                    Util.deepAssign(target[key], source[key]);
                }
                else {
                    target[key] = source[key];
                }
            });
        }
        return Util.deepAssign.apply(Util, [target].concat(sources));
    };
    ;
    Util.isObject = function (item) {
        return item !== null && typeof item === 'object';
    };
    Util.isMergebleObject = function (item) {
        return Util.isObject(item) && !Array.isArray(item);
    };
    Util.castToType = function (o) {
        var value = o;
        if (Number(value) && value !== "true" && value !== "false") {
            value = Number(value);
        }
        if (value === "0") {
            value = 0;
        }
        if (value === "true") {
            value = true;
        }
        if (value === "false") {
            value = false;
        }
        return value;
    };
    ;
    /**
     * Returns the current date as seconds since midnight Jan 1, 1970.
     * @method Util.getTimeStamp
     * @param date optional: A string or Date object to return as seconds since midnight Jan 1, 1970.
     * @returns
     */
    Util.getTimeStamp = function (date) {
        if (date === void 0) { date = null; }
        if (date) {
            if (typeof date === 'string') {
                return Math.round(+new Date(date) / 1000);
            }
            else {
                return Math.round(+date / 1000);
            }
        }
        else {
            return Math.round(+new Date() / 1000);
        }
    };
    /**
     * Returns a UUID.
     * @method Util.getUUID
     * @returns
     */
    Util.getUUID = function () {
        var d = new Date().getTime();
        var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c === 'x' ? r : (r & 0x7 | 0x8)).toString(16);
        });
        return uuid;
    };
    /**
     * Checks if the application is running in a browser.
     * @method Util.isOnline
     * @returns
     */
    Util.isOnline = function () {
        if (typeof window !== 'undefined' && window.navigator.onLine) {
            return true;
        }
        else {
            return false;
        }
    };
    /**
     * Returns the size of the viewport.
     * @method Util.viewportSize
     * @returns
     */
    Util.viewportSize = function () {
        var obj = {};
        if (typeof window !== 'undefined') {
            if (window.innerHeight) {
                obj.width = window.innerWidth;
                obj.height = window.innerHeight;
            }
            else {
                obj.width = document.documentElement.clientWidth;
                obj.height = document.documentElement.clientHeight;
            }
        }
        // todo: native + nodejs
        if (typeof window === 'undefined') { }
        return obj;
    };
    /**
     * Checks if the application is running in a browser.
     * @method Util.isBrowser
     * @returns
     */
    Util.isBrowser = function () {
        return typeof window !== 'undefined';
    };
    Util.getStyle = function (el, styleProp) {
        var value;
        var defaultView = el.ownerDocument.defaultView;
        // W3C standard way:
        if (defaultView && defaultView.getComputedStyle) {
            // sanitize property name to css notation (hypen separated words eg. font-Size)
            styleProp = styleProp.replace(/([A-Z])/g, '-$1').toLowerCase();
            return defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
        }
        else if (el.currentStyle) {
            // sanitize property name to camelCase
            styleProp = styleProp.replace(/\-(\w)/g, function (str, letter) {
                return letter.toUpperCase();
            });
            value = el.currentStyle[styleProp];
            // convert other units to pixels on IE
            if (/^\d+(em|pt|%|ex)?$/i.test(value)) {
                return (function (value) {
                    var oldLeft = el.style.left, oldRsLeft = el.runtimeStyle.left;
                    el.runtimeStyle.left = el.currentStyle.left;
                    el.style.left = value || 0;
                    value = el.style.pixelLeft + 'px';
                    el.style.left = oldLeft;
                    el.runtimeStyle.left = oldRsLeft;
                    return value;
                })(value);
            }
            return value;
        }
    };
    return Util;
}());
exports.Util = Util;
//# sourceMappingURL=util.js.map