"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.replaceUrlCurlySegments = replaceUrlCurlySegments;
var _has = _interopRequireDefault(require("lodash/has"));
function hasKey(item, maybeKeyOfT) {
  return (0, _has.default)(item, maybeKeyOfT);
}
const getValueAt = (item, dataPropertyMap) => maybeCurlyProperty => {
  var _exec;
  const maybeCurlySegment = (_exec = /^{(.*)}$/.exec(maybeCurlyProperty)) == null ? void 0 : _exec[1];
  if (maybeCurlySegment) {
    var _dataPropertyMap$mayb;
    const maybeKeyOfT = dataPropertyMap == null ? maybeCurlySegment : (_dataPropertyMap$mayb = dataPropertyMap[maybeCurlySegment]) == null ? void 0 : _dataPropertyMap$mayb.find(prop => hasKey(item, prop));
    if (maybeKeyOfT != null && hasKey(item, maybeKeyOfT)) {
      return String(item[maybeKeyOfT]);
    }
  }
  return maybeCurlyProperty;
};
function replaceUrlCurlySegments(templateUrl, item, dataPropertyMap) {
  const url = new URL(templateUrl.href);
  url.pathname = templateUrl.pathname.split('/').map(decodeURIComponent).map(getValueAt(item, dataPropertyMap)).map(encodeURIComponent).join('/');
  const getItemValueAt = getValueAt(item, dataPropertyMap);
  for (const [key, value] of Array.from(templateUrl.searchParams.entries())) {
    url.searchParams.set(key, getItemValueAt(value));
  }
  return url;
}
//# sourceMappingURL=replaceUrlCurlySegments.js.map