var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
export class Util {
    // todo: implement for complex objects
    static arrayRemoveValue(arr, value) {
        return arr.filter(function (ele) {
            return ele != value;
        });
        // ex:   var result = arrayRemove(array, 'myRemoveString');
    }
    static arraySetUnique(arr) {
        let obj = {};
        for (let a = 0; a < arr.length; a++)
            obj[arr[a]] = true;
        let resultarr = [];
        for (let o in obj)
            resultarr.push(o);
        return resultarr;
    }
    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;
    }
    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 fetchJson(url) {
        return __awaiter(this, void 0, void 0, function* () {
            const response = yield fetch(url).catch((error) => {
                console.error(error);
            });
            const json = yield response.json();
            return json;
        });
    }
    // static fromJson(str) {
    //     let obj = null;
    //     if (typeof str == "string") {
    //         try {
    //             obj = JSON.parse(str);
    //         } catch (e) {
    //             throw new Error("Invalid JSON string provided. ");
    //         }
    //     }
    //     return obj;
    // }
    /**
     * Returns the global object of the environment the application is running in (browser, node, web worker, or frame)
     */
    static getGlobal() {
        // the only reliable means to get the global object is
        // `Function('return this')()`
        // However, this causes CSP violations in Chrome apps.
        if (typeof self !== 'undefined') {
            return self;
        }
        if (typeof window !== 'undefined') {
            return window;
        }
        if (typeof global !== 'undefined') {
            return global;
        }
        throw new Error('unable to locate global object');
    }
    ;
    /**
     * 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 isBrowser() {
        return typeof window !== 'undefined';
    }
    /**
     * Checks if the application is running in a browser and online.
     */
    static isOnline() {
        if (typeof window !== 'undefined' && window.navigator.onLine) {
            return true;
        }
        else {
            return false;
        }
    }
    static isMergebleObject(item) {
        return Util.isObject(item) && !Array.isArray(item);
    }
    static isObject(item) {
        return item !== null && typeof item === 'object';
    }
}
//# sourceMappingURL=util.js.map