"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 _Calendar = _interopRequireDefault(require("../Calendar/Calendar.driver"));
var _constants = require("./constants");
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; }
//FIXME: use PopoverNext legacy driver
/** @deprecated use datePickerUniDriverFactory instead  */
var datePickerDriverFactory = _ref => {
  var {
    element,
    eventTrigger
  } = _ref;
  var popoverElement = element.querySelector("[data-hook=\"".concat(_constants.dataHooks.datePickerPopover, "\"]"));
  var popoverTrigger = () => {
    return element.querySelector("[data-hook=\"".concat(_constants.dataHooks.datePickerPopover, "-trigger\"]"));
  };
  var popoverContent = () => document.body.querySelector("[data-hook=\"".concat(_constants.dataHooks.datePickerPopover, "-content\"]"));
  var clickOutside = () => {
    document.dispatchEvent(new Event('mousedown'));
    document.dispatchEvent(new Event('mouseup'));
  };
  var calendarTestkit = () => (0, _Calendar.default)({
    element: popoverContent()
  });
  var calendarDriver = _objectSpread({}, Object.keys((0, _Calendar.default)({
    element
  })).reduce((prev, current) => _objectSpread(_objectSpread({}, prev), {}, {
    [current]: args => {
      if (current === 'isVisible') {
        return !!popoverContent();
      }
      if (current === 'mouseClickOutside') {
        return clickOutside();
      }
      return calendarTestkit()[current](args);
    }
  }), {}));

  // here we have a dirty hack for getting input element directly
  // this is not the best way to do it, but knowing that we exposed
  // input datahook - we cannot change this for now.
  var inputElement = popoverTrigger().querySelector('[data-size]');
  var inputDriver = (0, _Input.default)({
    element: inputElement,
    eventTrigger
  });
  var driver = {
    exists: () => !!element,
    open: () => {
      inputDriver.click();
      inputDriver.trigger('keyDown', {
        key: 'ArrowUp',
        keyCode: 38
      });
    },
    getWidth: () => element.style.width,
    clickOutside,
    getFooter: () => element.$("[data-hook=\"".concat(_constants.dataHooks.datePickerFooter, "\"] > *"))
  };
  return {
    exists: driver.exists,
    driver,
    inputDriver,
    calendarDriver
  };
};
var _default = exports.default = datePickerDriverFactory;
//# sourceMappingURL=DatePicker.driver.js.map