"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.LinkPreview = void 0;
var React = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _styles = require("./styles");
var _utils = require("./utils");
var _RenderTextWithMention = _interopRequireDefault(require("../RenderTextWithMention/RenderTextWithMention"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const LinkPreview = exports.LinkPreview = /*#__PURE__*/React.memo(({
  text,
  mentionPositionArr
}) => {
  const [data, setData] = React.useState(null);
  const styles = (0, _styles.useStyles)();
  React.useEffect(() => {
    let isCancelled = false;
    const fetchData = async () => {
      setData(undefined);
      const newData = await (0, _utils.getPreviewData)(text);
      if (!isCancelled) {
        setData(newData);
      }
    };
    fetchData();
    return () => {
      isCancelled = true;
    };
  }, [text]);
  const handlePress = () => (data === null || data === void 0 ? void 0 : data.link) && _reactNative.Linking.openURL(data.link);
  const renderImageNode = React.useCallback(image => {
    const imageUrl = image ? {
      uri: image
    } : require('../../../core/assets/images/previewLinkDefaultBackground.png');
    return /*#__PURE__*/React.createElement(_reactNative.Image, {
      accessibilityRole: "image",
      resizeMode: "cover",
      source: imageUrl,
      style: styles.image
    });
  }, [styles.image]);
  const renderTitleNode = title => {
    return /*#__PURE__*/React.createElement(_reactNative.Text, {
      numberOfLines: 1,
      style: styles.title
    }, title);
  };
  const renderShortUrl = url => {
    const matches = url.match(/^https?:\/\/([^/?#]+)(?:[/?#]|$)/i);
    const shortUrl = matches ? matches[1] : '';
    return /*#__PURE__*/React.createElement(_reactNative.Text, {
      numberOfLines: 1,
      style: styles.shortUrl
    }, shortUrl);
  };
  return /*#__PURE__*/React.createElement(_reactNative.View, null, /*#__PURE__*/React.createElement(_RenderTextWithMention.default, {
    textPost: text,
    mentionPositionArr: mentionPositionArr
  }), (data === null || data === void 0 ? void 0 : data.link) && /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    onPress: handlePress,
    style: styles.metadataContainer
  }, renderImageNode(data.image), /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.metadataTextContainer
  }, (data === null || data === void 0 ? void 0 : data.link) && renderShortUrl(data.link), (data === null || data === void 0 ? void 0 : data.title) && renderTitleNode(data.title))));
});
//# sourceMappingURL=LinkPreview.js.map