module TranslationHelpers

def clear_editor(locator)

locator - The input field ID. The DOM element is selected using jQuery.

Handles how to clear a WYSIWYG editor.
def clear_editor(locator)
  page.execute_script <<-SCRIPT
    document.querySelector('##{locator} .editor-container .ProseMirror').innerHTML = '<p><br class="ProseMirror-trailingBreak"></p>';
    document.querySelector('##{locator} input').value = "";
  SCRIPT
end

def clear_i18n_editor(field, tab_selector, locales)

locales - an Array with the locales IDs.
"#title-tabs")
the tabs for this input. It usually is `"#-tabs" (e.g.
tab_selector - a String representing the ID of the HTML element that holds
locale-related part (e.g. `:participatory_process_title`)
field - the name of the field that should be cleared, without the

Handles how to clear i18n form fields which uses a WYSIWYG editor.
def clear_i18n_editor(field, tab_selector, locales)
  clear_i18n_fields(field, tab_selector, locales) do |locator|
    clear_editor locator
  end
end

def clear_i18n_fields(field, tab_selector, locales)

def clear_i18n_fields(field, tab_selector, locales)
  # Ensure the field is visible in the view to avoid "element has zero size"
  # errors
  scroll_to(find(tab_selector))
  locales.each do |locale|
    within tab_selector do
      click_on I18n.with_locale(locale) { t("name", scope: "locale") }
    end
    yield "#{field}_#{locale}"
  end
end

def fill_in_editor(locator, params = {})

:with - A String value that will be entered in the form field. (required)
params - A Hash of options
locator - The input field ID. The DOM element is selected using jQuery.

Handles how to fill a WYSIWYG editor.
def fill_in_editor(locator, params = {})
  raise ArgumentError if params[:with].blank?
  page.execute_script <<-SCRIPT
    document.querySelector('##{locator} .editor-container .ProseMirror').innerHTML = `#{params[:with]}`;
    document.querySelector('##{locator} input').value = `#{params[:with]}`;
  SCRIPT
end

def fill_in_i18n(field, tab_selector, localized_values)

are the values that will be entered in the form field.
localized_values - a Hash where the keys are the locales IDs and the values
"#title-tabs")
the tabs for this input. It usually is `"#-tabs" (e.g.
tab_selector - a String representing the ID of the HTML element that holds
locale-related part (e.g. `:participatory_process_title`)
field - the name of the field that should be filled, without the

Handles how to fill in i18n form fields.
def fill_in_i18n(field, tab_selector, localized_values)
  fill_in_i18n_fields(field, tab_selector, localized_values) do |locator, value|
    fill_in locator, with: value
  end
end

def fill_in_i18n_editor(field, tab_selector, localized_values)

are the values that will be entered in the form field.
localized_values - a Hash where the keys are the locales IDs and the values
"#title-tabs")
the tabs for this input. It usually is `"#-tabs" (e.g.
tab_selector - a String representing the ID of the HTML element that holds
locale-related part (e.g. `:participatory_process_title`)
field - the name of the field that should be filled, without the

Handles how to fill in i18n form fields which uses a WYSIWYG editor.
def fill_in_i18n_editor(field, tab_selector, localized_values)
  fill_in_i18n_fields(field, tab_selector, localized_values) do |locator, value|
    fill_in_editor locator, with: value
  end
end

def fill_in_i18n_fields(field, tab_selector, localized_values)

def fill_in_i18n_fields(field, tab_selector, localized_values)
  # Ensure the field is visible in the view to avoid "element has zero size"
  # errors
  scroll_to(find(tab_selector))
  localized_values.each do |locale, value|
    within tab_selector do
      click_on I18n.with_locale(locale) { t("name", scope: "locale") }
    end
    yield "#{field}_#{locale}", value
  end
end

def have_i18n_content(field, upcase: false, strip_tags: false)

upcase - a boolean to indicate whether the string must be checked upcased or not.
field - the field that holds the translations

HTML tags from the field (in case there are any).
Checks that the current page has some translated content. It strips the
def have_i18n_content(field, upcase: false, strip_tags: false)
  have_content(i18n_content(field, upcase:, strip_tags:).strip)
end

def have_no_i18n_content(field, upcase: false)

upcase - a boolean to indicate whether the string must be checked upcased or not.
field - the field that holds the translations

the HTML tags from the field (in case there are any).
Checks that the current page does not have some translated content. It strips
def have_no_i18n_content(field, upcase: false)
  have_no_content(i18n_content(field, upcase:))
end

def i18n_content(field, upcase: false, strip_tags: false)

upcase - a boolean to indicate whether the string must be checked upcased or not.
field - the field that holds the translations

Gives a specific language version of a field and (optionally) upcases it
def i18n_content(field, upcase: false, strip_tags: false)
  content = translated(field, locale: I18n.locale)
  content = if strip_tags
              Decidim::ApplicationController.helpers.strip_tags(content)
            else
              stripped(content)
            end
  upcase ? content.upcase : content
end

def t(key, scope: nil)

Allows using the `t` shortcut inside specs just like in views
def t(key, scope: nil)
  I18n.t(key, scope:, raise: true)
end

def translated(field, locale: I18n.locale)

translated attributes implementation can change more easily.
It is intended to be used to avoid the implementation details, so that the

locale defaults to the application's default one.
Gives the localized version of the attribute for the given locale. The
def translated(field, locale: I18n.locale)
  return field if field.is_a?(String)
  return if field.nil?
  field[locale.to_s] || field.dig("machine_translations", locale.to_s)
end