AboutLoginsRecordTelemetryEvent", {
      bubbles: true,
      detail: event,
    })
  );
}

export function setKeyboardAccessForNonDialogElements(enableKeyboardAccess) {
  const pageElements = document.querySelectorAll(
    "login-item, login-list, menu-button, login-filter, fxaccounts-button, [tabindex]"
  );

  let { activeElement: docActiveElement } = document;
  if (
    !enableKeyboardAccess &&
    docActiveElement &&
    !docActiveElement.closest("confirmation-dialog")
  ) {
    let elementToBlur =
      docActiveElement?.shadowRoot?.activeElement ?? docActiveElement;
    elementToBlur.blur();
  }

  pageElements.forEach(el => {
    if (!enableKeyboardAccess) {
      if (el.tabIndex > -1) {
        el.dataset.oldTabIndex = el.tabIndex;
      }
      el.tabIndex = "-1";
    } else if (el.dataset.oldTabIndex) {
      el.tabIndex = el.dataset.oldTabIndex;
      delete el.dataset.oldTabIndex;
    } else {
      el.removeAttribute("tabindex");
    }
  });
}

export function promptForPrimaryPassword(messageId, reason) {
  return new Promise(resolve => {
    window.AboutLoginsUtils.promptForPrimaryPassword(
      resolve,
      messageId,
      reason
    );
  });
}

/**
 * Initializes a dialog based on a template using shadow dom.
 *
 * @param {HTMLElement} element The element to attach the shadow dom to.
 * @param {string} templateSelector The selector of the template to be used.
 * @returns {object} The shadow dom that is attached.
 */
export function initDialog(element, templateSelector) {
  let template = document.querySelector(templateSelector);
  let shadowRoot = element.attachShadow({ mode: "open" });
  document.l10n.connectRoot(shadowRoot);
  shadowRoot.appendChild(template.content.cloneNode(true));
  return shadowRoot;
}
PK