"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.nestableListDriverFactory = exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _NestableListBaseUni = require("../NestableListBase/NestableListBase.uni.driver");
var _constants = require("./constants");
var _TextButtonUni = require("../TextButton/TextButton.uni.driver");
var _unidriver = require("../utils/test-utils/unidriver");
var _constants2 = require("../NestableListBase/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 nestableListDriverFactory = (base, body) => {
  var nestableListBaseUniDriver = (0, _NestableListBaseUni.nestableListBaseUniDriverFactory)(base, body);
  var getActionsWrapper = /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2.default)(function* () {
      var dataHook = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'nestable-list-root-actions';
      var container = yield base.$$("[data-hook=\"".concat(dataHook, "\"]"));
      if ((yield container.count()) === 1) {
        return container.get(0);
      }
      return null;
    });
    return function getActionsWrapper() {
      return _ref.apply(this, arguments);
    };
  }();
  var getActions = /*#__PURE__*/function () {
    var _ref2 = (0, _asyncToGenerator2.default)(function* (parentDataHook) {
      var actionsContainer = yield getActionsWrapper(parentDataHook);
      if (actionsContainer) {
        yield hoverActionsContainer(parentDataHook);
        return actionsContainer.$$("[data-hook=\"nestable-list-action\"]");
      }
      return null;
    });
    return function getActions(_x) {
      return _ref2.apply(this, arguments);
    };
  }();
  var getActionTextButton = /*#__PURE__*/function () {
    var _ref3 = (0, _asyncToGenerator2.default)(function* (parentDataHook, index) {
      var actions = yield getActions(parentDataHook);
      if (actions) {
        var action = yield actions.get(index);
        if (yield action.exists()) {
          return (0, _TextButtonUni.textButtonDriverFactory)(action);
        }
      } else {
        throw new Error("Button doesn't exist at ".concat(JSON.stringify({
          parentDataHook,
          index
        })));
      }
    });
    return function getActionTextButton(_x2, _x3) {
      return _ref3.apply(this, arguments);
    };
  }();
  var hoverActionsContainer = /*#__PURE__*/function () {
    var _ref4 = (0, _asyncToGenerator2.default)(function* (dataHook) {
      var actionsContainer = yield getActionsWrapper(dataHook);
      if (actionsContainer) {
        yield actionsContainer.hover();
      }
    });
    return function hoverActionsContainer(_x4) {
      return _ref4.apply(this, arguments);
    };
  }();
  var dndProviderBase = (0, _unidriver.findByHook)(base, _constants2.dataHooks.provider);
  return _objectSpread(_objectSpread({}, (0, _unidriver.baseUniDriverFactory)(dndProviderBase, body)), {}, {
    /**
     * reorder nestable list items by data-hook
     * @param {ReorderDataHooks} dataHooks { from: string, to: string }
     * @return {Promise<void>}
     */

    reorder: function () {
      var _reorder = (0, _asyncToGenerator2.default)(function* (_ref5) {
        var {
          dataHookFrom,
          dataHookTo
        } = _ref5;
        return nestableListBaseUniDriver.reorder({
          dataHookFrom: "".concat(dataHookFrom, "-item"),
          dataHookTo: "".concat(dataHookTo, "-item")
        });
      });
      function reorder(_x5) {
        return _reorder.apply(this, arguments);
      }
      return reorder;
    }(),
    /**
     * get nestable list item position
     * @param itemDetails { dataHook: string }
     * @return {Promise<void>}
     */

    getItemPosition: function () {
      var _getItemPosition = (0, _asyncToGenerator2.default)(function* (_ref6) {
        var {
          dataHook
        } = _ref6;
        return nestableListBaseUniDriver.getItemPosition({
          dataHook: "".concat(dataHook, "-item")
        });
      });
      function getItemPosition(_x6) {
        return _getItemPosition.apply(this, arguments);
      }
      return getItemPosition;
    }(),
    /**
     * get nestable list item depth
     * @param itemDetails { dataHook: string }
     * @return {Promise<void>}
     */

    getItemDepth: function () {
      var _getItemDepth = (0, _asyncToGenerator2.default)(function* (_ref7) {
        var {
          dataHook
        } = _ref7;
        return nestableListBaseUniDriver.getItemDepth({
          dataHook: "".concat(dataHook, "-item")
        });
      });
      function getItemDepth(_x7) {
        return _getItemDepth.apply(this, arguments);
      }
      return getItemDepth;
    }(),
    /**
     *
     * @param dataHook - optional. data hook of parent item, if it's not provided means root of the three
     * @return {Promise<number>}
     */
    getActionsCount(dataHook) {
      return (0, _asyncToGenerator2.default)(function* () {
        var actions = yield getActions(dataHook);
        if (actions) {
          return actions.count();
        }
        return 0;
      })();
    },
    /**
     *
     * @param dataHook - optional. data hook of parent item, if it's not provided means root of the three
     * @return {Promise<void>}
     */
    hoverActionsContainer,
    getActionLabelAt: function () {
      var _getActionLabelAt = (0, _asyncToGenerator2.default)(function* (_ref8) {
        var {
          actionIndex,
          parentDataHook = 'nestable-list-root-actions'
        } = _ref8;
        var textButtonDriver = yield getActionTextButton(parentDataHook, actionIndex);
        return textButtonDriver.getButtonTextContent();
      });
      function getActionLabelAt(_x8) {
        return _getActionLabelAt.apply(this, arguments);
      }
      return getActionLabelAt;
    }(),
    clickActionAt: function () {
      var _clickActionAt = (0, _asyncToGenerator2.default)(function* (_ref9) {
        var {
          actionIndex,
          parentDataHook = 'nestable-list-root-actions'
        } = _ref9;
        var textButtonDriver = yield getActionTextButton(parentDataHook, actionIndex);
        return textButtonDriver.click();
      });
      function clickActionAt(_x9) {
        return _clickActionAt.apply(this, arguments);
      }
      return clickActionAt;
    }(),
    /**
     * change nestable list item depth
     * @param {NestableItemDetails} dataHooks { dataHook: string, depthLevel: number }
     * @return {Promise<void>}
     */

    changeItemDepth: _ref0 => {
      var {
        dataHook,
        depthLevel
      } = _ref0;
      return nestableListBaseUniDriver.changeItemDepth({
        dataHook: "".concat(dataHook, "-item"),
        depthLevel,
        threshold: _constants.depthThresholds.large
      });
    }
  });
};
exports.nestableListDriverFactory = nestableListDriverFactory;
var _default = exports.default = nestableListDriverFactory;
//# sourceMappingURL=NestableList.uni.driver.js.map