import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["child", "children", "enterDelay", "leaveDelay", "offset", "type", "trigger", "placement", "initialVisible", "portalClassName", "disableItemsAutoClose", "onVisibleChange", "visible"];
import _JSXStyle from "../styled-jsx.es.js";
/* "use client" */
import React, { useEffect, useMemo, useState } from 'react';
import Tooltip from '../tooltip/tooltip';
import { getReactNode } from '../utils/collections';
import useScale, { withScale } from '../use-scale';
import { PopoverContext } from './popover-context';
import { useClasses } from '@helpdice/theme';
var PopoverComponent = function PopoverComponent(_ref) {
  var child = _ref.child,
    children = _ref.children,
    _ref$enterDelay = _ref.enterDelay,
    enterDelay = _ref$enterDelay === void 0 ? 100 : _ref$enterDelay,
    _ref$leaveDelay = _ref.leaveDelay,
    leaveDelay = _ref$leaveDelay === void 0 ? 150 : _ref$leaveDelay,
    _ref$offset = _ref.offset,
    offset = _ref$offset === void 0 ? 12 : _ref$offset,
    _ref$type = _ref.type,
    type = _ref$type === void 0 ? 'default' : _ref$type,
    _ref$trigger = _ref.trigger,
    trigger = _ref$trigger === void 0 ? 'click' : _ref$trigger,
    _ref$placement = _ref.placement,
    placement = _ref$placement === void 0 ? 'bottom' : _ref$placement,
    _ref$initialVisible = _ref.initialVisible,
    initialVisible = _ref$initialVisible === void 0 ? false : _ref$initialVisible,
    _ref$portalClassName = _ref.portalClassName,
    portalClassName = _ref$portalClassName === void 0 ? '' : _ref$portalClassName,
    _ref$disableItemsAuto = _ref.disableItemsAutoClose,
    disableItemsAutoClose = _ref$disableItemsAuto === void 0 ? false : _ref$disableItemsAuto,
    _ref$onVisibleChange = _ref.onVisibleChange,
    onVisibleChange = _ref$onVisibleChange === void 0 ? function () {} : _ref$onVisibleChange,
    customVisible = _ref.visible,
    props = _objectWithoutProperties(_ref, _excluded);
  var _useScale = useScale(),
    SCALES = _useScale.SCALES;
  var _useState = useState(initialVisible),
    _useState2 = _slicedToArray(_useState, 2),
    visible = _useState2[0],
    setVisible = _useState2[1];
  var textNode = useMemo(function () {
    return getReactNode(child);
  }, [child]);
  var onChildClick = function onChildClick() {
    onPopoverVisibleChange(false);
  };
  var value = useMemo(function () {
    return {
      onItemClick: onChildClick,
      disableItemsAutoClose: disableItemsAutoClose
    };
  }, [disableItemsAutoClose]);
  var classes = useClasses('popover', portalClassName);
  var onPopoverVisibleChange = function onPopoverVisibleChange(next) {
    setVisible(next);
    onVisibleChange(next);
  };
  useEffect(function () {
    if (customVisible === undefined) return;
    onPopoverVisibleChange(customVisible);
  }, [customVisible]);
  return /*#__PURE__*/React.createElement(PopoverContext.Provider, {
    value: value
  }, /*#__PURE__*/React.createElement(Tooltip, _extends({
    type: type,
    enterDelay: enterDelay,
    leaveDelay: leaveDelay,
    offset: offset,
    text: textNode,
    trigger: trigger,
    placement: placement,
    portalClassName: classes,
    visible: visible,
    onVisibleChange: onPopoverVisibleChange
  }, props), children, /*#__PURE__*/React.createElement(_JSXStyle, {
    id: "622655983",
    dynamic: [SCALES.pt(0.9), SCALES.pr(0), SCALES.pb(0.9), SCALES.pl(0)]
  }, ".tooltip-content.popover > .inner{padding:".concat(SCALES.pt(0.9), " ").concat(SCALES.pr(0), " ").concat(SCALES.pb(0.9), " ").concat(SCALES.pl(0), ";}"))));
};
PopoverComponent.displayName = 'Popover';
var Popover = withScale(PopoverComponent);
export default Popover;