function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { Component } from "react";
import { Linking } from "react-native";
import { connect } from "react-redux";
import ParsedText from "react-native-parsed-text";
import { Text } from "./Text";
import { getMainBrandingColourFromState, TEXT_DARKEST } from "../colours";
import { getValidUrl } from "../helper";
import { analyticsActions } from "../apis";
class FormattedText extends Component {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "onPressUrl", url => {
      const urlToUse = getValidUrl(url);
      Linking.openURL(urlToUse);
      this.checkLog("UrlClick", urlToUse);
    });
    _defineProperty(this, "onPressUrlText", urlText => {
      const texts = urlText.match(FormattedText.UrlTextRegex);
      this.onPressUrl(texts[1]);
    });
    _defineProperty(this, "onPressEmail", email => {
      Linking.openURL(`mailto:${email}`);
      this.checkLog("EmailClick", email);
    });
    _defineProperty(this, "onPressPhone", phone => {
      const parsedPhone = phone.replace(/\D/g, "");
      Linking.openURL(`tel:${parsedPhone.replace(/\D/g, "")}`);
      this.checkLog("PhoneClick", parsedPhone);
    });
    _defineProperty(this, "ifEnabled", value => this.props.disabled ? undefined : value);
  }
  static get UrlTextRegex() {
    return /\[([^\]\|\]]+)\|([^\]\|\]]+)\]/i;
  }
  checkLog(type, target) {
    if (this.props.logClicks) {
      analyticsActions.log(this.props.site, type, this.props.entityType, this.props.entityId, {
        target
      });
    }
  }
  renderTextContent(matchingString, matches) {
    return matches[1];
  }
  renderUrlText(matchingString, matches) {
    return matches[2];
  }
  render() {
    const {
      style,
      numberOfLines,
      titleSize,
      colourBrandingMain,
      subtitleSize,
      children
    } = this.props;
    return /*#__PURE__*/React.createElement(ParsedText, {
      maxFontSizeMultiplier: 1.2,
      style: [styles.text, this.ifEnabled({
        lineHeight: 24
      }), style],
      numberOfLines: numberOfLines,
      parse: [{
        pattern: /\*\*(.*?)\*\*/i,
        style: this.ifEnabled({
          fontFamily: "sf-bold",
          lineHeight: 24
        }),
        renderText: this.renderTextContent
      }, {
        pattern: /\*t\*(.*?)\*t\*/i,
        style: this.ifEnabled({
          fontSize: titleSize || 22,
          color: colourBrandingMain,
          fontFamily: "sf-semibold",
          lineHeight: 24
        }),
        renderText: this.renderTextContent
      }, {
        pattern: /\*s\*(.*?)\*s\*/i,
        style: this.ifEnabled({
          fontSize: subtitleSize || 18,
          color: TEXT_DARKEST,
          fontFamily: "sf-semibold",
          lineHeight: 24
        }),
        renderText: this.renderTextContent
      }, {
        pattern: FormattedText.UrlTextRegex,
        style: this.ifEnabled({
          color: colourBrandingMain
        }),
        onPress: this.ifEnabled(this.onPressUrlText),
        renderText: this.renderUrlText
      }, {
        pattern: /(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/im,
        style: this.ifEnabled({
          color: colourBrandingMain
        }),
        onPress: this.ifEnabled(this.onPressUrl)
      }, {
        pattern: /([A-Z0-9._%-]+)@([A-Z0-9.-]+\.[A-Z]{2,4})/im,
        style: this.ifEnabled({
          color: colourBrandingMain
        }),
        onPress: this.ifEnabled(this.onPressEmail)
      }, {
        pattern: /(?:\+?(\d{1,3}))?([-. (]*(\d{1,3})[-. )]*)?((\d{2,4})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$/im,
        style: this.ifEnabled({
          color: colourBrandingMain
        }),
        onPress: this.ifEnabled(this.onPressPhone)
      }]
    }, children);
  }
}
const styles = {
  text: {
    color: TEXT_DARKEST,
    fontSize: 14,
    fontFamily: "sf-regular"
  }
};
const mapStateToProps = state => {
  return {
    colourBrandingMain: getMainBrandingColourFromState(state)
  };
};
export default connect(mapStateToProps, {})(FormattedText);
//# sourceMappingURL=FormattedText.js.map