"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.datePickerUniDriverFactory = 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 _CalendarUniDriver = require("../Calendar/Calendar.uni.driver.js");
var _InputUni = require("../Input/Input.uni.driver");
var _constants = require("./constants");
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 datePickerUniDriverFactory = (base, body) => {
  /**
   * The input element is selected using [data-input-root] attribute instead of a dataHook
   * because the inputDataHook can be overridden by DatePicker prop, making it unreliable for testing
   */
  var inputDriver = (0, _InputUni.testkit)(base.$('[data-input-root]'), body);
  var popoverDriver = (0, _PopoverNextUni.popoverNextUniDriverFactory)((0, _unidriver.findByHook)(base, _constants.dataHooks.datePickerPopover), body);
  var driver = {
    exists: () => (0, _unidriver.baseUniDriverFactory)(base).exists(),
    open: function () {
      var _open = (0, _asyncToGenerator2.default)(function* () {
        yield inputDriver.click();
        yield inputDriver.trigger('keyDown', {
          key: 'ArrowUp',
          keyCode: 38
        });
      });
      function open() {
        return _open.apply(this, arguments);
      }
      return open;
    }(),
    getWidth: () => base._prop('style').then(style => style.width),
    clickOutside: () => popoverDriver.clickOutside(),
    focusCalendar: () => inputDriver.trigger('keyDown', {
      key: 'ArrowUp',
      keyCode: 38
    }),
    /**
     * Returns the element rendered inside the footer.
     */
    getFooter: () => base.$("[data-hook=\"".concat(_constants.dataHooks.datePickerFooter, "\"] > *"))
  };
  // TODO: needs to be fixed, autodocs doesn't generate the docs below.
  return {
    /**
     * Input Unidriver methods
     */
    inputDriver,
    /**
     * Calender Unidriver methods
     */
    calendarDriver: calendarDriverProxy(base, body),
    /**
     * Checks whether the component found with the given data hook.
     * @returns {Promise<boolean>}
     */
    exists: driver.exists,
    /**
     * Contains 'exists', 'open' and 'getWidth' methods
     */
    driver
  };
};

// Finds the correct calendar element no matter where the popover is rendered (window, viewport, parent, or scrollParent).
exports.datePickerUniDriverFactory = datePickerUniDriverFactory;
var calendarDriverProxy = (base, body) => _objectSpread({}, Object.keys((0, _CalendarUniDriver.calendarUniDriverFactory)(base)).reduce((testkit, method) => {
  return _objectSpread(_objectSpread({}, testkit), {}, {
    [method]: (0, _asyncToGenerator2.default)(function* () {
      var id = yield base.attr('data-date-picker-id');
      var calendarDriver = (0, _CalendarUniDriver.calendarUniDriverFactory)(body.$("[data-calendar-id=\"".concat(id, "\"]")));
      return calendarDriver[method](...arguments);
    })
  });
}, {}));
//# sourceMappingURL=DatePicker.uni.driver.js.map