zy.AutofillFormFactory.findRootForField(element);
    return this.#formHandlerByRootElement.get(rootElement);
  }

  /**
   * Get the form handler for the specified root element.
   *
   * @param {string} rootElementId
   *        the id of the root element
   * @returns {FormAutofillHandler | null}
   *        The form handler associated with the specified input element.
   */
  getFormHandlerByRootElementId(rootElementId) {
    return this.#formHandlerByRootId.get(rootElementId);
  }

  /**
   * Get the form handler for the specified input element. If no handler exists
   * in the cache, this function creates a new one.
   *
   * @param {HTMLElement} element
   *        Focused input which triggered profile searching
   * @returns {FormAutofillHandler}
   *        The form handler associated with the specified input element.
   */
  getOrCreateFormHandler(element) {
    let handler = this.getFormHandler(element);
    if (!handler) {
      handler = new lazy.FormAutofillHandler(
        lazy.AutofillFormFactory.createFromField(element),
        this.onFilledModifiedCallback
      );

      const root = handler.form.rootElement;
      const rootElementId = lazy.FormAutofillUtils.getElementIdentifier(root);
      this.#formHandlerByRootId.set(rootElementId, handler);
      this.#formHandlerByRootElement.set(root, handler);
    }
    return handler;
  }

  removeFormHandlerByElementEntries(handler) {
    handler.form.elements.forEach(element =>
      this.#formHandlerByElement.delete(element)
    );
  }

  addFormHandlerByElementEntries(handler) {
    handler.form.elements.forEach(element => {
      if (!this.#formHandlerByElement.has(element, handler)) {
        this.#formHandlerByElement.set(element, handler);
      }
    });
  }
}

export default FormStateManager;
PK