"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _Input = _interopRequireDefault(require("../Input/Input.driver"));
var _DropdownLayout = _interopRequireDefault(require("../DropdownLayout/DropdownLayout.driver"));
var _PopoverNext = require("../PopoverNext/PopoverNext.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 inputWithOptionsDriverFactory = _ref => {
  var {
    element: _element,
    eventTrigger
  } = _ref;
  var dropdownDataHook = 'inputwithoptions-dropdownlayout';
  var dropdownLayoutSelector = "[data-hook=\"".concat(dropdownDataHook, "\"]");
  var inputWrapperSelector = '[data-input-parent]';
  var popoverTestkit = () => (0, _PopoverNext.popoverNextLegacyDriverFactory)({
    element: _element
  });
  var _inputWrapper = () => _element && _element.querySelector("".concat(inputWrapperSelector, " > *:first-child "));
  var dropdownLayoutMain = () => document.body.querySelector(dropdownLayoutSelector);
  var inputDriver = (0, _Input.default)({
    element: _inputWrapper(),
    wrapper: _inputWrapper(),
    eventTrigger
  });
  var dropdownLayoutTestkit = () => (0, _DropdownLayout.default)({
    element: dropdownLayoutMain()
  });
  var driver = {
    exists: () => !!_element,
    element: () => _element,
    /** Select an option by id. (If dropdown options is not opened yet, this will open it and click on the option) */
    selectOptionById: id => {
      inputDriver.click();
      dropdownLayoutTestkit().optionById(id).click();
    },
    isReadOnly: () => inputDriver.getReadOnly() && _inputWrapper().className.includes('readOnly'),
    isEditable: () => !inputDriver.getReadOnly() && !inputDriver.getDisabled(),
    isDisabled: () => !!inputDriver.getDisabled(),
    inputWrapper: () => _inputWrapper(),
    focus: () => inputDriver.focus(),
    blur: () => dropdownLayoutTestkit().mouseClickOutside(),
    pressKey: key => inputDriver.keyDown(key),
    isMenuOpen: () => popoverTestkit().isContentElementExists(),
    outsideClick: () => popoverTestkit().clickOutside()
  };
  var dropdownLayoutDummy = (0, _DropdownLayout.default)({
    element: document.body,
    dataHook: dropdownDataHook
  });
  return {
    exists: () => driver.exists(),
    driver,
    inputDriver,
    dropdownLayoutDriver: Object.keys(dropdownLayoutDummy).reduce((prev, current) => {
      return _objectSpread(_objectSpread({}, prev), {}, {
        [current]: args => {
          if (current === 'isShown' || current === 'exists') {
            return popoverTestkit().isContentElementExists();
          }
          if (current === 'getDropdown' || current === 'getDropdownItem') {
            return popoverTestkit().isContentElementExists() ? dropdownLayoutTestkit()[current](args) : undefined;
          }
          !popoverTestkit().isContentElementExists() && inputDriver.click();
          return dropdownLayoutTestkit()[current](args);
        }
      });
    }, {})
  };
};
var _default = exports.default = inputWithOptionsDriverFactory;
//# sourceMappingURL=InputWithOptions.driver.js.map