"use strict";

exports.__esModule = true;
exports.default = exports.messageContainsValues = 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(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

(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(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; }

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 = 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;
  });
};

exports.messageContainsValues = messageContainsValues;

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 = _default2;
exports.default = _default3;
;

(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);
})();