package ru.forbion.f7cloud.feature.talk

/** Брендинг F7cloud в WebView Talk (как f7cloud_branding.js на сервере). */
object TalkWebBranding {
    fun brandingScriptUrl(serverUrl: String): String =
        "${serverUrl.trimEnd('/')}/themes/forbion/js/f7cloud_branding.js"

    fun jsAfterBranding(serverUrl: String, body: String): String {
        val url = brandingScriptUrl(serverUrl).replace("\\", "\\\\").replace("'", "\\'")
        return """
            (function() {
              var run = function() { $body };
              if (window.__f7cloudBranding) { run(); return; }
              var s = document.createElement('script');
              s.src = '$url';
              s.onload = run;
              s.onerror = run;
              (document.head || document.documentElement).appendChild(s);
            })();
        """.trimIndent()
    }
}
