"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.querySelector = exports.captureElement = void 0;
var _getByQuerySelector = require("./getByQuerySelector.cjs");
var _getComputedAccessibleText = require("./getComputedAccessibleText.cjs");
var _linkedHtmlSelectors = require("./linkedHtmlSelectors.cjs");
var _sanityChecks = require("./sanityChecks.cjs");
/*!
 * 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.
 */

const captureElement = ({
  containerElement,
  queryMethod,
  querySelectorOptions,
  role,
  testSelector
}) => {
  if ("elementSelector" in testSelector && querySelectorOptions) {
    const sharedProps = {
      element: containerElement,
      queryMethod,
      queryOptions: Object.fromEntries(Object.entries(testSelector.elementSelector.options).map(([testSelectorOptionKey, testingLibraryOptionKey]) => [testingLibraryOptionKey, querySelectorOptions[testSelectorOptionKey]]))
    };
    if (testSelector.elementSelector.method === "ByRole") {
      return (0, _getByQuerySelector.getByRoleQuerySelector)({
        ...sharedProps,
        role: Array.isArray(testSelector.elementSelector.role) || role ? role || "" : testSelector.elementSelector.role
      });
    }
    return (0, _getByQuerySelector.getByTextQuerySelector)({
      ...sharedProps,
      selectionMethod: testSelector.elementSelector.method,
      text: testSelector.elementSelector.text
    });
  } else if ("isControlledElement" in testSelector && testSelector.isControlledElement) {
    try {
      return (0, _linkedHtmlSelectors.getControlledElement)({
        element: containerElement
      });
    } catch (error) {
      if (queryMethod === "query") {
        return null;
      }
      throw error;
    }
  }
  return null;
};
exports.captureElement = captureElement;
const querySelector = testSelector => props => {
  const {
    element: containerElement,
    queryMethod
  } = props;
  const capturedElement = captureElement({
    containerElement,
    queryMethod: queryMethod || "get",
    querySelectorOptions: "options" in props ? props.options : undefined,
    role: "role" in props ? props.role : undefined,
    testSelector
  });
  const getAccessibleText = labelName => {
    if (!capturedElement) {
      throw new _sanityChecks.ElementError("No child HTML element available", containerElement);
    }
    if (!("accessibleText" in testSelector)) {
      throw new Error("Missing `accessibleText` in `TestSelector`");
    }
    return (0, _getComputedAccessibleText.getComputedAccessibleText)({
      element: capturedElement,
      type: testSelector.accessibleText[labelName]
    });
  };
  const selectChild = childProps => {
    if (!capturedElement) {
      throw new _sanityChecks.ElementError("No child HTML element available", containerElement);
    }
    if (!("children" in testSelector)) {
      throw new Error("Missing `children` in `TestSelector`");
    }
    return querySelector(testSelector.children[childProps.name])({
      element: capturedElement,
      queryMethod: childProps.queryMethod,
      ...("options" in childProps && childProps.options ? {
        options: childProps.options
      } : {}),
      ...("role" in childProps && childProps.role ? {
        role: childProps.role
      } : {})
    });
  };
  return {
    element: capturedElement,
    getAccessibleText: getAccessibleText,
    selectChild: selectChild
  };
};
exports.querySelector = querySelector;
//# sourceMappingURL=querySelector.cjs.map