{"size": 2379, "ext": "js", "max_stars_repo_path": "src/select-options.js", "max_stars_repo_name": "DanielRHayes/user-event", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/select-options.js", "max_issues_repo_name": "DanielRHayes/user-event", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/select-options.js", "max_forks_repo_name": "DanielRHayes/user-event", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.7375, "max_line_length": 114, "alphanum_fraction": 0.6473308113}
import {createEvent, getConfig, fireEvent} from '@testing-library/dom'
import {click} from './click'
import {focus} from './focus'

function selectOptionsBase(newValue, select, values, init) {
  if (!newValue && !select.multiple) {
    throw getConfig().getElementError(
      `Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead.`,
      select,
    )
  }
  const valArray = Array.isArray(values) ? values : [values]
  const allOptions = Array.from(select.querySelectorAll('option'))
  const selectedOptions = valArray
    .map(val => {
      if (allOptions.includes(val)) {
        return val
      } else {
        const matchingOption = allOptions.find(o => o.value === val)
        if (matchingOption) {
          return matchingOption
        } else {
          throw getConfig().getElementError(
            `Value "${val}" not found in options`,
            select,
          )
        }
      }
    })
    .filter(option => !option.disabled)

  if (select.disabled || !selectedOptions.length) return

  if (select.multiple) {
    for (const option of selectedOptions) {
      // events fired for multiple select are weird. Can't use hover...
      fireEvent.pointerOver(option, init)
      fireEvent.pointerEnter(select, init)
      fireEvent.mouseOver(option)
      fireEvent.mouseEnter(select)
      fireEvent.pointerMove(option, init)
      fireEvent.mouseMove(option, init)
      fireEvent.pointerDown(option, init)
      fireEvent.mouseDown(option, init)
      focus(select, init)
      fireEvent.pointerUp(option, init)
      fireEvent.mouseUp(option, init)
      selectOption(option)
      fireEvent.click(option, init)
    }
  } else if (selectedOptions.length === 1) {
    click(select, init)
    selectOption(selectedOptions[0])
  } else {
    throw getConfig().getElementError(
      `Cannot select multiple options on a non-multiple select`,
      select,
    )
  }

  function selectOption(option) {
    option.selected = newValue
    fireEvent(
      select,
      createEvent('input', select, {
        bubbles: true,
        cancelable: false,
        composed: true,
        ...init,
      }),
    )
    fireEvent.change(select, init)
  }
}

const selectOptions = selectOptionsBase.bind(null, true)
const deselectOptions = selectOptionsBase.bind(null, false)

export {selectOptions, deselectOptions}
