"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = useTooltip;

var _react = require("react");

var _reactDom = require("@floating-ui/react-dom");

var _UCLProvider = require("../uclprovider/UCLProvider");

var _selectn = _interopRequireDefault(require("selectn"));

var _useEscapeEffect = _interopRequireDefault(require("../../custom-hooks/useEscapeEffect"));

var _debounce = _interopRequireDefault(require("../../utils/debounce"));

var _reducerHandler = _interopRequireDefault(require("../../libraries/reducerHandler"));

var _Tooltip = _interopRequireWildcard(require("./Tooltip.reducers"));

var _Tooltip2 = require("./Tooltip.actions");

var _utils = require("./utils");

var _Tooltip3 = require("./Tooltip.constants");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function useTooltip(_ref) {
  var container = _ref.container,
      reducer = _ref.reducer,
      offsetOption = _ref.offsetOption,
      tooltipPlacement = _ref.placement;

  var _useCommonReducer = (0, _reducerHandler["default"])(_Tooltip["default"], reducer, _Tooltip.initialState),
      state = _useCommonReducer.state,
      dispatch = _useCommonReducer.dispatch;

  var arrowReference = (0, _react.useRef)(null);

  var _useFloating = (0, _reactDom.useFloating)({
    placement: tooltipPlacement,
    middleware: [(0, _reactDom.offset)(offsetOption), (0, _reactDom.flip)(), (0, _reactDom.shift)(), (0, _reactDom.arrow)({
      element: arrowReference
    })]
  }),
      x = _useFloating.x,
      y = _useFloating.y,
      reference = _useFloating.reference,
      floating = _useFloating.floating,
      position = _useFloating.strategy,
      middlewareData = _useFloating.middlewareData,
      placement = _useFloating.placement;

  var title = state.title,
      isHtmlTitle = state.isHtmlTitle;
  (0, _useEscapeEffect["default"])(function () {
    handleReset();
  }, [title]);
  var componentThemeProps = (0, _UCLProvider.useThemeByComponent)(_Tooltip3.COMPONENT_NAME);

  function handleReset() {
    if (title) {
      dispatch((0, _Tooltip2.reset)());
    }
  }

  function mouseHover(e) {
    //For handling nested tooltip avoid call multiple times
    if (e.isTooltipHanlde) {
      handleReset();
      return null;
    }

    var element = e.target;
    var titleElement = (0, _utils.getClosestTitleElement)(element);

    if (titleElement) {
      var _title = titleElement.getAttribute(_Tooltip3.DATA_TITLE_ATTR) || titleElement.getAttribute(_Tooltip3.TITLE_ATTR);

      if (_title != "") {
        titleElement.setAttribute(_Tooltip3.DATA_TITLE_ATTR, _title);
        /* reason for removing title attribute avoiding default browser tooltip*/

        titleElement.removeAttribute(_Tooltip3.TITLE_ATTR);
      }

      var _isHtmlTitle = titleElement.getAttribute(_Tooltip3.DATA_TITLE_ISHTML);

      if (_isHtmlTitle) {
        dispatch((0, _Tooltip2.setTitle)(_title, _isHtmlTitle == "true" ? true : false));
      } else {
        dispatch((0, _Tooltip2.setTitle)(_title));
      }

      e.isTooltipHanlde = true;
      reference(titleElement);
    } else {
      handleReset();
    }
  }

  var mouseHoverDebounced = (0, _debounce["default"])(mouseHover, _Tooltip3.TOOLTIP_DELAY);
  (0, _react.useEffect)(function () {
    container.addEventListener("mouseover", mouseHoverDebounced);
    container.addEventListener("mouseleave", handleReset);
    container.addEventListener("click", handleReset);
    return function () {
      container.removeEventListener("mouseover", mouseHoverDebounced);
      container.removeEventListener("mouseleave", handleReset);
      container.removeEventListener("click", handleReset);
    };
  }, [title, container]);
  var arrowX = (0, _selectn["default"])("arrow.x", middlewareData);
  var arrowY = (0, _selectn["default"])("arrow.y", middlewareData);
  return {
    title: title,
    isHtmlTitle: isHtmlTitle,
    floating: floating,
    position: position,
    x: x,
    y: y,
    arrowReference: arrowReference,
    arrowX: arrowX,
    arrowY: arrowY,
    arrowPlacement: _Tooltip3.ARROW_PLACEMENT[placement.split('-')[0]],
    componentThemeProps: componentThemeProps
  };
}