"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.inputWithOptionsUniDriverFactory = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _InputUni = require("../Input/Input.uni.driver");
var _DropdownLayoutUni = require("../DropdownLayout/DropdownLayout.uni.driver");
var _unidriver = require("../utils/test-utils/unidriver");
var _InputWithOptionsProxy = require("./InputWithOptions.proxy.driver");
var _PopoverNextUni = require("../PopoverNext/PopoverNext.uni.driver");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var inputWithOptionsUniDriverFactory = (base, body) => {
  var inputWrapperSelector = '[data-input-parent]';
  var inputWrapper = base.$(inputWrapperSelector);
  var inputDriver = (0, _InputUni.testkit)(base.$("".concat(inputWrapperSelector, " > *:first-child ")), body);
  var popoverTestkit = () => (0, _PopoverNextUni.popoverNextUniDriverFactory)(base, body);
  var dropdownLayoutFromBase = () => (0, _DropdownLayoutUni.dropdownLayoutDriverFactory)(base, body);
  var dropdownLayoutTestkit = /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2.default)(function* () {
      var content = yield popoverTestkit().getContentElementUniDriver();
      if (!content) {
        return undefined;
      }
      return (0, _DropdownLayoutUni.dropdownLayoutDriverFactory)(content.$("[data-hook=\"inputwithoptions-dropdownlayout\"]"), body);
    });
    return function dropdownLayoutTestkit() {
      return _ref.apply(this, arguments);
    };
  }();
  var dropdownLayoutDriver = (0, _InputWithOptionsProxy.dropdownLayoutDriverProxy)(dropdownLayoutFromBase, dropdownLayoutTestkit, popoverTestkit, inputDriver);
  var driver = _objectSpread(_objectSpread({}, base), {}, {
    /** Select an option by id. (If dropdown options is not opened yet, this will open it and click on the option) */
    selectOptionById: function () {
      var _selectOptionById = (0, _asyncToGenerator2.default)(function* (id) {
        var _yield$yield$dropdown, _yield$dropdownLayout;
        var nativeSelect = base.$('[data-hook=native-select]');
        if (yield nativeSelect.exists()) {
          var dataHookPrefix = "native-option-".concat(id);
          var option = base.$("[data-hook*=".concat(dataHookPrefix, "]"));
          var selectedIndex = yield option.attr('data-index');
          return yield (0, _unidriver.ReactBase)(nativeSelect).select(selectedIndex);
        }
        var isPopoverShown = yield popoverTestkit().isContentElementExists();
        if (!isPopoverShown) {
          yield inputDriver.click();
        }
        yield (_yield$yield$dropdown = yield (_yield$dropdownLayout = yield dropdownLayoutTestkit()) == null ? void 0 : _yield$dropdownLayout.optionById(id)) == null ? void 0 : _yield$yield$dropdown.click();
      });
      function selectOptionById(_x) {
        return _selectOptionById.apply(this, arguments);
      }
      return selectOptionById;
    }(),
    isReadOnly: inputDriver.getReadOnly,
    isEditable: function () {
      var _isEditable = (0, _asyncToGenerator2.default)(function* () {
        return !(yield inputDriver.getReadOnly()) && !(yield inputDriver.getDisabled());
      });
      function isEditable() {
        return _isEditable.apply(this, arguments);
      }
      return isEditable;
    }(),
    isDisabled: inputDriver.getDisabled,
    /** @deprecated  Should be private */
    inputWrapper: inputWrapper.getNative,
    focus: inputDriver.focus,
    blur: function () {
      var _blur = (0, _asyncToGenerator2.default)(function* () {
        var _yield$dropdownLayout2;
        return (_yield$dropdownLayout2 = yield dropdownLayoutTestkit()) == null ? void 0 : _yield$dropdownLayout2.mouseClickOutside();
      });
      function blur() {
        return _blur.apply(this, arguments);
      }
      return blur;
    }(),
    pressKey: function () {
      var _pressKey = (0, _asyncToGenerator2.default)(function* (key) {
        return inputDriver.keyDown({
          key
        });
      });
      function pressKey(_x2) {
        return _pressKey.apply(this, arguments);
      }
      return pressKey;
    }(),
    outsideClick: popoverTestkit().clickOutside,
    isFocus: function () {
      var _isFocus = (0, _asyncToGenerator2.default)(function* () {
        return !!(yield inputDriver.isFocus());
      });
      function isFocus() {
        return _isFocus.apply(this, arguments);
      }
      return isFocus;
    }()
  });
  return {
    exists: driver.exists,
    driver,
    inputDriver,
    dropdownLayoutDriver
  };
};
exports.inputWithOptionsUniDriverFactory = inputWithOptionsUniDriverFactory;
//# sourceMappingURL=InputWithOptions.uni.driver.js.map