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.
     */
    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.
     */
    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.
     */
    static isOnline() {
        if (typeof window !== 'undefined' && window.navigator.onLine) {
            return true;
        }
        else {
            return false;
        }
    }
    // /**
    //  * Returns the size of the viewport.
    //  */
    // static viewportSize() {
    //     let obj: any = {};
    //     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.
     */
    static isBrowser() {
        return typeof window !== 'undefined';
    }
    static copyToClipboard(str) {
        const el = document.createElement('textarea'); // Create a <textarea> element
        el.value = str; // Set its value to the string that you want copied
        el.setAttribute('readonly', ''); // Make it readonly to be tamper-proof
        el.style.position = 'absolute';
        el.style.left = '-9999px'; // Move outside the screen to make it invisible
        document.body.appendChild(el); // Append the <textarea> element to the HTML document
        const selected = document.getSelection().rangeCount > 0 // Check if there is any content selected previously
            ? document.getSelection().getRangeAt(0) // Store selection if found
            : false; // Mark as false to know no selection existed before
        el.select(); // Select the <textarea> content
        document.execCommand('copy'); // Copy - only works as a result of a user action (e.g. click events)
        document.body.removeChild(el); // Remove the <textarea> element
        if (selected) { // If a selection existed before copying
            document.getSelection().removeAllRanges(); // Unselect everything on the HTML document
            document.getSelection().addRange(selected); // Restore the original selection
        }
    }
}
//# sourceMappingURL=util.js.map