"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 _Tooltip = require("../Tooltip/Tooltip.driver");
var _Input = require("./Input.constants");
var inputDriverFactory = _ref => {
  var {
    element,
    eventTrigger
  } = _ref;
  var input = element && element.querySelector('input');
  var clearButton = element && element.querySelector("[data-hook=input-clear-button]");
  var customAffixNode = element && element.querySelector("[data-hook=\"custom-affix\"]");
  var iconAffixNode = element && element.querySelector("[data-hook=\"icon-affix\"]");
  var menuArrowNode = element && element.querySelector("[data-hook=\"".concat(_Input.dataHooks.menuArrow, "\"]"));
  var getName = () => input.getAttribute('name');
  var getType = () => input.getAttribute('type');
  var getMaxLength = () => input.getAttribute('maxlength');
  var getTooltipDriver = dataHook => (0, _Tooltip.tooltipLegacyDriverFactory)({
    element: document.body.querySelector("[data-hook=\"".concat(dataHook, "\"]")),
    eventTrigger
  });
  var driver = {
    trigger: (_trigger, event) => _testUtils.default.Simulate[_trigger](input, event),
    focus: options => {
      input.focus(options);
      _testUtils.default.Simulate.focus(input);
    },
    blur: () => {
      input.blur();
      _testUtils.default.Simulate.blur(input);
    },
    getName,
    getMaxLength,
    getType,
    keyDown: key => _testUtils.default.Simulate.keyDown(input, {
      key
    }),
    click: () => _testUtils.default.Simulate.click(input),
    clickCustomAffix: () => _testUtils.default.Simulate.click(customAffixNode),
    clickClear: () => _testUtils.default.Simulate.click(clearButton),
    clickIconAffix: () => _testUtils.default.Simulate.click(iconAffixNode),
    clickMenuArrow: () => _testUtils.default.Simulate.click(menuArrowNode),
    mouseOver: () => _testUtils.default.Simulate.mouseOver(input),
    mouseOut: () => _testUtils.default.Simulate.mouseOut(input),
    clearText: () => driver.enterText(''),
    enterText: text => {
      if (driver.getReadOnly() || driver.isDisabled()) {
        return;
      }
      input.value = text;
      _testUtils.default.Simulate.change(input, {
        target: {
          name: getName(),
          type: getType(),
          value: text
        }
      });
    },
    getValue: () => input.value,
    getText: () => input.value,
    getPlaceholder: () => input.placeholder,
    getPattern: () => input.pattern,
    getDefaultValue: () => input.defaultValue,
    getTabIndex: () => input.tabIndex,
    getReadOnly: () => input.readOnly,
    getDisabled: () => input.disabled,
    getTextOverflow: () => input.style['text-overflow'],
    getAriaLabel: () => input.getAttribute('aria-label'),
    getAriaControls: () => input.getAttribute('aria-controls'),
    getAriaDescribedby: () => input.getAttribute('aria-describedby'),
    getAutocomplete: () => input.getAttribute('autocomplete'),
    getRequired: () => input.required,
    hasPrefix: () => element.hasAttribute(_Input.DATA_ATTR.PREFIX),
    hasSuffix: () => !!element.querySelector("[data-hook=\"".concat(_Input.dataHooks.suffixes, "\"]")),
    getClearButtonTooltipContent: () => getTooltipDriver(_Input.dataHooks.tooltip).getTooltipText(),
    prefixComponentExists: style => element.hasAttribute(_Input.DATA_ATTR.PREFIX) && !!element.querySelector(style),
    suffixComponentExists: style => !!element.querySelector("[data-hook=\"".concat(_Input.dataHooks.suffixes, "\"] ").concat(style)),
    getDataHook: () => element.getAttribute('data-hook'),
    getCustomAffix: () => customAffixNode.textContent,
    hasMenuArrow: () => !!menuArrowNode,
    hasClearButton: () => !!clearButton,
    isRTL: () => element.getAttribute('dir') === 'rtl',
    isFocusedStyle: () => element.hasAttribute(_Input.DATA_ATTR.FOCUS),
    isHoveredStyle: () => element.hasAttribute(_Input.DATA_ATTR.HOVER),
    isDisabled: () => element.getAttribute(_Input.DATA_ATTR.DISABLED) === 'true',
    isOfSize: size => element.getAttribute(_Input.DATA_ATTR.SIZE) === size,
    getSize: () => element.getAttribute(_Input.DATA_ATTR.SIZE),
    isFocus: () => document.activeElement === input,
    exists: () => !!(element && element.querySelector('input')),
    startComposing: () => _testUtils.default.Simulate.compositionStart(input),
    endComposing: () => _testUtils.default.Simulate.compositionEnd(input),
    getCursorLocation: () => input.selectionStart,
    getRootElementClasses: () => element.classList,
    getInputElementClasses: () => input.classList,
    getInputAttribute: name => input.getAttribute(name),
    hasRightBorderRadius: () => !element.hasAttribute(_Input.DATA_ATTR.RIGHTBORDERRADIUS),
    hasLeftBorderRadius: () => !element.hasAttribute(_Input.DATA_ATTR.LEFTBORDERRADIUS),
    isCustomInput: () => {
      return input.getAttribute('data-hook') === 'wsr-custom-input';
    },
    isMasked: () => input.getAttribute('data-mask') === 'true',
    // Status
    /** Return true if the given status is displayed */
    hasStatus: status => element.getAttribute(_Input.DATA_ATTR.STATUS) === status,
    /** If there's a status message, returns its text value */
    getStatusMessage: () => getTooltipDriver(_Input.dataHooks.statusIndicatorTooltip).getTooltipText()
  };
  return driver;
};
var _default = exports.default = inputDriverFactory;
//# sourceMappingURL=Input.driver.js.map