"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.imageViewerUniDriverFactory = 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 _IconButtonUni = require("../IconButton/IconButton.uni.driver");
var _AddItemUni = require("../AddItem/AddItem.uni.driver");
var _TooltipUni = require("../Tooltip/Tooltip.uni.driver");
var _constants = require("./constants");
var _StatusIndicatorUni = require("../StatusIndicator/StatusIndicator.uni.driver");
var _PopoverMenuUni = require("../PopoverMenu/PopoverMenu.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 imageViewerUniDriverFactory = (base, body) => {
  var find = dataHook => base.$("[data-hook=\"".concat(dataHook, "\"]"));
  var hasDataAttribute = /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2.default)(function* (dataAttr, el) {
      return (yield el.attr(dataAttr)) === 'true';
    });
    return function hasDataAttribute(_x, _x2) {
      return _ref.apply(this, arguments);
    };
  }();
  var hoverElement = () => base.hover();
  var showButtons = hoverElement;
  var isImagesContainerElementVisible = () => hasDataAttribute(_constants.dataAttributes.containerVisible, find(_constants.dataHooks.imagesContainer));
  var isImageElementVisible = imgElement => hasDataAttribute(_constants.dataAttributes.imageVisible, imgElement);
  var addItemTestkit = (0, _AddItemUni.addItemUniDriverFactory)(find(_constants.dataHooks.addItem), body);
  var tooltipTestkit = dataHook => (0, _TooltipUni.tooltipDriverFactory)(find(dataHook), body);
  var iconButtonTestkit = dataHook => (0, _IconButtonUni.iconButtonDriverFactory)(find(dataHook));
  var popoverMenuDriver = dataHook => (0, _PopoverMenuUni.PopoverMenuDriver)(find(dataHook), body);
  var removeIconButton = iconButtonTestkit(_constants.dataHooks.remove);
  var downloadIconButton = iconButtonTestkit(_constants.dataHooks.download);
  var updateIconButton = iconButtonTestkit(_constants.dataHooks.update);
  var moreIconButton = iconButtonTestkit(_constants.dataHooks.more);
  var updateTooltip = tooltipTestkit(_constants.dataHooks.updateTooltip);
  var downloadTooltip = tooltipTestkit(_constants.dataHooks.downloadTooltip);
  var removeTooltip = tooltipTestkit(_constants.dataHooks.removeTooltip);
  var moreTooltip = tooltipTestkit(_constants.dataHooks.moreTooltip);
  var getStatusIndicatorDriver = () => (0, _StatusIndicatorUni.statusIndicatorDriverFactory)(base.$("[data-hook=\"".concat(_constants.dataHooks.errorTooltip, "\"]")), body);
  var moreButtonExists = () => moreIconButton.exists();
  var clickMore = () => showButtons().then(() => moreIconButton.click());
  return _objectSpread(_objectSpread({}, (0, _unidriver.baseUniDriverFactory)(base)), {}, {
    updateExists: () => !!find(_constants.dataHooks.update),
    updateButtonExists: () => updateIconButton.exists(),
    removeButtonExists: () => removeIconButton.exists(),
    downloadButtonExists: () => downloadIconButton.exists(),
    moreButtonExists,
    clickAdd: () => addItemTestkit.click(),
    clickUpdate: () => showButtons().then(() => updateIconButton.click()),
    clickDownload: function () {
      var _clickDownload = (0, _asyncToGenerator2.default)(function* () {
        yield showButtons();
        if (yield moreButtonExists()) {
          yield clickMore();
          var moreActions = popoverMenuDriver(_constants.dataHooks.actionsMenu);
          return moreActions.clickAtChild(0);
        } else {
          return downloadIconButton.click();
        }
      });
      function clickDownload() {
        return _clickDownload.apply(this, arguments);
      }
      return clickDownload;
    }(),
    clickRemove: function () {
      var _clickRemove = (0, _asyncToGenerator2.default)(function* () {
        yield showButtons();
        if (yield moreButtonExists()) {
          yield clickMore();
          var moreActions = popoverMenuDriver(_constants.dataHooks.actionsMenu);
          return moreActions.clickAtChild(1);
        } else {
          return removeIconButton.click();
        }
      });
      function clickRemove() {
        return _clickRemove.apply(this, arguments);
      }
      return clickRemove;
    }(),
    getContainerStyles: () => base.attr('style'),
    getAddTooltipContent: () => addItemTestkit.getTooltipContent(),
    getUpdateTooltipContent: () => updateTooltip.getTooltipText(),
    getDownloadTooltipContent: () => downloadTooltip.getTooltipText(),
    getRemoveTooltipContent: () => removeTooltip.getTooltipText(),
    getMoreTooltipContent: () => moreTooltip.getTooltipText(),
    isDisabled: () => base.attr(_constants.dataAttributes.disabled).then(x => x === 'true'),
    isAddItemVisible: () => find(_constants.dataHooks.addItem).exists(),
    isLoaderVisible: () => find(_constants.dataHooks.loader).exists(),
    isImageLoaded: () => base.attr('data-image-loaded').then(value => value === 'true'),
    isImageVisible: function () {
      var _isImageVisible = (0, _asyncToGenerator2.default)(function* () {
        var image = find(_constants.dataHooks.image);
        return (yield image.exists()) && !!(yield image.attr('src')) && (yield isImageElementVisible(image)) && (yield isImagesContainerElementVisible());
      });
      function isImageVisible() {
        return _isImageVisible.apply(this, arguments);
      }
      return isImageVisible;
    }(),
    isPreviousImageVisible: function () {
      var _isPreviousImageVisible = (0, _asyncToGenerator2.default)(function* () {
        var previousImage = find(_constants.dataHooks.previousImage);
        return (yield previousImage.exists()) && !!(yield previousImage.attr('src')) && (yield isImageElementVisible(previousImage)) && (yield isImagesContainerElementVisible());
      });
      function isPreviousImageVisible() {
        return _isPreviousImageVisible.apply(this, arguments);
      }
      return isPreviousImageVisible;
    }(),
    getImageUrl: function () {
      var _getImageUrl = (0, _asyncToGenerator2.default)(function* () {
        var image = find(_constants.dataHooks.image);
        return (yield image.exists()) && (yield image.attr('src'));
      });
      function getImageUrl() {
        return _getImageUrl.apply(this, arguments);
      }
      return getImageUrl;
    }(),
    getPreviousImageUrl: function () {
      var _getPreviousImageUrl = (0, _asyncToGenerator2.default)(function* () {
        var previousImage = find(_constants.dataHooks.previousImage);
        return (yield previousImage.exists()) && (yield previousImage.attr('src'));
      });
      function getPreviousImageUrl() {
        return _getPreviousImageUrl.apply(this, arguments);
      }
      return getPreviousImageUrl;
    }(),
    hover: hoverElement,
    // Status
    /** Return true if there's a status */
    hasStatus: function () {
      var _hasStatus = (0, _asyncToGenerator2.default)(function* (status) {
        var statusIndicatorDriver = getStatusIndicatorDriver();
        if (yield statusIndicatorDriver.exists()) {
          return status === (yield statusIndicatorDriver.getStatus());
        }
        return false;
      });
      function hasStatus(_x3) {
        return _hasStatus.apply(this, arguments);
      }
      return hasStatus;
    }(),
    /** If there's a status message, returns its text value */
    getStatusMessage: function () {
      var _getStatusMessage = (0, _asyncToGenerator2.default)(function* () {
        var statusIndicatorDriver = getStatusIndicatorDriver();
        var tooltipText = null;
        if (yield statusIndicatorDriver.hasMessage()) {
          tooltipText = yield statusIndicatorDriver.getMessage();
        }
        return tooltipText;
      });
      function getStatusMessage() {
        return _getStatusMessage.apply(this, arguments);
      }
      return getStatusMessage;
    }()
  });
};
exports.imageViewerUniDriverFactory = imageViewerUniDriverFactory;
//# sourceMappingURL=ImageViewer.uni.driver.js.map