"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.match = exports.isEmptyParagraph = exports.generateHeaderElement = exports.generateEmptyElement = exports.generateElement = exports.generateDefaultText = exports.generateDefaultParagraph = exports.Placeholder = void 0;
var _slugid = _interopRequireDefault(require("slugid"));
var _reactI18next = require("react-i18next");
var _elementTypes = require("../../constants/element-types");
var _constants = require("../../../constants");
const match = (node, path, predicate) => {
  if (!predicate) return true;
  if (typeof predicate === 'object') {
    return Object.entries(predicate).every(_ref => {
      let [key, value] = _ref;
      if (value && !Array.isArray(value)) {
        return node[key] === value;
      }
      value = value ? value : [];
      return value.includes(node[key]);
    });
  }
  return predicate(node, path);
};
exports.match = match;
const generateDefaultText = () => {
  return {
    id: _slugid.default.nice(),
    text: ''
  };
};
exports.generateDefaultText = generateDefaultText;
const generateDefaultParagraph = () => {
  return {
    id: _slugid.default.nice(),
    type: _elementTypes.PARAGRAPH,
    children: [generateDefaultText()]
  };
};
exports.generateDefaultParagraph = generateDefaultParagraph;
const generateEmptyElement = type => {
  return {
    id: _slugid.default.nice(),
    type,
    children: [generateDefaultText()]
  };
};
exports.generateEmptyElement = generateEmptyElement;
const generateHeaderElement = text => {
  const headerText = {
    id: _slugid.default.nice(),
    text: text
  };
  return {
    id: _slugid.default.nice(),
    type: _elementTypes.HEADER1,
    children: [headerText]
  };
};

/**
 * @param {String} type
 * @param {Object} options
 * @param {Node[] | String} [options.childrenOrText = ''] If provide a string,that will generate a text node as children automatically
 * @param {object} [options.props = {}]
 * @returns {Node}
 */
exports.generateHeaderElement = generateHeaderElement;
const generateElement = function (type) {
  let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  let {
    childrenOrText = '',
    props = {}
  } = options;
  if (typeof childrenOrText === 'string') {
    childrenOrText = [{
      id: _slugid.default.nice(),
      text: childrenOrText
    }];
  }
  if (!Array.isArray(childrenOrText)) {
    throw Error('childrenOrText must be a string or a Node array!');
  }
  return {
    id: _slugid.default.nice(),
    type,
    children: childrenOrText,
    ...props
  };
};
exports.generateElement = generateElement;
const isEmptyParagraph = node => {
  if (node.type !== 'paragraph') return false;
  if (node.children.length !== 1) return false;
  const [child] = node.children;
  if (Text.isText(child) && child.text === '' && child.type !== 'image') return true;
  return false;
};
exports.isEmptyParagraph = isEmptyParagraph;
const Placeholder = props => {
  const {
    title
  } = props;
  const {
    t
  } = (0, _reactI18next.useTranslation)(_constants.TRANSLATE_NAMESPACE);
  return /*#__PURE__*/React.createElement("span", {
    style: {
      position: 'absolute',
      top: '0px',
      color: 'rgba(191,191,191,1)',
      pointerEvents: 'none',
      width: '100%',
      maxWidth: '100%',
      display: 'block',
      userSelect: 'none',
      textDecoration: 'none'
    }
  }, t(title));
};
exports.Placeholder = Placeholder;