import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { isValidElement } from 'react';
import isString from 'lodash/isString';
import flatMap from 'lodash/flatMap';
import formatTemplate from './formatTemplate';
export 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 = flatMap(result, function (text) {
      if (isString(text)) {
        var stringText = text;

        if ( /*#__PURE__*/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 formatTemplate(text, _defineProperty({}, key, node), index < length - 1);
      }

      return text;
    });
  });

  if (result.every(isString)) {
    return result.join('');
  }

  return /*#__PURE__*/React.createElement(React.Fragment, null, result);
}
export default formatReactTemplate;
//# sourceMappingURL=formatReactTemplate.js.map
