"use strict";

exports.__esModule = true;
exports.messageContainsValues = exports.default = void 0;
var _reactRedux = require("react-redux");
var _reactIntl = require("react-intl");
var _utils = require("../utils");
var _safeGet = _interopRequireDefault(require("../utils/safeGet"));
var _stripKey = _interopRequireDefault(require("../utils/stripKey"));
var _react = _interopRequireDefault(require("react"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
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), !0).forEach(function (r) { _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; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var defaultBuildMessage = function defaultBuildMessage(message) {
  return /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, message);
};
var messageContainsValues = exports.messageContainsValues = function messageContainsValues(message) {
  var valRefs = message.defaultMessage.match(/(\{\w+\})/g);
  if (!valRefs) return true;
  var valNames = valRefs.map(function (ref) {
    return ref.replace(/[{}]/g, "");
  });
  return !!message.values && valNames.every(function (key) {
    return message.values[key] !== undefined && message.values[key] !== null;
  });
};
var useLabelMessage = function useLabelMessage(label, buildMessage) {
  if (buildMessage === void 0) {
    buildMessage = defaultBuildMessage;
  }
  var messageResult = null;
  var missingValues = false;
  var valueSelector = function valueSelector() {};
  var message = typeof label === "object" ? _objectSpread({}, label) : label || "";
  if (typeof (0, _safeGet.default)(message, "values") === "function") {
    valueSelector = message.values;
    message = (0, _stripKey.default)("values", message);
  }
  var selectValues = (0, _utils.unwrapImmutable)((0, _reactRedux.useSelector)(valueSelector));
  if (message && (message.values || selectValues)) {
    message.values = _objectSpread(_objectSpread({}, message.values), selectValues);
  }
  if (message.id) {
    if (message.values || selectValues) {
      message.values = _objectSpread(_objectSpread({}, message.values), selectValues);
    }
    if (messageContainsValues(message)) {
      messageResult = buildMessage(message);
    } else {
      missingValues = true;
      messageResult = null;
    }
  } else {
    messageResult = message.toString();
  }
  return [messageResult, missingValues];
};
__signature__(useLabelMessage, "useSelector{}", function () {
  return [_reactRedux.useSelector];
});
__signature__(useLabelMessage, "useSelector{}", function () {
  return [_reactRedux.useSelector];
});
var _default = useLabelMessage;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(defaultBuildMessage, "defaultBuildMessage", "/home/vsts/work/1/s/src/hooks/useLabelMessage.js");
  reactHotLoader.register(messageContainsValues, "messageContainsValues", "/home/vsts/work/1/s/src/hooks/useLabelMessage.js");
  reactHotLoader.register(useLabelMessage, "useLabelMessage", "/home/vsts/work/1/s/src/hooks/useLabelMessage.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/hooks/useLabelMessage.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(defaultBuildMessage, "defaultBuildMessage", "/home/vsts/work/1/s/src/hooks/useLabelMessage.js");
  reactHotLoader.register(messageContainsValues, "messageContainsValues", "/home/vsts/work/1/s/src/hooks/useLabelMessage.js");
  reactHotLoader.register(useLabelMessage, "useLabelMessage", "/home/vsts/work/1/s/src/hooks/useLabelMessage.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/hooks/useLabelMessage.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();