var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
    return new Promise(function (resolve, reject) {
        generator = generator.call(thisArg, _arguments);
        function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
        function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
        function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
        function step(verb, value) {
            var result = generator[verb](value);
            result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
        }
        step("next", void 0);
    });
};
export class Util {
    static copy(obj) {
        return JSON.parse(JSON.stringify(obj));
    }
    static deepAssign(target, ...sources) {
        if (!sources.length) {
            return target;
        }
        const 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(target, ...sources);
    }
    ;
    static isObject(item) {
        return item !== null && typeof item === 'object';
    }
    static isMergebleObject(item) {
        return Util.isObject(item) && !Array.isArray(item);
    }
    static castToType(o) {
        let 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
     */
    static getTimeStamp(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
     */
    static getUUID() {
        let d = new Date().getTime();
        const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            const 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
     */
    static isOnline() {
        if (typeof window !== 'undefined' && window.navigator.onLine) {
            return true;
        }
        else {
            return false;
        }
    }
    /**
     * Returns the size of the viewport.
     * @method Util.viewportSize
     * @returns
     */
    static viewportSize() {
        let 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
     */
    static isBrowser() {
        return typeof window !== 'undefined';
    }
    static getStyle(el, styleProp) {
        let value;
        const 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) {
                    const 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;
        }
    }
}
//# sourceMappingURL=util.js.map