//import * as abpTypings from '../lib/abp';
class Utils {
  constructor() {
    this.sample = (array) => array[Math.floor(Math.random() * array.length)];
    this.keyBy = (array, keyMap, valMap) => array.reduce((obj, item) => {
      obj[keyMap(item)] = valMap ? valMap(item) : item;
      return obj;
    }, {});
    this.debounce = (fn, wait) => {
      let scheduled = null;
      return (...args) => {
        clearTimeout(scheduled);
        scheduled = setTimeout(fn, wait, ...args);
      };
    };
  }
  loadScript(url) {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    document.body.appendChild(script);
  }
  loadScriptAsync(src) {
    return new Promise(function (resolve, reject) {
      const s = document.createElement('script');
      let r = false;
      s.type = 'text/javascript';
      s.src = src;
      s.async = true;
      s.onerror = function (err) {
        reject(err);
      };
      s.onload = s.onreadystatechange = function () {
        // console.log(this.readyState); // uncomment this line to see which ready states are called.
        if (!r && (!this.readyState || this.readyState == 'complete')) {
          r = true;
          resolve(true);
        }
      };
      const t = document.getElementsByTagName('script')[0];
      t.parentElement.insertBefore(s, t);
    });
  }
  extend(...args) {
    let options, name, src, srcType, copy, copyIsArray, clone, target = args[0] || {}, i = 1, length = args.length, deep = false;
    if (typeof target === 'boolean') {
      deep = target;
      target = args[i] || {};
      i++;
    }
    if (typeof target !== 'object' && typeof target !== 'function') {
      target = {};
    }
    if (i === length) {
      target = this;
      i--;
    }
    for (; i < length; i++) {
      if ((options = args[i]) !== null) {
        for (name in options) {
          src = target[name];
          copy = options[name];
          if (target === copy) {
            continue;
          }
          srcType = Array.isArray(src) ? 'array' : typeof src;
          if (deep && copy && ((copyIsArray = Array.isArray(copy)) || typeof copy === 'object')) {
            if (copyIsArray) {
              copyIsArray = false;
              clone = src && srcType === 'array' ? src : [];
            }
            else {
              clone = src && srcType === 'object' ? src : {};
            }
            target[name] = this.extend(deep, clone, copy);
          }
          else if (copy !== undefined) {
            target[name] = copy;
          }
        }
      }
    }
    return target;
  }
  setLocalization() {
    if (!sparkle.utils.getCookieValue('Sparkle.Localization.CultureName')) {
      let language = navigator.language;
      sparkle.utils.setCookieValue('Sparkle.Localization.CultureName', language, new Date(new Date().getTime() + 5 * 365 * 86400000), sparkle.appPath);
    }
  }
}
export default new Utils();
