import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "core-js/modules/es.array.concat.js";
import React, { useEffect } from 'react';
import { FcrIcon } from '../icon';
import { FcrIconType } from '../icon/type';
import { FcrPopover } from '../popover';
import { FcrToolTip } from '../tooltip';
import './tooltip-button.css';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export var FrcTooltipButton = function FrcTooltipButton(props) {
  var active = props.active,
    withPopover = props.withPopover,
    classNames = props.classNames,
    _props$tooltip = props.tooltip,
    tooltipIcon = _props$tooltip.icon,
    _props$tooltip$trigge = _props$tooltip.trigger,
    tooltipTrigger = _props$tooltip$trigge === void 0 ? 'hover' : _props$tooltip$trigge,
    _props$tooltip$placem = _props$tooltip.placement,
    tooltipPlacement = _props$tooltip$placem === void 0 ? 'top' : _props$tooltip$placem,
    tooltipContent = _props$tooltip.content,
    _props$popover = props.popover,
    _props$popover2 = _props$popover === void 0 ? {} : _props$popover,
    popoverIcon = _props$popover2.icon,
    _props$popover2$trigg = _props$popover2.trigger,
    popoverTrigger = _props$popover2$trigg === void 0 ? 'click' : _props$popover2$trigg,
    _props$popover2$place = _props$popover2.placement,
    popoverPlacement = _props$popover2$place === void 0 ? 'top' : _props$popover2$place,
    popoverContent = _props$popover2.content,
    onVisibleChange = _props$popover2.onVisibleChange,
    _props$popover2$visib = _props$popover2.visible,
    visible = _props$popover2$visib === void 0 ? false : _props$popover2$visib,
    _props$onTooltipButto = props.onTooltipButtonClick,
    onTooltipButtonClick = _props$onTooltipButto === void 0 ? function () {} : _props$onTooltipButto,
    children = props.children;
  var _React$useState = React.useState(visible),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    popoverVisible = _React$useState2[0],
    setVisible = _React$useState2[1];
  var handlerVisibleChange = function handlerVisibleChange(visible) {
    setVisible(visible);
    onVisibleChange === null || onVisibleChange === void 0 || onVisibleChange(visible);
  };
  useEffect(function () {
    setVisible(visible);
  }, [visible]);
  useEffect(function () {
    function handleBlur(event) {
      setVisible(false);
    }
    window.addEventListener('blur', handleBlur);
    return function () {
      window.removeEventListener('blur', handleBlur);
    };
  }, []);
  return /*#__PURE__*/_jsxs("div", {
    className: "frc-tooltip-button ".concat(classNames === null || classNames === void 0 ? void 0 : classNames.container, " ").concat(active ? 'frc-tooltip-button--active' : ''),
    children: [/*#__PURE__*/_jsx(FcrToolTip, {
      content: tooltipContent,
      trigger: tooltipTrigger,
      placement: tooltipPlacement,
      overlayInnerStyle: {
        border: 0,
        boxShadow: '0px 0px 15px var(--fcr_web_light_dropup_m)',
        borderRadius: 'var(--fcr_cornerradius_l)',
        padding: 10
      },
      mouseEnterDelay: 0,
      children: /*#__PURE__*/_jsxs("div", {
        className: "tooltip-button ".concat(withPopover ? '' : 'tooltip-single-button'),
        onClick: onTooltipButtonClick,
        children: [(tooltipIcon === null || tooltipIcon === void 0 ? void 0 : tooltipIcon.type) && /*#__PURE__*/_jsx(FcrIcon, {
          type: tooltipIcon.type,
          size: tooltipIcon.size,
          colors: tooltipIcon.colors
        }), children]
      })
    }), withPopover && /*#__PURE__*/_jsx(FcrPopover, {
      trigger: popoverTrigger,
      overlayClassName: "frc-tooltip-button__popover ".concat(classNames === null || classNames === void 0 ? void 0 : classNames.container),
      placement: popoverPlacement,
      mouseEnterDelay: 0,
      onVisibleChange: handlerVisibleChange,
      visible: popoverVisible,
      overlayInnerStyle: {
        border: 0,
        boxShadow: 'none',
        borderRadius: 0
      },
      content: popoverContent,
      children: /*#__PURE__*/_jsx("div", {
        className: "popover-button ".concat(active ? 'popover-button--active' : ''),
        children: /*#__PURE__*/_jsx(FcrIcon, {
          type: FcrIconType.FCR_DROPUP4,
          size: 24,
          colors: {
            iconPrimary: 'var(--fcr_web_ui_scene_mainicon1)'
          }
        })
      })
    })]
  });
};