/*!
 * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

import { getRole } from "dom-accessibility-api";
import { ElementError } from "./sanityChecks.js";
export const getControlledElement = ({
  element,
  role
}) => {
  if (element instanceof HTMLInputElement && element.list) {
    return element.list;
  }
  if (element.getAttribute("aria-expanded") === "false") {
    throw new ElementError("Popup isn't open in ARIA; therefore, it cannot be captured.", element);
  }
  const linkedElementIds = element.getAttribute("aria-controls") || element.getAttribute("aria-owns");
  if (!linkedElementIds) {
    throw new ElementError("Popup isn't linked; therefore, it cannot be captured.", element);
  }
  const linkedElement = linkedElementIds.split(" ").map(linkedElementId => element.ownerDocument.getElementById(linkedElementId)).filter(linkedElement => Boolean(linkedElement)).find(linkedElement => role ? getRole(linkedElement) === role : Boolean(linkedElement));
  if (!linkedElement) {
    throw new ElementError("Controlled element isn't available; therefore, it cannot be captured.", element);
  }
  return linkedElement;
};
//# sourceMappingURL=linkedHtmlSelectors.js.map