'use strict';

var objectWithoutProperties = require('../objectWithoutProperties-wSsbHFcG.js');
var slicedToArray = require('../slicedToArray-BcL7fKuL.js');
var asyncToGenerator = require('../asyncToGenerator-8vhqAwJl.js');
var defineProperty = require('../defineProperty-lChFfVqi.js');
var i18next = require('i18next');
var neetoCist = require('@bigbinary/neeto-cist');
var Toastr = require('@bigbinary/neetoui/Toastr');
var qs = require('qs');
var ramda = require('ramda');
require('../unsupportedIterableToArray-BoHMiKNA.js');
require('../typeof-D4ZZ_FlZ.js');

var _this = undefined;
var _excluded = ["toCamelCase"];
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = false, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = true, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), true), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { defineProperty._defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var toCamelCasedString = function toCamelCasedString(string) {
  return string.replace(/[_-]+(\w)/g, function (_, nextChar) {
    return nextChar.toUpperCase();
  });
};
var withEventTargetValue = /*#__PURE__*/ramda.curry(function (func, event) {
  return func(event.target.value);
});
var getSubdomain = function getSubdomain() {
  var host = window.location.host;
  var parts = host.split(".");
  return parts.length >= 3 ? parts[0] : "";
};
var simulateApiCall = function simulateApiCall(result, error) {
  var errorProbability = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0.1;
  var delay = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : neetoCist.getRandomInt(350, 1000);
  return new Promise(function (resolve, reject) {
    return setTimeout(function () {
      var defaultErrorObj = {
        success: false,
        notice: i18next.t("neetoCommons.notice.errorOccurred")
      };
      Math.random() < errorProbability ? reject(_objectSpread(_objectSpread({}, defaultErrorObj), error)) : resolve(result);
    }, delay);
  });
};
var copyToClipboard = /*#__PURE__*/function () {
  var _ref = asyncToGenerator._asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(text) {
    var _ref2,
      _ref2$showToastr,
      showToastr,
      _ref2$message,
      message,
      textArea,
      isCopied,
      _args = arguments,
      _t2;
    return _regenerator().w(function (_context) {
      while (1) switch (_context.p = _context.n) {
        case 0:
          _ref2 = _args.length > 1 && _args[1] !== undefined ? _args[1] : {}, _ref2$showToastr = _ref2.showToastr, showToastr = _ref2$showToastr === void 0 ? true : _ref2$showToastr, _ref2$message = _ref2.message, message = _ref2$message === void 0 ? i18next.t("neetoCommons.toastr.success.copiedToClipboard") : _ref2$message;
          _context.p = 1;
          _context.n = 2;
          return navigator.clipboard.writeText(text);
        case 2:
          showToastr && Toastr.success(message);
          _context.n = 7;
          break;
        case 3:
          _context.p = 3;
          _context.v;
          _context.p = 4;
          textArea = document.createElement("textarea");
          textArea.value = text;
          textArea.style.top = "0";
          textArea.style.left = "0";
          textArea.style.position = "fixed";
          document.body.appendChild(textArea);
          textArea.focus();
          textArea.select();
          isCopied = document.execCommand("copy");
          document.body.removeChild(textArea);
          if (isCopied) {
            _context.n = 5;
            break;
          }
          throw new Error(i18next.t("neetoCommons.copyToClipboard.failed"));
        case 5:
          showToastr && Toastr.success(message);
          _context.n = 7;
          break;
        case 6:
          _context.p = 6;
          _t2 = _context.v;
          Toastr.error(_t2);
        case 7:
          return _context.a(2);
      }
    }, _callee, null, [[4, 6], [1, 3]]);
  }));
  return function copyToClipboard(_x) {
    return _ref.apply(this, arguments);
  };
}();
var buildUrl = function buildUrl(route, params) {
  var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  var placeHolders = [];
  var _options$toSnakeCase = options.toSnakeCase,
    toSnakeCase = _options$toSnakeCase === void 0 ? true : _options$toSnakeCase;
  ramda.toPairs(params).forEach(function (_ref3) {
    var _ref4 = slicedToArray._slicedToArray(_ref3, 2),
      key = _ref4[0],
      value = _ref4[1];
    if (!route.includes(":".concat(key))) return;
    placeHolders.push(key);
    route = route.replace(":".concat(key), encodeURIComponent(value));
  });
  var keyTransformer = toSnakeCase ? neetoCist.camelToSnakeCase : ramda.identity;
  var transformObjectKeys = function transformObjectKeys(params) {
    return neetoCist.transformObjectDeep(params, function (key, value) {
      return [keyTransformer(key), value];
    }, function (object) {
      return typeof (object === null || object === void 0 ? void 0 : object.toJSON) === "function" ? object.toJSON() : object;
    });
  };
  var queryParams = ramda.pipe(ramda.omit(placeHolders), transformObjectKeys, qs.stringify)(params);
  return ramda.isEmpty(queryParams) ? route : "".concat(route, "?").concat(queryParams);
};
var toLocale = function toLocale(number) {
  var _window$globalProps;
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
  return Number(number).toLocaleString(((_window$globalProps = window.globalProps) === null || _window$globalProps === void 0 || (_window$globalProps = _window$globalProps.user) === null || _window$globalProps === void 0 ? void 0 : _window$globalProps.locale) || navigator.language || navigator.languages[0], options);
};
var parseQueryParams = function parseQueryParams(search) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var _options$toCamelCase = options.toCamelCase,
    toCamelCase = _options$toCamelCase === void 0 ? true : _options$toCamelCase,
    qsOptions = objectWithoutProperties._objectWithoutProperties(options, _excluded);
  var params = qs.parse(search, _objectSpread({
    ignoreQueryPrefix: true
  }, qsOptions));
  if (toCamelCase) {
    var pairedParams = ramda.toPairs(params);
    params = {};
    pairedParams.forEach(function (_ref5) {
      var _ref6 = slicedToArray._slicedToArray(_ref5, 2),
        key = _ref6[0],
        value = _ref6[1];
      var camelCasedKey = toCamelCasedString(key);
      params[camelCasedKey] = value;
    });
  }
  return params;
};
var getQueryParams = function getQueryParams(options) {
  return parseQueryParams(location.search, options);
};
var joinHyphenCase = function joinHyphenCase() {
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
    args[_key] = arguments[_key];
  }
  return args.join(" ").replace(/\s+/g, "-").toLowerCase();
};
var hyphenize = function hyphenize(value) {
  var fallbackString = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
  if (typeof value === "number") return String(value);
  if (value && typeof value === "string" && value.replace) {
    return value.replace(/[\s_]/g, "-").replace(/([a-z])([A-Z])/g, "$1-$2").replace(/-+/g, "-").toLowerCase();
  }
  return fallbackString;
};
var debounce = function debounce(func) {
  var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 350;
  var timer;
  return function () {
    for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      args[_key2] = arguments[_key2];
    }
    clearTimeout(timer);
    timer = setTimeout(function () {
      return func.apply(_this, args);
    }, delay);
  };
};
var getFromLocalStorage = function getFromLocalStorage(key) {
  try {
    // eslint-disable-next-line @bigbinary/neeto/no-local-storage
    return JSON.parse(localStorage.getItem(key));
  } catch (_unused2) {
    return null;
  }
};
var setToLocalStorage = function setToLocalStorage(key, value) {
  try {
    // eslint-disable-next-line @bigbinary/neeto/no-local-storage
    localStorage.setItem(key, JSON.stringify(value));
  } catch (_unused3) {
    // localStorage access can fail due to private browsing mode or storage restrictions
  }
};
var removeFromLocalStorage = function removeFromLocalStorage(key) {
  try {
    // eslint-disable-next-line @bigbinary/neeto/no-local-storage
    localStorage.removeItem(key);
  } catch (_unused4) {
    // localStorage access can fail due to private browsing mode or storage restrictions
  }
};
var showThumbsUpToastr = function showThumbsUpToastr() {
  return (
    // eslint-disable-next-line @bigbinary/neeto/use-show-thumbs-up-toastr
    Toastr.success("", {
      icon: "👍",
      className: "w-20"
    })
  );
};

exports.buildUrl = buildUrl;
exports.copyToClipboard = copyToClipboard;
exports.debounce = debounce;
exports.getFromLocalStorage = getFromLocalStorage;
exports.getQueryParams = getQueryParams;
exports.getSubdomain = getSubdomain;
exports.hyphenize = hyphenize;
exports.joinHyphenCase = joinHyphenCase;
exports.parseQueryParams = parseQueryParams;
exports.removeFromLocalStorage = removeFromLocalStorage;
exports.setToLocalStorage = setToLocalStorage;
exports.showThumbsUpToastr = showThumbsUpToastr;
exports.simulateApiCall = simulateApiCall;
exports.toLocale = toLocale;
exports.withEventTargetValue = withEventTargetValue;
//# sourceMappingURL=general.js.map
