import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import classNames from 'classnames';
import PopperJs from 'popper.js';
import { cloneElement, Component, createRef, Fragment } from 'react';
import { findDOMNode } from 'react-dom';
import { CLS_PREFIX } from '../../constants';
import Layer from '../Layer';
import { jsxs as _jsxs } from "react/jsx-runtime";
import { jsx as _jsx } from "react/jsx-runtime";
/**
 * `Popper` component.
 */
class Popper extends Component {
  constructor() {
    super(...arguments);
    _defineProperty(this, "state", {
      popperStyle: null,
      arrowStyle: null,
      actualPlacement: this.props.placement
    });
    _defineProperty(this, "popper", null);
    _defineProperty(this, "contentRef", /*#__PURE__*/createRef());
    _defineProperty(this, "arrowRef", null);
    // @ts-ignore
    _defineProperty(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 = findDOMNode(this);
    }
    this.popper = new PopperJs(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__*/cloneElement(arrow, {
      ref: ref => this.arrowRef = ref,
      style: arrowStyle
    });
    return /*#__PURE__*/_jsxs(Fragment, {
      children: [target, show ? /*#__PURE__*/_jsx(Layer, {
        level: zIndex,
        children: /*#__PURE__*/_jsxs("div", {
          ref: this.contentRef,
          className: classNames(baseClassName, className, `${baseClassName}--placement-${actualPlacement}`),
          style: {
            ...style,
            ...(popperStyle ? popperStyle : {
              position: 'fixed',
              opacity: 0
            })
          },
          ...props,
          children: [children, arrowElement]
        })
      }) : null]
    });
  }
}
_defineProperty(Popper, "defaultProps", {
  show: true,
  children: null,
  arrow: null,
  placement: 'bottom-start',
  behavior: 'flip',
  offset: '0, 0',
  onUpdate: null,
  className: null,
  baseClassName: `${CLS_PREFIX}popper`,
  zIndex: null,
  boundariesElement: 'window',
  style: undefined,
  targetRef: undefined
});
export default Popper;