define(['app'], function(app) {
    var parseLinkStringToAddAnchor = function() {
      return function(value) {
        return parseLinkStringToAddAnchorFormat(value)
      }
    }
    app.filter('parseLinkStringToAddAnchorFormatFormatter', parseLinkStringToAddAnchor)
  
    function parseLinkStringToAddAnchorFormat(value) {
      if(!value) return value;

      /**
       *
       * Goal: We need to wrap an anchor around plain valid url.
       *
       * Every url that is not wrapped into an anchor tag already will be wrapped.
       * This updated regex now properly:
       *
       * Matches URLs starting with http://, https://, or www..
       * Avoids URLs that are already within Markdown-style links or HTML anchor tags.
       * Captures the entire URL in a single group.
       *
       * Examples:
       * Input: This is a link: https://example.com
       *
       * Match: https://example.com
       * Input: Check this [link](https://example.com)
       *
       * No Match: Properly avoids URLs already within Markdown-style links.
       * Input: <a href="https://example.com">Link</a>
       *
       * No Match: Properly avoids URLs inside HTML anchor tags.
       * Input: Visit www.google.com for more info.
       *
       * Match: www.google.com
       * @type {RegExp}
       */
      // const emailPattern = /(?<!<a\s+href=")(?<!\[)(?<!\()((https?:\/\/|www\.)[^\s/$.?#].[^\s]*)(?![^<]*<\/a>)(?![\])])/g;
      // (?<!\]\()https?:\/\/www..*?\.[a-zA-Z]{1,6}
      const plainUrlPattern = /(?<!\]\()https?:\/\/[^\s\/$.?#].[^\s]*/g
      value = value.replace(plainUrlPattern, (url) => {
        if(url){
          url = prependHttpProtocol(url)
          return wrapWithMarkDown(url,url);
        } else{
          return url
        }
      });

      const plainUrlPatternStartsWithWww = /[^\/\/\(\(]www.[^\s\/$.?#].[^\s]*/g
      value = value.replace(plainUrlPatternStartsWithWww, (url) => {
        if(url){
          url = url.replace(/\n/g, '')
          url = prependHttpProtocol(url)
          return wrapWithMarkDown(url,url);
        } else{
          return url
        }
      });

      const markdownUrlStartsWithWww = /[\(]www.[^\s\/$.?#].[^\s]*[\)]/g
      value = value.replace(markdownUrlStartsWithWww, (url) => {
        if (url) {
          return prependHttpProtocol(url)
        } else {
          return url
        }
      })

      return value
    }

    /**
     * Add protocol to links without https
     * @param url
     * @returns {string}
     */
    function prependHttpProtocol(url){
      // If the match starts with 'www.', prepend 'https://'
      if (url.startsWith('www.')) {
        url = 'https://' + url;
      } else if (url.startsWith('(www.')) {
        url = url.replace('www.', 'https://www.')
      }
      return url
    }

    /**
     * Wrap url into an anchor tag, to be opened on another tab
     * @param url
     * @param text
     * @returns {string}
     */
    function wrapWithMarkDown(url,text){
      url = url.trim(' ')
      text = text.trim(' ')
      return `[${url}](${text})`
    }
    }
)
  