/** The class retrieves the next sibling element of an input container and creates a new error label if it does not already exist. */
export class SiblingContainerSelfRetrievalStrategy {
  /**
   * The function adds an error message container next to the input container if it doesn't already exist.
   *
   * @param {HTMLElement} inputContainer - The `inputContainer` parameter is an HTMLElement that
   * represents the container element of an input field in the HTML document.
   * @returns {HTMLElement} the `siblingContainer` HTMLElement that was either found as the next sibling of the
   * `inputContainer` or created as a new `label` element with the class `vega-error` if it didn't
   * exist before.
   */
  run(inputContainer) {
    let siblingContainer = inputContainer.nextElementSibling;
    if (siblingContainer && !siblingContainer.classList.contains('vega-error')) {
      // eslint-disable-next-line no-restricted-globals
      siblingContainer = document.createElement('label');
      siblingContainer.classList.add('vega-error');
      // vega-table needs to hide error content.
      siblingContainer.setAttribute('part', 'field-error-message');
      inputContainer.after(siblingContainer);
    }
    return siblingContainer;
  }
}
