"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _HighlightedItem = _interopRequireDefault(require("./HighlightedItem"));
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/Highlighter/Highlighter.jsx",
  _this = void 0;
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
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) { (0, _defineProperty2["default"])(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; }
/**
 * Highlighter
 *
 * It highlights string type children by wrapping match
 * with strong dom element.
 * It remains children element structure.
 */

var childKeyGenerator = function childKeyGenerator() {
  var childKey = 0;
  return function () {
    return "highlighted-child-".concat(childKey++);
  };
};
var ELEM_TYPES = {
  STRING: 'string',
  ARRAY: 'array',
  REACT_ELEMENT: 'React_element'
};
var getElementType = function getElementType(element) {
  if (Array.isArray(element)) {
    return ELEM_TYPES.ARRAY;
  }
  if (/*#__PURE__*/_react["default"].isValidElement(element)) {
    return ELEM_TYPES.REACT_ELEMENT;
  }
  if (typeof element === 'string') {
    return ELEM_TYPES.STRING;
  }
  return '';
};
var _highlight = function highlight(element, match, nextChildKey, props) {
  if (!element) {
    return null;
  }
  var elementType = getElementType(element);
  var elementTypesMap = (0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])({}, ELEM_TYPES.STRING, function (elem, _match) {
    return /*#__PURE__*/_react["default"].createElement(_HighlightedItem["default"], {
      key: nextChildKey(),
      match: _match,
      emphasize: props.emphasize,
      __self: _this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 43,
        columnNumber: 7
      }
    }, elem);
  }), ELEM_TYPES.REACT_ELEMENT, function (elem) {
    if (elem.props.children) {
      return /*#__PURE__*/_react["default"].cloneElement(elem, _objectSpread(_objectSpread({}, elem.props), {}, {
        key: nextChildKey()
      }), _highlight(elem.props.children, match, nextChildKey, props));
    }
    return elem;
  }), ELEM_TYPES.ARRAY, function (elem) {
    return elem.map(function (el) {
      return _highlight(el, match, nextChildKey, props);
    });
  });
  return elementTypesMap[elementType] ? elementTypesMap[elementType](element, match) : element;
};
var Highlighter = /*#__PURE__*/function (_React$PureComponent) {
  function Highlighter(props) {
    var _this2;
    (0, _classCallCheck2["default"])(this, Highlighter);
    _this2 = _callSuper(this, Highlighter, [props]);
    // we want to create new react keys generator for instance of highlighter
    _this2.nextChildKey = childKeyGenerator();
    return _this2;
  }
  (0, _inherits2["default"])(Highlighter, _React$PureComponent);
  return (0, _createClass2["default"])(Highlighter, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
        dataHook = _this$props.dataHook,
        children = _this$props.children,
        match = _this$props.match;
      return /*#__PURE__*/_react["default"].createElement("span", {
        "data-hook": dataHook,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 80,
          columnNumber: 7
        }
      }, _highlight(children, match, this.nextChildKey, this.props));
    }
  }]);
}(_react["default"].PureComponent);
Highlighter.displayName = 'Highlighter';
var _default = exports["default"] = Highlighter;