const MYTT_ORIGIN = 'https://www.mytischtennis.de';
const MYTT_PROXY_PREFIX = '/api/mytischtennis/proxy';

class MyTischtennisProxyService {
  getOrigin() {
    return MYTT_ORIGIN;
  }

  getProxyPrefix() {
    return MYTT_PROXY_PREFIX;
  }

  rewriteContent(content) {
    if (typeof content !== 'string' || !content) {
      return content;
    }

    let rewritten = content;

    rewritten = rewritten.replace(
      /(["'])\/build\//g,
      `$1${MYTT_PROXY_PREFIX}/build/`
    );
    rewritten = rewritten.replace(
      /(["'])\/fonts\//g,
      `$1${MYTT_PROXY_PREFIX}/fonts/`
    );

    rewritten = rewritten.replace(
      /https:\/\/www\.mytischtennis\.de\/build\//g,
      `${MYTT_PROXY_PREFIX}/build/`
    );
    rewritten = rewritten.replace(
      /https:\/\/www\.mytischtennis\.de\/fonts\//g,
      `${MYTT_PROXY_PREFIX}/fonts/`
    );

    rewritten = rewritten.replace(
      /url\((["']?)\/fonts\//g,
      `url($1${MYTT_PROXY_PREFIX}/fonts/`
    );

    rewritten = rewritten.replace(
      /(["'])\/api\/private-captcha/g,
      `$1${MYTT_PROXY_PREFIX}/api/private-captcha`
    );

    return rewritten;
  }
}

export default new MyTischtennisProxyService();
