import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["h1", "h2", "h3", "h4", "h5", "h6", "p", "b", "small", "i", "span", "del", "em", "blockquote", "noWrap", "children", "className"];
/* "use client" */;
import React, { useMemo } from 'react';
import TextChild from './child';
import { withScale } from '../use-scale';
var _getModifierChild = function getModifierChild(tags, children) {
  if (!tags.length) return children;
  var nextTag = tags.slice(1, tags.length);
  return /*#__PURE__*/React.createElement(TextChild, {
    tag: tags[0]
  }, _getModifierChild(nextTag, children));
};
var TextComponent = function TextComponent(_ref) {
  var _ref$h = _ref.h1,
    h1 = _ref$h === void 0 ? false : _ref$h,
    _ref$h2 = _ref.h2,
    h2 = _ref$h2 === void 0 ? false : _ref$h2,
    _ref$h3 = _ref.h3,
    h3 = _ref$h3 === void 0 ? false : _ref$h3,
    _ref$h4 = _ref.h4,
    h4 = _ref$h4 === void 0 ? false : _ref$h4,
    _ref$h5 = _ref.h5,
    h5 = _ref$h5 === void 0 ? false : _ref$h5,
    _ref$h6 = _ref.h6,
    h6 = _ref$h6 === void 0 ? false : _ref$h6,
    _ref$p = _ref.p,
    p = _ref$p === void 0 ? false : _ref$p,
    _ref$b = _ref.b,
    b = _ref$b === void 0 ? false : _ref$b,
    _ref$small = _ref.small,
    small = _ref$small === void 0 ? false : _ref$small,
    _ref$i = _ref.i,
    i = _ref$i === void 0 ? false : _ref$i,
    _ref$span = _ref.span,
    span = _ref$span === void 0 ? false : _ref$span,
    _ref$del = _ref.del,
    del = _ref$del === void 0 ? false : _ref$del,
    _ref$em = _ref.em,
    em = _ref$em === void 0 ? false : _ref$em,
    _ref$blockquote = _ref.blockquote,
    blockquote = _ref$blockquote === void 0 ? false : _ref$blockquote,
    _ref$noWrap = _ref.noWrap,
    noWrap = _ref$noWrap === void 0 ? false : _ref$noWrap,
    children = _ref.children,
    _ref$className = _ref.className,
    className = _ref$className === void 0 ? '' : _ref$className,
    props = _objectWithoutProperties(_ref, _excluded);
  var elements = {
    h1: h1,
    h2: h2,
    h3: h3,
    h4: h4,
    h5: h5,
    h6: h6,
    p: p,
    blockquote: blockquote
  };
  var inlineElements = {
    span: span,
    small: small,
    b: b,
    em: em,
    i: i,
    del: del
  };
  var names = Object.keys(elements).filter(function (name) {
    return elements[name];
  });
  var inlineNames = Object.keys(inlineElements).filter(function (name) {
    return inlineElements[name];
  });

  /**
   *  Render element "p" only if no element is found.
   *  If there is only one modifier, just rendered one modifier element
   *  e.g.
   *    <Text /> => <p />
   *    <Text em /> => <em />
   *    <Text p em /> => <p><em>children</em></p>
   *
   */

  var tag = useMemo(function () {
    if (names[0]) return names[0];
    if (inlineNames[0]) return inlineNames[0];
    return 'p';
  }, [names, inlineNames]);
  var renderableChildElements = inlineNames.filter(function (name) {
    return name !== tag;
  });
  var modifers = useMemo(function () {
    if (!renderableChildElements.length) return children;
    return _getModifierChild(renderableChildElements, children);
  }, [renderableChildElements, children]);
  return /*#__PURE__*/React.createElement(TextChild, _extends({
    style: {
      textAlign: props.align
    },
    className: "".concat(className, " ").concat(noWrap ? 'no-wrap' : ''),
    tag: tag
  }, props), modifers);
};
TextComponent.displayName = 'Text';
var Text = withScale(TextComponent);
export default Text;