<commit_before>import {consoleLogger} from 'tocco-util'

import {ATTRIBUTE_WIDGET_KEY, BOOTSTRAP_SCRIPT_OBJ_NAME, ERROR_CODE_INVALID_DOMAIN} from './constants'
import * as remoteLogger from './remoteLogger'
import {executeRequest, enhanceExtractedBody} from './requests'
import {attachMethods, getEventHandlers, getTheme, getVersion, loadScriptAsync} from './utils'

const getWidgetKey = container => container.getAttribute(ATTRIBUTE_WIDGET_KEY)

const handleRequestError = (backendUrl, response, key) => {
  const {ok, status, body} = response
  if (!ok) {
    if (status === 403 && body?.errorCode === ERROR_CODE_INVALID_DOMAIN) {
      remoteLogger.logError(backendUrl, body.message)
    }

    if (status === 404) {
      remoteLogger.logError(backendUrl, `Widget config '${key}' not found.`)
    }

    return undefined
  }
  return response
}

const initializeWidget = async (backendUrl, assetUrl, container) => {
  const key = getWidgetKey(container)
  const widgetConfig = await executeRequest(`${backendUrl}/nice2/rest/widget/configs/${key}`)
    .then(enhanceExtractedBody)
    .then(response => handleRequestError(backendUrl, response, key))
    .then(response => response?.body)
    .catch(error => {
      remoteLogger.logException(backendUrl, `Could not fetch widget config '${key}'.`, error)
    })

  if (widgetConfig) {
    const {appName, packageName, locale, config} = widgetConfig

    try {
      await loadScriptAsync(`${assetUrl}/js/tocco-${packageName}/dist/index.js`)
    } catch (error) {
      remoteLogger.logException(backendUrl, `Could not fetch package 'tocco-${packageName}'.`, error)
      return
    }

    const customTheme = getTheme()
    const input = {
      backendUrl,
      ...(customTheme ? {customTheme} : {}),
      locale,
      ...config,
      appContext: {
        embedType: 'widget',<commit_msg>chore(widget-utils): use different entry files per app in package<commit_after>import {consoleLogger} from 'tocco-util'

import {ATTRIBUTE_WIDGET_KEY, BOOTSTRAP_SCRIPT_OBJ_NAME, ERROR_CODE_INVALID_DOMAIN} from './constants'
import * as remoteLogger from './remoteLogger'
import {executeRequest, enhanceExtractedBody} from './requests'
import {attachMethods, getEventHandlers, getTheme, getVersion, loadScriptAsync} from './utils'

const getWidgetKey = container => container.getAttribute(ATTRIBUTE_WIDGET_KEY)

const handleRequestError = (backendUrl, response, key) => {
  const {ok, status, body} = response
  if (!ok) {
    if (status === 403 && body?.errorCode === ERROR_CODE_INVALID_DOMAIN) {
      remoteLogger.logError(backendUrl, body.message)
    }

    if (status === 404) {
      remoteLogger.logError(backendUrl, `Widget config '${key}' not found.`)
    }

    return undefined
  }
  return response
}

const initializeWidget = async (backendUrl, assetUrl, container) => {
  const key = getWidgetKey(container)
  const widgetConfig = await executeRequest(`${backendUrl}/nice2/rest/widget/configs/${key}`)
    .then(enhanceExtractedBody)
    .then(response => handleRequestError(backendUrl, response, key))
    .then(response => response?.body)
    .catch(error => {
      remoteLogger.logException(backendUrl, `Could not fetch widget config '${key}'.`, error)
    })

  if (widgetConfig) {
    const {appName, packageName, locale, config} = widgetConfig

    try {
      const entryFilename = packageName !== appName ? appName : 'index'
      await loadScriptAsync(`${assetUrl}/js/tocco-${packageName}/dist/${entryFilename}.js`)
    } catch (error) {
      remoteLogger.logException(backendUrl, `Could not fetch package 'tocco-${packageName}'.`, error)
      return
    }

    const customTheme = getTheme()
    const input = {
      backendUrl,
      ...(customTheme ? {customTheme} : {}),
      locale,
      ...config,
      appContext: {
        embedType: 'widget',
{'fraction_non_alphanumeric': 0.1298667363470081, 'fraction_numerical': 0.0036582179252678338, 'mean_word_length': 3.9973890339425586, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3314773', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1128, 'n_words': 267}