"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _Badge = _interopRequireDefault(require("../Badge/Badge.driver"));
var DATA_ATTR = _interopRequireWildcard(require("./DataAttr"));
var _DropdownLayout = _interopRequireDefault(require("../DropdownLayout/DropdownLayout.driver"));
var _Popover = require("../Popover/Popover.driver");
var _deprecationTestkitLog = _interopRequireDefault(require("../utils/deprecationTestkitLog"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var badgeSelectDriverFactory = _ref => {
  var {
    element,
    eventTrigger
  } = _ref;
  (0, _deprecationTestkitLog.default)('badgeSelectTestkitFactory legacy testkit is deprecated and will be removed in next major release. Please use the BadgeSelectTestkit unidriver testkit instead.');
  var popoverDriver = (0, _Popover.popoverLegacyDriverFactory)({
    element,
    eventTrigger
  });
  var badgeDriver = (0, _Badge.default)({
    element: element.querySelector("[data-hook=\"".concat(DATA_ATTR.DATA_BADGE, "\"]")),
    eventTrigger
  });
  return {
    /** Returns 'true' whether the element exists */
    exists: () => !!element,
    /** Gets badge text */
    text: badgeDriver.text,
    /** Click on an option */
    clickAtOption: index => {
      if (!popoverDriver.isContentElementExists()) {
        badgeDriver.click();
      }

      // Since the popover may be closed, we need to create the DropdownLayout driver every time
      var dropdownLayoutDriver = (0, _DropdownLayout.default)({
        element: popoverDriver.getContentElement().querySelector("[data-hook=\"".concat(DATA_ATTR.DATA_DROPDOWN, "\"]"))
      });
      if (dropdownLayoutDriver.exists()) {
        dropdownLayoutDriver.clickAtOption(index);
      }
    }
  };
};
var _default = exports.default = badgeSelectDriverFactory;
//# sourceMappingURL=BadgeSelect.driver.js.map