"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.dropdownBaseDriverFactory = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _unidriver = require("../utils/test-utils/unidriver");
var _DropdownLayoutUni = require("../DropdownLayout/DropdownLayout.uni.driver");
var _PopoverNextUni = require("../PopoverNext/PopoverNext.uni.driver");
var _DataAttr = require("../DropdownLayout/DataAttr");
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 dropdownBaseDriverFactory = (base, body) => {
  var byDataHook = dataHook => base.$("[data-hook=\"".concat(dataHook, "\"]"));
  var getTargetElement = dataHook => byDataHook(dataHook);
  var getContentElement = /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2.default)(function* () {
      return (0, _PopoverNextUni.popoverNextUniDriverFactory)(base, body).getContentElementUniDriver();
    });
    return function getContentElement() {
      return _ref.apply(this, arguments);
    };
  }();
  var createDropdownLayoutDriver = /*#__PURE__*/function () {
    var _ref2 = (0, _asyncToGenerator2.default)(function* () {
      var contentElement = yield getContentElement();
      if (!contentElement) {
        throw new Error('Content element not found');
      }
      return (0, _DropdownLayoutUni.dropdownLayoutDriverFactory)(contentElement.$("[data-hook=\"dropdown-base-dropdownlayout\"]"), body);
    });
    return function createDropdownLayoutDriver() {
      return _ref2.apply(this, arguments);
    };
  }();
  return _objectSpread(_objectSpread({}, (0, _unidriver.baseUniDriverFactory)(base)), {}, {
    /** Returns the list type prop value */
    getListType: function () {
      var _getListType = (0, _asyncToGenerator2.default)(function* (dataHook) {
        var dropdownBaseRootElement = yield body.$("[data-hook=\"".concat(dataHook, "\"]"));
        return dropdownBaseRootElement.attr('data-list-type');
      });
      function getListType(_x) {
        return _getListType.apply(this, arguments);
      }
      return getListType;
    }(),
    /** Returns the target element */
    clickTargetElement: dataHook => getTargetElement(dataHook).click(),
    /** Hover the target element */
    hoverTargetElement: function () {
      var _hoverTargetElement = (0, _asyncToGenerator2.default)(function* (dataHook) {
        return yield getTargetElement(dataHook).mouse.hover();
      });
      function hoverTargetElement(_x2) {
        return _hoverTargetElement.apply(this, arguments);
      }
      return hoverTargetElement;
    }(),
    /** Returns `true` if the dropdown is being shown */
    isDropdownShown: () => (0, _PopoverNextUni.popoverNextUniDriverFactory)(base, body).isContentElementExists(),
    /** Select a specific option (requires the DropdownBase to be opened) */
    selectOption: function () {
      var _selectOption = (0, _asyncToGenerator2.default)(function* (index) {
        return (yield createDropdownLayoutDriver()).clickAtOption(index);
      });
      function selectOption(_x3) {
        return _selectOption.apply(this, arguments);
      }
      return selectOption;
    }(),
    /** Select a specific option by its data hook (requires the DropdownBase to be opened) */
    selectOptionByDataHook: function () {
      var _selectOptionByDataHook = (0, _asyncToGenerator2.default)(function* (dataHook) {
        return (yield createDropdownLayoutDriver()).clickAtOptionByDataHook(dataHook);
      });
      function selectOptionByDataHook(_x4) {
        return _selectOptionByDataHook.apply(this, arguments);
      }
      return selectOptionByDataHook;
    }(),
    /** Click outside of the component */
    clickOutside: () => (0, _PopoverNextUni.popoverNextUniDriverFactory)(base, body).clickOutside(),
    /** Options count (requires the DropdownBase to be opened) */
    optionsCount: function () {
      var _optionsCount = (0, _asyncToGenerator2.default)(function* () {
        return (yield createDropdownLayoutDriver()).optionsLength();
      });
      function optionsCount() {
        return _optionsCount.apply(this, arguments);
      }
      return optionsCount;
    }(),
    optionContentAt: function () {
      var _optionContentAt = (0, _asyncToGenerator2.default)(function* (position) {
        var dropdownLayoutDriver = yield createDropdownLayoutDriver();
        var optionsDrivers = yield dropdownLayoutDriver.options();
        var optionElement = yield optionsDrivers[position].element();
        var option = yield (0, _unidriver.findByHook)(optionElement, _DataAttr.DATA_HOOKS.OPTION);

        /*
           Option content can be
           1. node - <div>some text</div>
           2. text - some text
        */
        var nodeContent = option.$$(':first-child');
        var contentIsNode = (yield nodeContent.count()) > 0;
        if (contentIsNode) {
          return yield nodeContent.get(0).getNative();
        } else {
          return option.text();
        }
      });
      function optionContentAt(_x5) {
        return _optionContentAt.apply(this, arguments);
      }
      return optionContentAt;
    }(),
    /** Returns the selected option (requires the DropdownBase to be opened)*/
    getSelectedOptionId: function () {
      var _getSelectedOptionId = (0, _asyncToGenerator2.default)(function* () {
        return (yield createDropdownLayoutDriver()).getSelectedOptionId();
      });
      function getSelectedOptionId() {
        return _getSelectedOptionId.apply(this, arguments);
      }
      return getSelectedOptionId;
    }(),
    /** Returns the marked option (requires the DropdownBase to be opened)*/
    getMarkedOption: function () {
      var _getMarkedOption = (0, _asyncToGenerator2.default)(function* () {
        return (yield createDropdownLayoutDriver()).markedOption();
      });
      function getMarkedOption() {
        return _getMarkedOption.apply(this, arguments);
      }
      return getMarkedOption;
    }(),
    mouseEnter: () => (0, _PopoverNextUni.popoverNextUniDriverFactory)(base, body).mouseEnter(),
    mouseLeave: () => (0, _PopoverNextUni.popoverNextUniDriverFactory)(base, body).mouseLeave()
  });
};
exports.dropdownBaseDriverFactory = dropdownBaseDriverFactory;
//# sourceMappingURL=DropdownBase.uni.driver.js.map