"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _wixUiIconsCommon = require("@wix/wix-ui-icons-common");
var _constants = require("./constants");
var _Tooltip = _interopRequireDefault(require("../Tooltip"));
var _InfoIconSt = require("./InfoIcon.st.css.js");
var _useId = require("../utils/useId");
var _WixDesignSystemIconThemeProvider = require("../WixDesignSystemIconThemeProvider");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/InfoIcon/InfoIcon.tsx";
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); }
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 InfoIcon = _ref => {
  var _ariaLabelledby, _ariaDescribedby;
  var {
    dataHook,
    content,
    size = 'medium',
    tooltipProps,
    className,
    children,
    ariaLabel,
    'aria-labelledby': ariaLabelledBy,
    'aria-describedby': ariaDescribedBy
  } = _ref;
  var icons = (0, _WixDesignSystemIconThemeProvider.useIcons)('InfoIcon', {
    InfoCircle: _wixUiIconsCommon.InfoCircle,
    InfoCircleSmall: _wixUiIconsCommon.InfoCircleSmall
  });
  var iconComponentBySizeMap = {
    small: icons.InfoCircleSmall,
    medium: icons.InfoCircle
  };
  var Icon = iconComponentBySizeMap[size];
  var tooltipRef = (0, _react.useRef)(null);
  var triggerRef = (0, _react.useRef)(null);
  var [isOpenedViaKeyboard, setIsOpenedViaKeyboard] = (0, _react.useState)(false);
  var autoId = (0, _useId.useId)();
  var propAriaLabelledBy = (_ariaLabelledby = tooltipProps == null ? void 0 : tooltipProps['aria-labelledby']) !== null && _ariaLabelledby !== void 0 ? _ariaLabelledby : ariaLabelledBy;
  var propAriaDescribedBy = (_ariaDescribedby = tooltipProps == null ? void 0 : tooltipProps['aria-describedby']) !== null && _ariaDescribedby !== void 0 ? _ariaDescribedby : ariaDescribedBy;
  var tooltipId = propAriaLabelledBy || propAriaDescribedBy || "infoicon-tooltip-".concat(autoId);
  var mergedTooltipProps = _objectSpread(_objectSpread({}, tooltipProps), propAriaLabelledBy ? {
    'aria-labelledby': propAriaLabelledBy
  } : {
    'aria-describedby': propAriaDescribedBy || tooltipId
  });
  var focusFirstInteractiveElement = (0, _react.useCallback)(() => {
    requestAnimationFrame(() => {
      var _tooltipRef$current;
      var tooltipContent = (_tooltipRef$current = tooltipRef.current) == null ? void 0 : _tooltipRef$current.getContentElement();
      var focusable = tooltipContent == null ? void 0 : tooltipContent.querySelector(_constants.FOCUSABLE_SELECTOR);
      focusable == null || focusable.focus({
        preventScroll: true
      });
    });
  }, []);
  var closeAndRefocus = (0, _react.useCallback)(() => {
    var _tooltipRef$current2, _triggerRef$current;
    (_tooltipRef$current2 = tooltipRef.current) == null || _tooltipRef$current2.close();
    setIsOpenedViaKeyboard(false);
    (_triggerRef$current = triggerRef.current) == null || _triggerRef$current.focus();
  }, []);

  // Handle when focus returns to trigger from tooltip content (e.g., via Tab)
  var handleTriggerFocus = (0, _react.useCallback)(event => {
    var _tooltipRef$current3;
    if (!isOpenedViaKeyboard) return;
    var relatedTarget = event.relatedTarget;
    var tooltipContent = (_tooltipRef$current3 = tooltipRef.current) == null ? void 0 : _tooltipRef$current3.getContentElement();

    // If focus came from inside the tooltip content, close the tooltip
    if (tooltipContent != null && tooltipContent.contains(relatedTarget)) {
      var _tooltipRef$current4;
      (_tooltipRef$current4 = tooltipRef.current) == null || _tooltipRef$current4.close();
      setIsOpenedViaKeyboard(false);
    }
  }, [isOpenedViaKeyboard]);
  var handleKeyDown = event => {
    if (event.key === 'Escape') {
      closeAndRefocus();
    }
    if (event.key === 'Enter') {
      var _tooltipRef$current5;
      event.preventDefault();
      (_tooltipRef$current5 = tooltipRef.current) == null || _tooltipRef$current5.open();
      setIsOpenedViaKeyboard(true);
      focusFirstInteractiveElement();
    }
  };

  // Global escape key handler for when focus is inside the tooltip
  (0, _react.useEffect)(() => {
    if (!isOpenedViaKeyboard) return;
    var handleGlobalKeyDown = event => {
      if (event.key === 'Escape') {
        event.preventDefault();
        event.stopPropagation();
        closeAndRefocus();
      }
    };

    // Use capture phase to run before Tooltip's handler
    document.addEventListener('keydown', handleGlobalKeyDown, true);
    return () => {
      document.removeEventListener('keydown', handleGlobalKeyDown, true);
    };
  }, [isOpenedViaKeyboard, closeAndRefocus]);
  var handleBlur = (0, _react.useCallback)(event => {
    var _triggerRef$current2, _tooltipRef$current6;
    // Only handle blur when tooltip was opened via keyboard
    // Hover-opened tooltips are closed by Tooltip's own handleMouseLeave
    if (!isOpenedViaKeyboard) {
      return;
    }
    var relatedTarget = event.relatedTarget;

    // Check if focus is moving to the trigger
    if ((_triggerRef$current2 = triggerRef.current) != null && _triggerRef$current2.contains(relatedTarget)) {
      return;
    }

    // Check if focus is moving to an element inside the tooltip content
    var tooltipContent = (_tooltipRef$current6 = tooltipRef.current) == null ? void 0 : _tooltipRef$current6.getContentElement();
    if (tooltipContent != null && tooltipContent.contains(relatedTarget)) {
      return;
    }

    // Focus left both trigger and tooltip - close and refocus
    requestAnimationFrame(() => {
      closeAndRefocus();
    });
  }, [isOpenedViaKeyboard, closeAndRefocus]);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _InfoIconSt.st)(_InfoIconSt.classes.root, className),
    "data-size": size,
    "data-hook": dataHook,
    onBlur: handleBlur,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 146,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_Tooltip.default, (0, _extends2.default)({}, mergedTooltipProps, {
    content: content,
    dataHook: _constants.dataHooks.tooltip,
    ref: tooltipRef,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 152,
      columnNumber: 7
    }
  }), () => /*#__PURE__*/_react.default.createElement("div", {
    ref: triggerRef,
    className: _InfoIconSt.classes.trigger,
    role: "button",
    tabIndex: 0,
    "aria-label": ariaLabel,
    "aria-haspopup": true,
    "aria-controls": tooltipId,
    onKeyDown: handleKeyDown,
    onFocus: handleTriggerFocus,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 160,
      columnNumber: 11
    }
  }, children !== null && children !== void 0 ? children : /*#__PURE__*/_react.default.createElement(Icon, {
    className: _InfoIconSt.classes.icon,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 171,
      columnNumber: 26
    }
  }))));
};
InfoIcon.displayName = 'InfoIcon';
var _default = exports.default = InfoIcon;
//# sourceMappingURL=InfoIcon.js.map