"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _testUtils = _interopRequireDefault(require("react-dom/test-utils"));
var _DropdownBaseLegacy = _interopRequireDefault(require("../DropdownBase/DropdownBase.legacy.driver"));
var _constants = require("./constants");
var calendarDriverFactory = _ref => {
  var {
    element
  } = _ref;
  var getCalendarWrapper = () => element.querySelector('.DayPicker-wrapper');
  var getNthDay = n => element.querySelectorAll('[role="gridcell"]:not([class*="disabled"])>[data-outsideday="false"]')[n];
  var getNthDayOfTheMonth = n => element.querySelectorAll('[role="gridcell"]>[data-outsideday="false"]')[n];
  var getDayOfDate = (year, month, day) => element.querySelector("[role=\"gridcell\"]>[data-outsideday=\"false\"][data-date='".concat(year, "-").concat(month, "-").concat(day, "']"));
  var _getSelectedDay = () => element.querySelector('[role="gridcell"][aria-selected=true]>[data-outsideday="false"]');
  var getYearDropdown = () => element.querySelector('[data-hook="datepicker-year-dropdown-button"]');
  var getMonthDropdownButton = () => element.querySelector('[data-hook="datepicker-month-dropdown-button"]');
  var getNthYear = n => element.querySelector("[data-hook=\"dropdown-item-".concat(n, "\"]"));
  var _getMonthCaption = () => element.querySelector('[data-hook="datepicker-month-caption"]');
  var getYearCaption = () => element.querySelector('[data-hook="datepicker-year-caption"]');
  var getMonthAndYear = () => [_getMonthCaption(), getYearCaption()];
  var _getNthWeekDayName = n => element.querySelectorAll("[data-hook=\"weekday-day\"]")[n];
  var getPrevMonthButton = () => element.querySelector('[data-hook="datepicker-left-arrow"]');
  var getNextMonthButton = () => element.querySelector('[data-hook="datepicker-right-arrow"]');
  var _getFocusedDay = () => element.querySelector('.DayPicker-Day:focus');
  var getVisuallyUnfocusedDay = () => element.querySelector('.unfocused');
  var getMonthContainers = () => element.querySelectorAll('.DayPicker-Month');
  var getVisibleMonths = () => element.querySelectorAll('[class*="DayPicker-Month"]');
  var _getSelectedDays = () => element.querySelectorAll('[role="gridcell"][aria-selected=true]>[data-outsideday="false"]');
  var driver = {
    exists: () => !!element,
    close: () => _testUtils.default.Simulate.keyDown(_getFocusedDay(), {
      key: 'Escape',
      keyCode: 27
    }),
    getCurrentMonthWithYear: () => getMonthAndYear() ? getMonthAndYear().map(elm => elm.textContent).join(' ') : '',
    getNthWeekDayName: function getNthWeekDayName() {
      var n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
      return _getNthWeekDayName(n) ? _getNthWeekDayName(n).textContent : '';
    },
    clickOnNthDay: function clickOnNthDay() {
      var n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
      var day = getNthDay(n);
      return day && _testUtils.default.Simulate.click(day);
    },
    clickDay: date => {
      var day = getDayOfDate(date.getFullYear(), date.getMonth(), date.getDate());
      if (day) {
        _testUtils.default.Simulate.click(day);
      } else {
        throw new Error("ERROR: CalendarDriver.clickDay() - The given date (".concat(date.toString(), ") is not visible"));
      }
    },
    isDayActive: date => {
      var day = getDayOfDate(date.getFullYear(), date.getMonth(), date.getDate());
      if (day) {
        return day.parentElement.getAttribute('aria-disabled') === 'false';
      } else {
        throw new Error("ERROR: CalendarDriver.isDayActive() - The given date (".concat(date.toString(), ") is not visible"));
      }
    },
    clickOnNthDayOfTheMonth: function clickOnNthDayOfTheMonth() {
      var n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
      var day = getNthDayOfTheMonth(n);
      return day && _testUtils.default.Simulate.click(day);
    },
    clickOnSelectedDay: () => _testUtils.default.Simulate.click(_getSelectedDay()),
    clickOnYearDropdown: () => _testUtils.default.Simulate.click(getYearDropdown()),
    clickOnMonthDropdown: () => _testUtils.default.Simulate.click(getMonthDropdownButton()),
    clickOnNthYear: function clickOnNthYear() {
      var n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
      return _testUtils.default.Simulate.mouseDown(getNthYear(n));
    },
    clickOnPrevMonthButton: () => _testUtils.default.Simulate.click(getPrevMonthButton()),
    clickOnNextMonthButton: () => _testUtils.default.Simulate.click(getNextMonthButton()),
    keyboardNextYear: () => _testUtils.default.Simulate.keyDown(getCalendarWrapper(), {
      key: 'ArrowDown',
      keyCode: 40
    }),
    keyboardPrevYear: () => _testUtils.default.Simulate.keyDown(getCalendarWrapper(), {
      key: 'ArrowUp',
      keyCode: 38
    }),
    isHeaderExists: () => !!element.querySelector('[data-hook="datepicker-head"]'),
    isYearDropdownExists: () => !!element.querySelector('[data-hook="datepicker-year-dropdown"]'),
    isYearCaptionExists: () => !!getYearCaption(),
    isMonthDropdownExists: () => !!element.querySelector('[data-hook="datepicker-month-dropdown"]'),
    isMonthCaptionExists: () => !!_getMonthCaption(),
    getMonthCaption: () => _getMonthCaption().textContent,
    getMonthDropdownLabel: () => getMonthDropdownButton().textContent,
    getSelectedYear: () => getYearDropdown().textContent,
    /** Returns the text of the focused day or `null` if there is no focused day */
    getFocusedDay: () => {
      var focusedDayElement = _getFocusedDay();
      return !!focusedDayElement ? focusedDayElement.textContent : null;
    },
    getFocusedDayElement: () => _getFocusedDay(),
    pressLeftArrow: () => _testUtils.default.Simulate.keyDown(_getFocusedDay(), {
      key: 'ArrowLeft',
      keyCode: 37
    }),
    pressRightArrow: () => _testUtils.default.Simulate.keyDown(_getFocusedDay(), {
      key: 'ArrowRight',
      keyCode: 39
    }),
    getSelectedDay: () => _getSelectedDay().textContent,
    triggerKeyDown: params => _testUtils.default.Simulate.keyDown(_getFocusedDay(), params),
    isFocusedDayVisuallyUnfocused: () => _getFocusedDay().classList.contains('unfocused'),
    containsVisuallyUnfocusedDay: () => !!getVisuallyUnfocusedDay(),
    isTwoMonthsLayout: () => getMonthContainers().length === 2,
    getMonthDropdownDriver: () => {
      _testUtils.default.Simulate.click(element.querySelector('[data-hook="datepicker-month-dropdown-button"]'));
      var dropdownBaseDriver = (0, _DropdownBaseLegacy.default)({
        element: element.querySelector('[data-hook="datepicker-month-dropdown"]')
      });
      return dropdownBaseDriver.getDropdownLayoutDriver();
    },
    getYearDropdownDriver: () => {
      _testUtils.default.Simulate.click(element.querySelector('[data-hook="datepicker-year-dropdown-button"]'));
      var dropdownBaseDriver = (0, _DropdownBaseLegacy.default)({
        element: element.querySelector('[data-hook="datepicker-year-dropdown"]')
      });
      return dropdownBaseDriver.getDropdownLayoutDriver();
    },
    getNumOfVisibleMonths: () => getVisibleMonths().length,
    getNumOfSelectedDays: () => _getSelectedDays().length,
    getSelectedDays: () => {
      var result = [];
      _getSelectedDays().forEach(item => {
        var date = item.getAttribute('data-date').split('-').map(part => parseInt(part));
        result.push(new Date(date[0], date[1], date[2]));
      });
      return result;
    },
    getSize: () => element.querySelector("[data-hook=\"".concat(_constants.dataHooks.baseCalendar, "\"]")).getAttribute('data-size')
  };
  return driver;
};
var _default = exports.default = calendarDriverFactory;
//# sourceMappingURL=Calendar.driver.js.map