Liferay.Loader.define("commerce-organization-web@4.0.23/js/utils/utils", ['module', 'exports', 'require', 'frontend-js-web', './window'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.endpointBuilder = endpointBuilder;
    exports.callApi = callApi;
    exports.truncateTextNode = truncateTextNode;
    exports.bindAll = bindAll;
    exports.getColorHue = getColorHue;
    exports.setupDataset = setupDataset;
    exports.getLocalizedText = getLocalizedText;
    exports.isArray = exports.isNumber = exports.noop = void 0;

    var _frontendJsWeb = require("frontend-js-web");

    var _window = _interopRequireDefault(require("./window"));

    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
    }

    function ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {
          return Object.getOwnPropertyDescriptor(object, sym).enumerable;
        });keys.push.apply(keys, symbols);
      }return keys;
    }

    function _objectSpread(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          ownKeys(Object(source)).forEach(function (key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
          });
        }
      }return target;
    }

    function _defineProperty(obj, key, value) {
      if (key in obj) {
        Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
      } else {
        obj[key] = value;
      }return obj;
    }

    function serializeParams(params) {
      return Object.keys(params).map(function (key) {
        return "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(params[key]));
      }).join('&');
    }

    function endpointBuilder(_ref) {
      var baseURL = _ref.baseURL,
          _ref$id = _ref.id,
          id = _ref$id === void 0 ? '0' : _ref$id,
          _ref$path = _ref.path,
          path = _ref$path === void 0 ? '' : _ref$path,
          _ref$queryParams = _ref.queryParams,
          queryParams = _ref$queryParams === void 0 ? {} : _ref$queryParams;

      if (!baseURL) {
        throw new Error('No API baseURL provided.');
      }

      var collection = path ? "/".concat(path) : '';
      var organizationId = "/".concat(id);
      var parameters = "?".concat(serializeParams(queryParams));
      var root = "".concat(baseURL, "/organizations");
      return "".concat(root).concat(organizationId).concat(collection).concat(parameters);
    }

    function callApi(parameters) {
      return (0, _frontendJsWeb.fetch)(endpointBuilder(parameters)).then(function (response) {
        return response.json();
      });
    }

    var noop = function noop() {};

    exports.noop = noop;

    var isNumber = function isNumber(value) {
      return typeof value === 'number';
    };

    exports.isNumber = isNumber;

    var isArray = function isArray(value) {
      return Array.isArray(value);
    };

    exports.isArray = isArray;

    function truncateTextNode() {
      var text = this.innerHTML;
      var textLength = this.getComputedTextLength();

      while (textLength > 220) {
        text = text.slice(0, -3);
        this.innerHTML = "".concat(text, "...");
        textLength = this.getComputedTextLength();
      }
    }

    function bindAll() {
      var _arguments = arguments;
      var clazz = arguments[0];
      Object.keys(arguments).forEach(function (argNo, i) {
        var method = _arguments[argNo];

        if (i !== 0 && typeof clazz[method] === 'function') {
          clazz[method] = clazz[method].bind(clazz);
        }
      });
      return clazz;
    }

    function isInRange(value, lower, upper) {
      return value >= lower && value < upper;
    }

    function getColorHue(prevHue) {
      var hue = Math.random() * 360;
      return !prevHue ? hue : isInRange(hue, prevHue - 2, prevHue + 3) ? hue + 15 > 360 ? hue - 15 : hue + 15 : hue;
    }

    function setupDataset(data) {
      var sanitizedData = _objectSpread({}, data);

      if (sanitizedData.organizations.length) {
        sanitizedData.organizations.forEach(function (orgObject, index) {
          delete orgObject.organizations;
          var prevColor = index ? sanitizedData.organizations[index - 1] : null;
          orgObject.colorIdentifier = "hsl(".concat(getColorHue(prevColor), ",75%,75%)");
        });
      }

      return sanitizedData;
    }

    function getLocalizedText(string) {
      try {
        return _window.default.Liferay.Language.get(string);
      } catch (error) {
        return string;
      }
    }
    //# sourceMappingURL=utils.js.map
  }
});
//# sourceMappingURL=utils.js.map