"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classnames = _interopRequireDefault(require("classnames"));
var _popper = _interopRequireDefault(require("popper.js"));
var _react = require("react");
var _reactDom = require("react-dom");
var _constants = require("../../constants");
var _Layer = _interopRequireDefault(require("../Layer"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

/**
 * `Popper` component.
 */
class Popper extends _react.Component {
  constructor() {
    super(...arguments);
    (0, _defineProperty2.default)(this, "state", {
      popperStyle: null,
      arrowStyle: null,
      actualPlacement: this.props.placement
    });
    (0, _defineProperty2.default)(this, "popper", null);
    (0, _defineProperty2.default)(this, "contentRef", /*#__PURE__*/(0, _react.createRef)());
    (0, _defineProperty2.default)(this, "arrowRef", null);
    // @ts-ignore
    (0, _defineProperty2.default)(this, "extractStyles", state => {
      const {
        onUpdate
      } = this.props;
      const data = {
        popperStyle: {
          ...(state.hide ? {
            visibility: 'hidden',
            pointerEvents: 'none'
          } : undefined),
          ...state.styles,
          position: state.offsets.popper.position
        },
        actualPlacement: state.placement,
        arrowStyle: state.arrowStyles
      };
      this.setState(data);
      onUpdate?.(data);
    });
  }
  componentDidMount() {
    this.updatePopper(this.props);
  }
  componentDidUpdate(prevProps) {
    const {
      show,
      boundariesElement,
      placement,
      offset,
      behavior,
      arrow,
      children
    } = this.props;
    if (prevProps.show !== show || prevProps.boundariesElement !== boundariesElement || prevProps.placement !== placement || prevProps.offset !== offset || prevProps.behavior !== behavior || prevProps.arrow !== arrow || prevProps.children !== children) {
      setTimeout(() => {
        this.updatePopper(this.props);
      }, 0);
    }
  }
  componentWillUnmount() {
    if (this.popper) {
      this.popper.destroy();
    }
  }
  updatePopper(_ref) {
    let {
      show,
      boundariesElement,
      placement,
      offset,
      behavior,
      arrow,
      targetRef
    } = _ref;
    if (this.popper) {
      this.popper.destroy();
    }
    if (!this.contentRef.current || !show) {
      return;
    }
    const popperOpts = {
      placement,
      modifiers: {
        applyStyle: {
          enabled: false
        },
        applyReactStyle: {
          enabled: true,
          fn: this.extractStyles,
          order: 900
        },
        preventOverflow: {
          enabled: true,
          boundariesElement
        },
        hide: {
          enabled: true
        },
        offset: {
          enabled: true,
          offset
        },
        flip: {
          enabled: true,
          padding: 0,
          behavior
        },
        arrow: arrow && this.arrowRef ? {
          enabled: true,
          element: this.arrowRef
        } : undefined
      }
    };
    let reference;
    if (targetRef && targetRef.current) {
      reference = targetRef.current;
    } else {
      reference = (0, _reactDom.findDOMNode)(this);
    }
    this.popper = new _popper.default(reference, this.contentRef.current, popperOpts);
  }
  render() {
    const {
      baseClassName,
      className,
      children,
      target,
      targetRef,
      arrow,
      placement,
      behavior,
      offset,
      onUpdate,
      zIndex,
      show,
      boundariesElement,
      style,
      ...props
    } = this.props;
    const {
      popperStyle
    } = this.state;
    const {
      arrowStyle,
      actualPlacement
    } = this.state;
    const arrowElement = arrow && /*#__PURE__*/(0, _react.cloneElement)(arrow, {
      ref: ref => this.arrowRef = ref,
      style: arrowStyle
    });
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_react.Fragment, {
      children: [target, show ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Layer.default, {
        level: zIndex,
        children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          ref: this.contentRef,
          className: (0, _classnames.default)(baseClassName, className, `${baseClassName}--placement-${actualPlacement}`),
          style: {
            ...style,
            ...(popperStyle ? popperStyle : {
              position: 'fixed',
              opacity: 0
            })
          },
          ...props,
          children: [children, arrowElement]
        })
      }) : null]
    });
  }
}
(0, _defineProperty2.default)(Popper, "defaultProps", {
  show: true,
  children: null,
  arrow: null,
  placement: 'bottom-start',
  behavior: 'flip',
  offset: '0, 0',
  onUpdate: null,
  className: null,
  baseClassName: `${_constants.CLS_PREFIX}popper`,
  zIndex: null,
  boundariesElement: 'window',
  style: undefined,
  targetRef: undefined
});
var _default = exports.default = Popper;