"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.editableSelectorUniDriverFactory = 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 _SelectorUni = require("../Selector/Selector.uni.driver");
var _EditableRowUni = require("./EditableRow/EditableRow.uni.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; }
var editableSelectorUniDriverFactory = (base, body) => {
  var newRowButton = () => (0, _unidriver.findByHook)(base, _constants.dataHooks.newRowButtonText);
  var selectorRowDriver = /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2.default)(function* (index) {
      return (0, _SelectorUni.selectorUniDriverFactory)(yield (0, _unidriver.findByHookAtIndex)(base, _constants.dataHooks.item, index), body);
    });
    return function selectorRowDriver(_x) {
      return _ref.apply(this, arguments);
    };
  }();
  var editButtonAt = index => (0, _unidriver.findByHookAtIndex)(base, _constants.dataHooks.editItem, index);
  var deleteButtonAt = index => (0, _unidriver.findByHookAtIndex)(base, _constants.dataHooks.deleteItem, index);
  var editableRowDriver = () => (0, _EditableRowUni.editableRowUniDriverFactory)((0, _unidriver.findByHook)(base, _constants.dataHooks.editRowWrapper), body);
  var isEditRowActive = /*#__PURE__*/function () {
    var _ref2 = (0, _asyncToGenerator2.default)(function* () {
      return !!(yield (0, _unidriver.countByHook)(base, _constants.dataHooks.editRowWrapper));
    });
    return function isEditRowActive() {
      return _ref2.apply(this, arguments);
    };
  }();
  return _objectSpread(_objectSpread({}, (0, _unidriver.baseUniDriverFactory)(base)), {}, {
    /**
     * Gets a list of selector drivers
     * @returns {Promise<SelectorUniDriver[]>}
     */
    items: () => {
      return base.$$("[data-hook=\"".concat(_constants.dataHooks.item, "\"]")).map(selector => (0, _SelectorUni.selectorUniDriverFactory)(selector, body));
    },
    /**
     * Checks whether editing mode is active
     * @returns {Promise<boolean>}
     */
    isEditing: () => isEditRowActive(),
    /**
     * Checks whether any row is being edited
     * @returns {Promise<boolean>}
     */
    isEditingRow: function () {
      var _isEditingRow = (0, _asyncToGenerator2.default)(function* () {
        return !!((yield isEditRowActive()) && (yield editableRowDriver().getText()));
      });
      function isEditingRow() {
        return _isEditingRow.apply(this, arguments);
      }
      return isEditingRow;
    }(),
    /**
     * Checks whether any row is being added
     * @returns {Promise<boolean>}
     */
    isAddingRow: function () {
      var _isAddingRow = (0, _asyncToGenerator2.default)(function* () {
        return !!((yield isEditRowActive()) && !(yield editableRowDriver().getText()));
      });
      function isAddingRow() {
        return _isAddingRow.apply(this, arguments);
      }
      return isAddingRow;
    }(),
    /**
     * Gets the new row button wrapper
     * @returns {Promise<HTMLElement>}
     */
    newRowButton,
    /**
     * Gets the delete row button wrapper at index
     * @param {number} index Item index
     * @returns {Promise<HTMLElement>}
     */
    deleteButtonAt,
    /**
     * Gets the edit row button wrapper at index
     * @param {number} index Item index
     * @returns {Promise<HTMLElement>}
     */
    editButtonAt,
    /**
     * Adds new row and sets input value
     * @param {string} label Text to input
     * @returns {Promise<void>}
     */
    addNewRow: function () {
      var _addNewRow = (0, _asyncToGenerator2.default)(function* (label) {
        yield newRowButton().click();
        return editableRowDriver().setText(label);
      });
      function addNewRow(_x2) {
        return _addNewRow.apply(this, arguments);
      }
      return addNewRow;
    }(),
    /**
     * Edits row and sets input value
     * @param {number} index Item index
     * @param {string} label Text to input
     * @returns {Promise<void>}
     */
    editRow: function () {
      var _editRow = (0, _asyncToGenerator2.default)(function* (index, label) {
        var editButton = yield editButtonAt(index);
        yield editButton.hover();
        yield editButton.click();
        return editableRowDriver().setText(label);
      });
      function editRow(_x3, _x4) {
        return _editRow.apply(this, arguments);
      }
      return editRow;
    }(),
    /**
     * Clicks delete button at index
     * @param {number} index Item index
     * @returns {Promise<void>}
     */
    deleteRow: function () {
      var _deleteRow = (0, _asyncToGenerator2.default)(function* (index) {
        var deleteButton = yield deleteButtonAt(index);
        yield deleteButton.hover();
        return deleteButton.click();
      });
      function deleteRow(_x5) {
        return _deleteRow.apply(this, arguments);
      }
      return deleteRow;
    }(),
    /**
     * Clicks add new row button
     * @returns {Promise<void>}
     */
    startAdding: () => newRowButton().click(),
    /**
     * Clicks edit row button at index
     * @param {number} index Item index
     * @returns {Promise<void>}
     */
    startEditing: function () {
      var _startEditing = (0, _asyncToGenerator2.default)(function* (index) {
        return (yield editButtonAt(index)).click();
      });
      function startEditing(_x6) {
        return _startEditing.apply(this, arguments);
      }
      return startEditing;
    }(),
    /**
     * Clicks approve button
     * @returns {Promise<void>}
     */
    clickApprove: () => editableRowDriver().clickApprove(),
    /**
     * Clicks cancel button
     * @returns {Promise<void>}
     */
    clickCancel: () => editableRowDriver().clickCancel(),
    /**
     * Gets the title
     * @returns {Promise<string>}
     */
    title: () => base.$("[data-hook=\"".concat(_constants.dataHooks.title, "\"] > span")).text(),
    /**
     * Toggles selector of the item at index
     * @param {number} index Item index
     * @returns {Promise<void>}
     */
    toggleItem: function () {
      var _toggleItem = (0, _asyncToGenerator2.default)(function* (index) {
        return (yield selectorRowDriver(index)).toggle();
      });
      function toggleItem(_x7) {
        return _toggleItem.apply(this, arguments);
      }
      return toggleItem;
    }()
  });
};
exports.editableSelectorUniDriverFactory = editableSelectorUniDriverFactory;
//# sourceMappingURL=EditableSelector.uni.driver.js.map