"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
exports.formatReactTemplate = formatReactTemplate;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireWildcard(require("react"));

var _isString = _interopRequireDefault(require("lodash/isString"));

var _flatMap = _interopRequireDefault(require("lodash/flatMap"));

var _formatTemplate2 = _interopRequireDefault(require("./formatTemplate"));

function formatReactTemplate(template, map) {
  var result = [template];
  var keys = Object.keys(map);
  var length = keys.length;
  keys.forEach(function (key, index) {
    var node = map[key];
    result = (0, _flatMap["default"])(result, function (text) {
      if ((0, _isString["default"])(text)) {
        var stringText = text;

        if ( /*#__PURE__*/(0, _react.isValidElement)(node)) {
          var placeholder = "{".concat(key, "}");
          var _length = placeholder.length;
          var textArr = [];

          var _index = stringText.indexOf(placeholder);

          while (_index > -1) {
            if (_index > 0) {
              textArr.push(stringText.slice(0, _index));
            }

            textArr.push(node);
            stringText = stringText.slice(_index + _length);
            _index = stringText.indexOf(placeholder);
          }

          if (stringText) {
            textArr.push(stringText);
          }

          return textArr;
        }

        return (0, _formatTemplate2["default"])(text, (0, _defineProperty2["default"])({}, key, node), index < length - 1);
      }

      return text;
    });
  });

  if (result.every(_isString["default"])) {
    return result.join('');
  }

  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, result);
}

var _default = formatReactTemplate;
exports["default"] = _default;
//# sourceMappingURL=formatReactTemplate.js.map
