class Utils {

    static updateQueryStringParameter(uri, key, value) {
        let re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
        let separator = uri.indexOf("?") !== -1 ? "&" : "?";
        if (uri.match(re)) {
            return uri.replace(re, "$1" + key + "=" + value + "$2");
        } else {
            return uri + separator + key + "=" + value;
        }
    }

    static getQueryVarFromAnchor(name) {
        const hash = window.location.hash.split('?')[1]; // Get the part after '?'
        if (!hash) return null;
    
        const params = new URLSearchParams(hash);
        return params.get(name);
    }

    static isEqual(obj1, obj2) {
        var props1 = Object.getOwnPropertyNames(obj1);
        var props2 = Object.getOwnPropertyNames(obj2);
        if (props1.length != props2.length) {
            return false;
        }
        for (var i = 0; i < props1.length; i++) {
            let val1 = obj1[props1[i]];
            let val2 = obj2[props1[i]];
            let isObjects = Utils.isObject(val1) && Utils.isObject(val2);
            if (isObjects && !Utils.isEqual(val1, val2) || !isObjects && val1 !== val2) {
                return false;
            }
        }
        return true;
    }

    static isObject(object) {
      return object != null && typeof object === 'object';
    }

    static isObjectEmpty(object) {
        if (!Utils.isObject(object)) {
            return false;
        }
        return Object.keys(object).length === 0;
    }

    static isFunction(fct) {
        return fct instanceof Function;
    }

    static isValidNumber (num) {
        return typeof num === 'number' && !isNaN(num);
    }

    static isString(val) {
        return typeof val === 'string';
    }

    static debounce(func, timeout = 300) {
        let timer;
        return (...args) => {
          clearTimeout(timer);
          timer = setTimeout(() => { func.apply(this, args); }, timeout);
        };
    }

    static isNull(val) {
        return val == null;
    }

    static guid() {
        let s4 = () => {
            return Math.floor((1 + Math.random()) * 0x10000)
                .toString(16)
                .substring(1);
        }
        return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
    }

    static uuidv4() {
        return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
          (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
        );
    }

    static decodeUriText(value) {
        let decoded = decodeURIComponent(value);
        return decoded.replace(/\+/g, " ");
    }

    static bodyRemoveBackground() {
        jQuery('body').removeClass(function(index, className) {
            return (className.match(/\bclm-bg\S*/g) || []).join(' ');
        });
    }
}
export default Utils;