# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require_relative 'test_flags'

# Form helpers below are loaded for the new stack app only and provide functions for returning the form field elements.
module FormHelpers
  @form_context = nil

  # Returns the outer container element of the form field via its label.
  #   The returned object is always an instance of `Capybara::Node::Element``, with some added sugar on top.
  def find_outer(label, **find_options)
    ZammadFormFieldCapybaraElementDelegator.new(find('.formkit-outer') { |element| element.has_css?('label', text: label, **find_options) }, @form_context)
  end

  # Usage:
  #
  #   find_input('Title')
  #   find_select('Owner')
  #   find_treeselect('Category')
  #   find_autocomplete('Customer')
  #   find_editor('Text')
  #   find_datepicker('Pending till')
  #   find_toggle('Remember me')
  #
  #   # In case of ambiguous labels, make sure to pass `exact_text` option
  #   find_datepicker(nil, exact_text: 'Date')
  #
  alias find_input find_outer
  alias find_select find_outer
  alias find_treeselect find_outer
  alias find_autocomplete find_outer
  alias find_editor find_outer
  alias find_datepicker find_outer
  alias find_toggle find_outer

  # Returns the outer container element of the form field radio via its ID.
  #   The returned object is always an instance of `Capybara::Node::Element``, with some added sugar on top.
  def find_radio(name, **find_options)
    ZammadFormFieldCapybaraElementDelegator.new(first("[name^=\"#{name}\"]", **find_options).ancestor('.formkit-outer'), @form_context)
  end

  # Provides a form context for stabilizing multiple field interactions.
  #   This is implemented by tracking of the expected form updater and other GraphQL responses.
  #   To define custom starting form updater response number, use the `form_updater_gql_number` argument (default: nil).
  #
  # Usage:
  #
  #   within_form(form_updater_gql_number: 2) do
  #     find_autocomplete('CC').search_for_options([email_address_1, email_address_2])
  #     find_autocomplete('Tags').search_for_options([tag_1, tag_2, tag_3]).select_options(%w[foo bar])
  #     find_editor('Text').type(body)
  #   end
  #
  def within_form(form_updater_gql_number: nil)
    setup_form_context(form_updater_gql_number)
    yield
    demolish_form_context
  end

  private

  def setup_form_context(form_updater_gql_number)
    @form_context = ZammadFormContext.new

    return if form_updater_gql_number.blank?

    @form_context.init_form_updater(form_updater_gql_number)
  end

  def demolish_form_context
    @form_context = nil
  end
end

# Extension below allows for execution of custom actions on the returned form field elements.
#   This class delegates any missing methods upstream to `Capybara::Node::Element` class.
class ZammadFormFieldCapybaraElementDelegator < SimpleDelegator
  attr_reader :element, :form_context

  include Capybara::DSL
  include BrowserTestHelper
  include TestFlags

  def initialize(element, form_context)
    @element = element
    @form_context = form_context

    super(element)
  end

  # Returns identifier of the form field.
  def field_id
    return element.find('.formkit-input', visible: :all)['id'] if input? || type_date? || type_datetime?
    return element.find('textarea')['id'] if type_textarea?
    return element.find('.formkit-fieldset')['id'] if type_radio?
    return element.find('[role="textbox"]')['id'] if type_editor?
    return element.find('[role="switch"], input[type="checkbox"]', visible: :all)['id'] if type_toggle? || type_checkbox?

    element.find('output', visible: :all)['id']
  end

  # Returns (hidden) input element used by several form field implementations to track the current value.
  #   NOTE: A returned element might not be a regular INPUT field due to custom implementation.
  def input_element
    element.find("##{field_id}", visible: :all)
  end

  # Searches treeselect and autocomplete fields for supplied option via its label and selects it.
  #
  # Usage:
  #
  #   find_treeselect('Tree Select').search_for_option('Parent 1::Option A')
  #   find_autocomplete('Tags').search_for_option(tag_1)
  #
  #   # To wait for a custom GraphQL response, you can provide expected `gql_filename` and/or `gql_number`.
  #   find_autocomplete('Custom').search_for_option('foo', gql_filename: 'shared/entities/user/graphql/queries/user.graphql', gql_number: 4)
  #
  #   # To select an autocomplete option with a different text than the query, provide an optional `label` parameter.
  #   find autocomplete('Customer').search_for_option(customer.email, label: customer.fullname)
  #
  def search_for_option(query, label: query, gql_filename: '', gql_number: 1, use_action: false, **find_options)
    return search_for_options(query, gql_filename: gql_filename, gql_number: gql_number, **find_options) if query.is_a?(Array)
    return search_for_tags_option(query, gql_filename: gql_filename, gql_number: gql_number) if type_tags?
    return search_for_autocomplete_option(query, label: label, gql_filename: gql_filename, gql_number: gql_number, use_action: use_action, **find_options) if autocomplete?

    raise 'Field does not support searching for options' if !type_treeselect?

    open_dropdown

    # calculate before closing, since we cannot access it, if dialog is closed
    is_multi_select = multi_select?

    browse_for_option(query, **find_options) do |option|

      # Filter input in desktop view is part of the element, not the dropdown/dialog.
      searchbox = if desktop_view?
                    element.find('[role="searchbox"]')
                  else
                    find('[role="searchbox"]')
                  end

      searchbox.fill_in with: option

      find('[role="option"]', text: option, **find_options).click

      maybe_wait_for_form_updater
    end

    send_keys(:escape) if is_multi_select

    wait_until_closed

    self # support chaining
  end

  # Searches treeselect and autocomplete fields for supplied options via their labels and selects them.
  #   NOTE: The field must support multiple selection, otherwise an error will be raised.
  #
  # Usage:
  #
  #   find_treeselect('Tree Select').search_for_options(['Parent 1::Option A', 'Parent 2::Option B', 'Option C'])
  #   find_autocomplete('Tags').search_for_options([tag_1, tag_2, tag_3])
  #
  #   # To wait for a custom GraphQL response, you can provide expected `gql_filename` and/or `gql_number`.
  #   find_autocomplete('Tags').search_for_option('foo', gql_number: 3)
  #
  def search_for_options(queries, labels: queries, gql_filename: '', gql_number: 1, use_action: false, **find_options)
    return search_for_tags_options(queries, gql_filename: gql_filename, gql_number: gql_number) if type_tags?
    return search_for_autocomplete_options(queries, labels: labels, gql_filename: gql_filename, gql_number: gql_number, use_action: use_action, **find_options) if autocomplete?

    raise 'Field does not support searching for options' if !type_treeselect?

    element.click

    wait_for_test_flag("field-tree-select-#{field_id}.opened")

    raise 'Field does not support multiple selection' if !multi_select?

    queries.each do |query|
      browse_for_option(query, **find_options) do |option, rewind|
        # Filter input in desktop view is part of the element, not the dropdown/dialog.
        searchbox = if desktop_view?
                      element.find('[role="searchbox"]')
                    else
                      find('[role="searchbox"]')
                    end

        searchbox.fill_in with: option

        find('[role="option"]', text: option, **find_options).click

        maybe_wait_for_form_updater

        rewind.call
      end
    end

    send_keys(:escape)

    wait_for_test_flag("field-tree-select-#{field_id}.closed")

    self # support chaining
  end

  # Selects an option in select, treeselect and autocomplete fields via its label.
  #   NOTE: The option must be part of initial options provided by the field, no autocomplete search will occur.
  #
  # Usage:
  #
  #   find_select('Owner').select_option('Test Admin Agent')
  #   find_treeselect('Tree Select').select_option('Parent 1::Option A')
  #   find_autocomplete('Organization').select_option(secondary_organizations.last.name)
  #
  def select_option(label, **find_options)
    return select_options(label, **find_options) if label.is_a?(Array)
    return select_treeselect_option(label, **find_options) if type_treeselect?
    return select_tags_option(label, **find_options) if type_tags?
    return select_autocomplete_option(label, **find_options) if autocomplete?

    raise 'Element is not a field of type select' if !type_select?

    open_dropdown

    # calculate before closing, since we cannot access it, if dialog is closed
    is_multi_select = multi_select?

    begin
      select_option_by_label(label, **find_options)
    rescue Capybara::ElementNotFound, Capybara::Playwright::Node::StaleReferenceError
      # An asynchronous form re-render (e.g. the processing of a form updater response of a
      #  previous field change) may close the dropdown again right after it was opened.
      # In this case open the dropdown once again and retry.
      # NOTE: only click if it is actually closed. The trigger's click handler toggles the
      #  dropdown, and `select_menu_open?` briefly lags behind it (e.g. during the collapse
      #  transition), so clicking while the dropdown still considers itself open would close
      #  it instead of reopening it, and 'common-select.opened' would then never fire again.
      if !select_menu_open?
        element.click

        wait_for_test_flag('common-select.opened')
      end

      select_option_by_label(label, **find_options)
    end

    send_keys(:escape) if is_multi_select

    wait_for_test_flag('common-select.closed')

    self # support chaining
  end

  # Selects multiple options in select, treeselect and autocomplete fields via its label.
  #   NOTE: The option must be part of initial options provided by the field, no autocomplete search will occur.
  #   NOTE: The field must support multiple selection, otherwise an error will be raised.
  #
  # Usage:
  #
  #   find_select('Multi Select').select_options(['Option 1', 'Option 2'])
  #   find_treeselect('Multi Tree Select').select_options(['Parent 1::Option A', 'Parent 2::Option C'])
  #   find_autocomplete('Tags').select_options(%w[foo bar])
  #
  def select_options(labels, **find_options)
    return select_treeselect_options(labels, **find_options) if type_treeselect?
    return select_tags_options(labels, **find_options) if type_tags?
    return select_autocomplete_options(labels, **find_options) if autocomplete?

    raise 'Element is not a field of type select' if !type_select?

    open_dropdown

    raise 'Field does not support multiple selection' if !multi_select?

    labels.each do |label|
      select_option_by_label(label, **find_options)
    end

    send_keys(:escape)

    wait_for_test_flag('common-select.closed')

    self # support chaining
  end

  # Clears selection in select, treeselect and autocomplete fields.
  #   NOTE: The field must support selection clearing, otherwise an error will be raised.
  def clear_selection
    raise 'Field does not support clearing selection' if !type_select? && !type_treeselect? && !autocomplete?

    element.find('[role="button"][aria-label="Clear selection"]').click

    maybe_wait_for_form_updater

    self # support chaining
  end

  # Types the provided text into an input or editor field.
  #
  # Usage:
  #
  #   find_input('Title').type(body)
  #   find_editor('Text').type(body)
  #
  def type(text, **type_options)
    return type_editor(text, **type_options) if type_editor?

    input_element.click.send_keys(text)

    maybe_wait_for_form_updater

    self # support chaining
  end

  def type_editor(text, click: true, skip_waiting: false, wait_for: nil)
    raise 'Field does not support typing' if !type_editor?

    move_editor_caret_to_start if click
    input_element.send_keys(text)

    maybe_wait_for_form_updater if !skip_waiting

    sleep wait_for if wait_for

    self # support chaining
  end

  # Clears the input of text, editor, date and datetime fields.
  def clear
    return clear_date if type_date? || type_datetime?

    raise 'Field does not support clearing' if !input? && !type_editor? && !type_textarea?

    input_element.click.send_keys([magic_key, 'a'], :backspace)

    maybe_wait_for_form_updater

    self # support chaining
  end

  # Selects a date in a date picker field.
  #
  # Usage:
  #   find_datepicker('Date Picker').select_date(Date.today)
  #   find_datepicker('Date Picker').select_date('2023-01-01')
  #
  def select_date(date)
    raise 'Field does not support selecting dates' if !type_date? && !type_datetime?

    date = Date.parse(date) if !date.is_a?(Date) && !date.is_a?(DateTime) && !date.is_a?(Time)

    open_date_picker
    pick_date(date)

    if block_given?
      yield
    elsif type_datetime?
      finish_time_picker
    end

    wait_until_closed

    maybe_wait_for_form_updater

    self # support chaining
  end

  # Selects a date and enters time in a datetime picker field.
  #
  # Usage:
  #   find_datepicker('Date Time').select_datetime(DateTime.now)
  #   find_datepicker('Date Time').select_datetime('2023-01-01T09:00:00.000Z')
  #
  def select_datetime(datetime)
    raise 'Field does not support selecting datetimes' if !type_datetime?

    datetime = DateTime.parse(datetime) if !datetime.is_a?(DateTime) && !datetime.is_a?(Time)

    select_date(datetime) do
      if desktop_view?
        pick_time_desktop_view(datetime)
      else
        pick_time_mobile_view(datetime)
      end

      finish_time_picker
    end
  end

  # Types date into a date field (desktop view only, the mobile view offers no typing).
  #
  # Usage:
  #   find_datepicker('Date Picker').type_date(Date.today)
  #   find_datepicker('Date Picker').type_date('2023-01-01')
  #
  def type_date(date)
    raise 'Field does not support typing dates' if !type_date?

    date = Date.parse(date) if !date.is_a?(Date)

    # TODO: Support locales other than `en`, depending on the language of the current user.
    type_date_segments(date.strftime('%m%d%Y'))
  end

  # Types date and time into a date field (desktop view only, the mobile view offers no typing).
  #
  # Usage:
  #   find_datepicker('Date Time').type_datetime(DateTime.now)
  #   find_datepicker('Date Picker').type_datetime('2023-01-01T09:00:00.000Z')
  #
  def type_datetime(datetime)
    raise 'Field does not support typing datetimes' if !type_datetime?

    datetime = DateTime.parse(datetime) if !datetime.is_a?(DateTime) && !datetime.is_a?(Time)

    # TODO: Support locales other than `en`, depending on the language of the current user.
    type_date_segments(datetime.strftime('%m%d%Y%I%M%p').downcase)
  end

  # Selects a choice in a radio form field.
  #
  # Usage:
  #
  #   find_radio('articleSenderType').select_option('Outbound Call')
  #
  def select_choice(choice, **find_options)
    raise 'Field does not support choice selection' if !type_radio?

    input_element.find('label', exact_text: choice, **find_options).click

    maybe_wait_for_form_updater

    self # support chaining
  end

  def toggle
    raise 'Field does not support toggling' if !type_toggle? && !type_checkbox?

    element.find('label').click

    self # support chaining
  end

  def toggle_on
    raise 'Field does not support toggling on' if !type_toggle? && !type_checkbox?

    element.find('label').click if input_element['aria-checked'] == 'false' || !input_element.checked?

    self # support chaining
  end

  def toggle_off
    raise 'Field does not support toggling off' if !type_toggle? && !type_checkbox?

    element.find('label').click if input_element['aria-checked'] == 'true' || input_element.checked?

    self # support chaining
  end

  def open
    element.click
    wait_until_opened

    self # support chaining
  end

  def close
    send_keys(:escape)
    wait_until_closed

    self # support chaining
  end

  def menu_element
    return dropdown_element if desktop_view?

    dialog_element
  end

  # Checks if the menu of a select field is currently shown.
  #   A short wait (instead of an instant snapshot) tolerates the brief window where a
  #   collapse-enter transition has inserted the menu but not yet restored its real height,
  #   during which it is present in the DOM but not yet considered visible.
  def select_menu_open?
    if desktop_view?
      page.has_css?('#common-select > [role="menu"]', wait: 1)
    else
      page.has_css?('#common-select[role="dialog"]', wait: 1)
    end
  end

  # Dropdowns are teleported to the root element, so we must search them within the document body.
  #   A short explicit wait (instead of none) tolerates the same brief window as
  #   `select_menu_open?`, while still failing fast if the dropdown genuinely never opens.
  def dropdown_element
    if type_select? || type_tags? || autocomplete?
      page.find('#common-select > [role="menu"]', wait: 1)
    elsif type_treeselect?
      page.find('#field-tree-select-input-dropdown > [role="menu"]', wait: 1)
    end
  end

  # Dialogs are teleported to the root element, so we must search them within the document body.
  #   A short explicit wait (instead of none) tolerates the same brief window as
  #   `select_menu_open?`, while still failing fast if the dialog genuinely never opens.
  def dialog_element
    if type_select?
      page.find('#common-select[role="dialog"]', wait: 1)
    elsif type_treeselect?
      page.find("#dialog-field-tree-select-#{field_id}", wait: 1)
    elsif type_tags?
      page.find("#dialog-field-tags-#{field_id}", wait: 1)
    elsif autocomplete?
      page.find("#dialog-field-auto-complete-#{field_id}", wait: 1)
    end
  end

  private

  def method_missing(method_name, *, &)

    # Simulate pseudo-methods in format of `#type_[name]?` in order to determine the internal type of the field.
    if method_name.to_s =~ %r{^type_(.+)\?$}
      return element['data-type'] == $1
    end

    super
  end

  def respond_to_missing?(method_name, include_private = false)
    method_name.to_s =~ %r{^type_(.+)\?$} || super
  end

  def input?
    type_text? || type_color? || type_email? || type_number? || type_tel? || type_url? || type_password?
  end

  def autocomplete?
    type_autocomplete? || type_customer? || type_organization? || type_recipient? || type_externalDataSource? || type_ticket?
  end

  # Input elements in supported fields define data attribute for "multiple" state.
  def multi_select?
    input_element['data-multiple'] == 'true'
  end

  # An autofocused dropdown field opens by itself once its surroundings settled, and a click would close it again.
  def open_dropdown
    if desktop_view?
      element.assert_no_selector('[data-dropdown-opening]')

      element.click if input_element['aria-expanded'] != 'true'
    else
      element.click
    end

    wait_until_opened
  end

  def wait_until_opened
    return wait_until_opened_desktop_view if desktop_view?
    return wait_for_test_flag('common-select.opened') if type_select?
    return wait_for_test_flag("field-tree-select-#{field_id}.opened") if type_treeselect?
    return wait_for_test_flag('field-date.opened') if type_date? || type_datetime?
    return wait_for_test_flag("field-tags-#{field_id}.opened") if type_tags?
    return wait_for_test_flag("field-auto-complete-#{field_id}.opened") if autocomplete?

    raise "Couldn't detect if element was opened"
  end

  def wait_until_opened_desktop_view
    return wait_for_test_flag('common-select.opened') if type_select? || type_tags? || autocomplete?
    return wait_for_test_flag('field-tree-select-input-dropdown.opened') if type_treeselect?
    return wait_for_test_flag('field-date.opened') if type_date? || type_datetime?

    raise "Couldn't detect if element was opened"
  end

  def wait_until_closed
    return wait_until_closed_desktop_view if desktop_view?
    return wait_for_test_flag('common-select.closed') if type_select?
    return wait_for_test_flag("field-tree-select-#{field_id}.closed") if type_treeselect?
    return wait_for_test_flag('field-date.closed') if type_date? || type_datetime?
    return wait_for_test_flag("field-tags-#{field_id}.closed") if type_tags?
    return wait_for_test_flag("field-auto-complete-#{field_id}.closed") if autocomplete?

    raise "Couldn't detect if element was closed"
  end

  def wait_until_closed_desktop_view
    return wait_for_test_flag('common-select.closed') if type_select? || type_tags? || autocomplete?
    return wait_for_test_flag('field-tree-select-input-dropdown.closed') if type_treeselect?
    return wait_for_test_flag('field-date.closed') if type_date? || type_datetime?

    raise "Couldn't detect if element was closed"
  end

  def select_option_by_label(label, **find_options)
    within menu_element do
      find('[role="option"]', text: label, **find_options).click

      maybe_wait_for_form_updater
    end
  end

  def browse_for_option(path, **find_options)
    components = path.split('::')

    # Goes back to the root page by clicking on back button multiple times.
    rewind = proc do
      depth = components.size - 1
      depth.times do
        find('[role="button"][aria-label="Back to previous page"]').click
      end
    end

    child_menu_button = if desktop_view?
                          'div[role="button"]'
                        else
                          'svg[role=link]'
                        end

    components.each_with_index do |option, index|

      # Child option is always the last item.
      if index == components.size - 1
        within menu_element do
          yield option, rewind
        end

        next
      end

      # Parents come before.
      within menu_element do
        find('[role="option"] span', text: option, **find_options).sibling(child_menu_button).click
      end
    end
  end

  def search_for_tags_option(query, gql_filename: '', gql_number: 1)
    open_dropdown

    within menu_element do
      # Filter input in desktop view is part of the element, not the dropdown/dialog.
      searchbox = if desktop_view?
                    element.find('[role="searchbox"]')
                  else
                    find('[role="searchbox"]')
                  end

      searchbox.fill_in with: query

      send_keys(:tab)

      wait_for_autocomplete_gql(gql_filename, gql_number)
    end

    send_keys(:escape)

    wait_until_closed

    maybe_wait_for_form_updater

    self # support chaining
  end

  def search_for_autocomplete_option(query, label: query, gql_filename: '', gql_number: 1, already_open: false, use_action: false, **find_options)
    open_dropdown if !already_open

    # calculate before closing, since we cannot access it, if dialog is closed
    is_multi_select = multi_select?

    within menu_element do
      # Filter input in desktop view is part of the element, not the dropdown/dialog.
      searchbox = if desktop_view?
                    element.find('[role="searchbox"]')
                  else
                    find('[role="searchbox"]')
                  end

      searchbox.fill_in with: query

      wait_for_autocomplete_gql(gql_filename, gql_number)

      if use_action
        find('[role="button"]', **find_options).click
      else
        find('[role="option"]', text: label, **find_options).click
      end

      maybe_wait_for_form_updater
    end

    send_keys(:escape) if is_multi_select

    wait_until_closed

    self # support chaining
  end

  def search_for_tags_options(queries, gql_filename: '', gql_number: 1)
    open_dropdown

    raise 'Field does not support multiple selection' if !multi_select?

    within menu_element do
      queries.each do |query|
        # Filter input in desktop view is part of the element, not the dropdown/dialog.
        searchbox = if desktop_view?
                      element.find('[role="searchbox"]')
                    else
                      find('[role="searchbox"]')
                    end

        searchbox.fill_in with: query

        send_keys(:tab)

        wait_for_autocomplete_gql(gql_filename, gql_number)
      end
    end

    send_keys(:escape)

    wait_until_closed

    maybe_wait_for_form_updater

    self # support chaining
  end

  def search_for_autocomplete_options(queries, labels: queries, gql_filename: '', gql_number: 1, use_action: false, **find_options)
    open_dropdown

    within menu_element do
      queries.each_with_index do |query, index|

        # Filter input in desktop view is part of the element, not the dropdown/dialog.
        searchbox = if desktop_view?
                      element.find('[role="searchbox"]')
                    else
                      find('[role="searchbox"]')
                    end

        searchbox.fill_in with: query

        wait_for_autocomplete_gql(gql_filename, gql_number + index)

        raise 'Field does not support multiple selection' if !multi_select?

        if use_action
          find('[role="button"]', text: 'add new email address', **find_options).click
        else
          find('[role="option"]', text: labels[index], **find_options).click
        end

        maybe_wait_for_form_updater

        if !use_action
          find('[aria-label="Clear search"]').click
        end
      end
    end

    send_keys(:escape)

    wait_until_closed

    self # support chaining
  end

  def select_treeselect_option(label, **find_options)
    open_dropdown

    # calculate before closing, since we cannot access it, if dialog is closed
    is_multi_select = multi_select?

    browse_for_option(label, **find_options) do |option|
      find('[role="option"]', text: option, **find_options).click

      maybe_wait_for_form_updater
    end

    send_keys(:escape) if is_multi_select

    wait_until_closed

    self # support chaining
  end

  def select_tags_option(label, **find_options)
    open_dropdown

    select_option_by_label(label, **find_options)

    send_keys(:escape)

    wait_until_closed

    self # support chaining
  end

  def select_autocomplete_option(label, **find_options)
    open_dropdown

    # calculate before closing, since we cannot access it, if dialog is closed
    is_multi_select = multi_select?

    select_option_by_label(label, **find_options)

    send_keys(:escape) if is_multi_select

    wait_until_closed

    self # support chaining
  end

  def select_treeselect_options(labels, **find_options)
    open_dropdown

    raise 'Field does not support multiple selection' if !multi_select?

    labels.each do |label|
      browse_for_option(label, **find_options) do |option, rewind|
        find('[role="option"]', text: option, **find_options).click

        maybe_wait_for_form_updater

        rewind.call
      end
    end

    send_keys(:escape)

    wait_until_closed

    self # support chaining
  end

  def select_tags_options(labels, **find_options)
    open_dropdown

    raise 'Field does not support multiple selection' if !multi_select?

    labels.each do |label|
      select_option_by_label(label, **find_options)
    end

    send_keys(:escape)

    wait_until_closed

    self # support chaining
  end

  def select_autocomplete_options(labels, **find_options)
    open_dropdown

    raise 'Field does not support multiple selection' if !multi_select?

    labels.each do |label|
      select_option_by_label(label, **find_options)
    end

    send_keys(:escape)

    wait_until_closed

    self # support chaining
  end

  # If a GraphQL filename is passed, we will explicitly wait for it here.
  #   Otherwise, we will implicitly wait for a query depending on the type of the field.
  #   If no waits are to be done, we display a friendly warning to devs, since this can lead to some instability.
  #   In form context, expected response number will be automatically increased and tracked.
  def wait_for_autocomplete_gql(gql_filename, gql_number)
    gql_number = autocomplete_gql_number(gql_filename) || gql_number

    if gql_filename.present?
      wait_for_gql(gql_filename, number: gql_number)
    elsif type_customer?
      query_name = if desktop_view?
                     'shared/components/Form/fields/FieldCustomer/graphql/queries/autocompleteSearch/generic.graphql'
                   else
                     'shared/components/Form/fields/FieldCustomer/graphql/queries/autocompleteSearch/user.graphql'
                   end
      wait_for_gql(query_name, number: gql_number)
    elsif type_organization?
      wait_for_gql('shared/components/Form/fields/FieldOrganization/graphql/queries/autocompleteSearch/organization.graphql', number: gql_number)
    elsif type_recipient?
      wait_for_gql('shared/components/Form/fields/FieldRecipient/graphql/queries/autocompleteSearch/recipient.graphql', number: gql_number)
    elsif type_externalDataSource?
      wait_for_gql('shared/components/Form/fields/FieldExternalDataSource/graphql/queries/autocompleteSearchObjectAttributeExternalDataSource.graphql', number: gql_number)
    elsif type_ticket?
      wait_for_gql('shared/entities/ticket/graphql/queries/autocompleteSearchTicket.graphql', number: gql_number)
    elsif type_tags?
      # NB: tags autocomplete query fires only once?!
      wait_for_gql('shared/entities/tags/graphql/queries/autocompleteTags.graphql', number: 1, skip_clearing: true)
    else
      warn 'Warning: missing `wait_for_gql` in `search_for_autocomplete_option()`, might lead to instability'
    end
  end

  def autocomplete_gql_number(gql_filename)
    return nil if form_context.nil?

    return form_context.form_gql_number(:autocomplete) if gql_filename.present?
    return form_context.form_gql_number(:customer) if type_customer?
    return form_context.form_gql_number(:organization) if type_organization?
    return form_context.form_gql_number(:recipient) if type_recipient?
    return form_context.form_gql_number(:externalDataSource) if type_externalDataSource?
    return form_context.form_gql_number(:ticket) if type_ticket?

    form_context.form_gql_number(:tags) if type_tags?
  end

  def triggers_form_updater?
    element['data-triggers-form-updater'] == 'true'
  end

  # Returns whether the caret is at the start of the editor, and moves it there if not.
  EDITOR_CARET_TO_START_JS = <<~JS.freeze
    ((el) => {
      const { view, state: { selection, doc } } = el.editor;
      const domSelection = document.getSelection();

      let start = null;
      doc.descendants((node, pos) => {
        if (start !== null) return false;
        if (node.isTextblock) {
          start = pos + 1;
          return false;
        }
      });

      // The browser inserts typed text at its own caret, which the editor reads asynchronously.
      const atStart = document.activeElement === el &&
        selection.empty && selection.from === start &&
        domSelection.isCollapsed && view.posAtDOM(domSelection.anchorNode, domSelection.anchorOffset) === start;

      if (!atStart) el.editor.commands.focus('start');

      return atStart;
    })(this)
  JS

  # The click leaves the caret at the click position, e.g. inside a signature, and
  #   go-to-start keys are unreliable: headless Chrome on macOS ignores them, and in
  #   CI they sometimes had no effect. Place the caret via the editor instead, until
  #   it stays there.
  def move_editor_caret_to_start
    input_element.click

    wait.until { input_element.evaluate_script(EDITOR_CARET_TO_START_JS) }
  rescue BrowserTestHelper::Waiter::TimeoutError
    raise 'The caret did not stay at the start of the editor'
  end

  def maybe_wait_for_form_updater
    return if form_context.nil? || !triggers_form_updater?

    gql_number = form_context.form_gql_number(:form_updater)

    wait_for_form_updater(gql_number)
  end

  def clear_date
    element.find('[role="button"][aria-label="Clear the value"]').click

    maybe_wait_for_form_updater

    self # support chaining
  end

  # The desktop view opens the panel with the icon button next to the input, the mobile view with
  #   the input itself.
  def open_date_picker
    if desktop_view?
      element.find('[role="button"][aria-label^="Choose date"], [role="button"][aria-label^="Change date"]').click
    else
      input_element.click
    end

    wait_until_opened
  end

  # The desktop panel is rendered at the end of the document, outside of the field.
  def date_picker_panel
    return page.document.find('[data-test-id="field-date-panel"]') if desktop_view?

    element.find('[data-test-id="field-date-panel"]')
  end

  # Walks from the day view up to the decade view and back down to the day, so any date is reached
  #   with a known number of clicks.
  def pick_date(date)
    panel = date_picker_panel

    panel.find('button[aria-label="Open the months overlay"]').click
    panel.find('button[aria-label="Open the years overlay"]').click

    year_grid = panel.find('[role="grid"][aria-label="The year picker"]')
    decades   = (date.year - year_grid.first('button').text.to_i) / 10

    decades.abs.times do
      panel.find("button[aria-label=\"#{decades.positive? ? 'Next' : 'Previous'} decade\"]").click
    end

    panel.find('[role="grid"][aria-label="The year picker"]').find('button', exact_text: date.year.to_s).click
    panel.find("button[aria-label=\"#{date.strftime('%B %Y')}\"]").click
    panel.find("button[aria-label=\"#{date.strftime('%A, %B')} #{date.day.ordinalize}, #{date.year}\"]").click
  end

  def twelve_hour_clock?(panel)
    panel.has_css?('button[aria-label="Toggle AM/PM mode"]', wait: 0)
  end

  def pick_meridiem(panel, datetime)
    return if !twelve_hour_clock?(panel)

    toggle = panel.find('button[aria-label="Toggle AM/PM mode"]')
    toggle.click if toggle.text.casecmp?(datetime.strftime('%p')) == false
  end

  def pick_time_desktop_view(datetime)
    panel = date_picker_panel
    hour  = twelve_hour_clock?(panel) ? datetime.strftime('%I') : datetime.strftime('%H')

    # The number inputs apply their value once they are left.
    panel.find('input[aria-label="Hours"]').fill_in(with: hour).send_keys(:tab)
    panel.find('input[aria-label="Minutes"]').fill_in(with: datetime.strftime('%M')).send_keys(:tab)

    pick_meridiem(panel, datetime)
  end

  # The minutes grid offers five-minute steps, the stepper covers the rest.
  def pick_time_mobile_view(datetime)
    panel = date_picker_panel
    hour  = twelve_hour_clock?(panel) ? datetime.strftime('%I') : datetime.strftime('%H')

    panel.find('button[aria-label="Open the hours overlay"]').click
    panel.find('[role="option"]', exact_text: hour).click

    nearest_minute = datetime.min / 5 * 5

    panel.find('button[aria-label="Open the minutes overlay"]').click
    panel.find('[role="option"]', exact_text: format('%02d', nearest_minute)).click

    (datetime.min - nearest_minute).times do
      panel.find('button[aria-label="Increment minutes value"]').click
    end

    pick_meridiem(panel, datetime)
  end

  def finish_time_picker
    date_picker_panel.find('button', exact_text: 'Done').click
  end

  # The segmented input takes the digits group by group from the first one, a trailing tab leaves
  #   the field and settles the entry. Keys only: a click behind the groups would open the panel.
  def type_date_segments(text)
    raise 'Typing into date fields is not supported on the mobile view, use select_date or select_datetime instead' if !desktop_view?

    input_element.send_keys(:home, text, :tab)

    maybe_wait_for_form_updater

    self # support chaining
  end

  def desktop_view?
    RSpec.current_example.metadata[:app] == :desktop_view
  end
end

class ZammadFormContext
  attr_reader :context

  def initialize
    @context = {}
  end

  def init_form_updater(number)
    context[:gql_number] = {}
    context[:gql_number][:form_updater] = number
  end

  def form_gql_number(name)
    if context[:gql_number].nil?
      context[:gql_number] = {}
    end

    if context[:gql_number][name].nil?
      context[:gql_number][name] = 1
    else
      context[:gql_number][name] += 1
    end

    context[:gql_number][name]
  end
end

RSpec.configure do |config|
  config.include FormHelpers, type: :system, app: :mobile
  config.include FormHelpers, type: :system, app: :desktop_view
end
