"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
var _exportNames = {
  findByHook: true,
  getByHook: true,
  getAllByHook: true,
  findByHookAtIndex: true,
  countByHook: true,
  getElement: true,
  getDataAttributeValue: true,
  isElementFocused: true,
  baseUniDriverFactory: true,
  ReactBase: true
};
exports.isElementFocused = exports.getElement = exports.getDataAttributeValue = exports.getByHook = exports.getAllByHook = exports.findByHookAtIndex = exports.findByHook = exports.countByHook = exports.baseUniDriverFactory = void 0;
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _unidriver = require("@wix/wix-ui-test-utils/unidriver");
var _ReactBase = require("./ReactBase");
exports.ReactBase = _ReactBase.ReactBase;
Object.keys(_ReactBase).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _ReactBase[key]) return;
  exports[key] = _ReactBase[key];
});
/**
 * Find element by `data-hook` (exact match)
 * @param {UniDriver} base
 * @param {string} hook
 */
var findByHook = (base, hook) => base.$("[data-hook=\"".concat(hook, "\"]"));

/**
 * Async find element by `data-hook` (exact match)
 * @param {UniDriver} base
 * @param {string} hook
 * @returns {Promise<import('@wix/unidriver-core').UniDriver>}
 */
exports.findByHook = findByHook;
var getByHook = (base, hook) => base.$("[data-hook=\"".concat(hook, "\"]"));

/**
 * Async find all elements by `data-hook` (exact match)
 * @param {UniDriver} base
 * @param {string} hook
 * @returns {Promise<import('@wix/wix-ui-test-utils/dist/types/unidriver').UniDriverList>}
 */
exports.getByHook = getByHook;
var getAllByHook = (base, hook) => base.$$("[data-hook=\"".concat(hook, "\"]"));

/**
 * Find element by `data-hook` at index (exact match)
 * @param {UniDriver} base
 * @param {string} hook
 * @param index index
 */
exports.getAllByHook = getAllByHook;
var findByHookAtIndex = (base, hook, index) => base.$$("[data-hook=\"".concat(hook, "\"]")).get(index);

/**
 * Counts elements by `data-hook` (exact match)
 * @param {UniDriver} base
 * @param {string} hook
 */
exports.findByHookAtIndex = findByHookAtIndex;
var countByHook = (base, hook) => base.$$("[data-hook=\"".concat(hook, "\"]")).count();

/**
 * Wrapper function which returns null if base doesn't exist.
 * @param {UniDriver} base
 */
exports.countByHook = countByHook;
var getElement = exports.getElement = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2.default)(function* (base) {
    return (yield base.exists()) ? base : null;
  });
  return function getElement(_x) {
    return _ref.apply(this, arguments);
  };
}();

/**
 * Gets data attribute value
 * @param {UniDriver} base
 * @param {string} attr
 */
var getDataAttributeValue = exports.getDataAttributeValue = /*#__PURE__*/function () {
  var _ref2 = (0, _asyncToGenerator2.default)(function* (base, attr) {
    return base.attr(attr);
  });
  return function getDataAttributeValue(_x2, _x3) {
    return _ref2.apply(this, arguments);
  };
}();

/** Checks if given element is focused
 * @param {UniDriver} element
 * @returns {Promise<boolean>}
 */
var isElementFocused = exports.isElementFocused = /*#__PURE__*/function () {
  var _ref3 = (0, _asyncToGenerator2.default)(function* (element) {
    var nativeElement = yield element.getNative();
    switch (element.type) {
      case 'react':
        return (0, _ReactBase.ReactBase)(element).isFocus();
      case 'puppeteer':
        return page.evaluate(el => document.activeElement === el, nativeElement);
      default:
        return;
    }
  });
  return function isElementFocused(_x4) {
    return _ref3.apply(this, arguments);
  };
}();
var baseUniDriverFactory = base => {
  var baseUniDriver = (0, _unidriver.baseUniDriverFactory)(base);
  return {
    /**
     * Checks whether the component found with the given data hook
     * @returns {Promise<boolean>}
     */
    exists: () => baseUniDriver.exists(),
    /**
     * Gets the component root element
     * @returns {Promise<any>}
     */
    element: () => baseUniDriver.element(),
    /**
     * Clicks on the component root element
     * @returns {Promise<void>}
     */
    click: () => baseUniDriver.click(),
    /**
     * Returns UniDriver for the base element
     * @return {UniDriver}
     */
    get base() {
      return baseUniDriver.base;
    }
  };
};
exports.baseUniDriverFactory = baseUniDriverFactory;
//# sourceMappingURL=index.js.map