"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _react = _interopRequireWildcard(require("react"));
var _PopoverNext = _interopRequireDefault(require("../../PopoverNext"));
var _constants = require("./constants");
var _excluded = ["content", "skin", "target", "children", "onClose", "onOpen", "initiallyOpened", "closeOnMouseLeave", "hideDelay"];
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/FloatingHelper/ClosablePopover/ClosablePopover.jsx";
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; }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var controlledErrorMsg = function controlledErrorMsg(method) {
  return "ClosablePopover.".concat(method, "() can not be called when component is Controlled. (opened prop should be undefined)");
};

/**
 * Closable Popover
 * Either a normal Controlled Popover, or a Popover that is initially opened and can be the closed by
 * calling a closeAction.
 */
var ClosablePopover = /*#__PURE__*/function (_PureComponent) {
  function ClosablePopover() {
    var _this;
    (0, _classCallCheck2["default"])(this, ClosablePopover);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, ClosablePopover, [].concat(args));
    _this.state = {
      open: _this.props.initiallyOpened,
      mode: _this.props.initiallyOpened ? _constants.Mode.ClickToClose : _constants.Mode.Hover
    };
    _this.open = function () {
      return _this._doOpen(_constants.Mode.ClickToClose);
    };
    _this._doOpen = function (nextMode) {
      !_this.state.open && _this.setState({
        open: true,
        mode: nextMode
      }, function () {
        _this.props.onOpen && _this.props.onOpen();
      });
    };
    _this.close = function () {
      if (_this._isControlled()) {
        throw new Error(controlledErrorMsg('close'));
      }
      _this.state.open && _this.setState({
        open: false,
        mode: _constants.Mode.Hover
      }, function () {
        _this.props.onClose && _this.props.onClose();
      });
    };
    _this._handleMouseEnter = function () {
      if (_this.state.mode === _constants.Mode.Hover) {
        _this._doOpen(_constants.Mode.Hover);
      }
    };
    _this._handleMouseLeave = function () {
      if (_this.state.mode === _constants.Mode.Hover && _this.props.closeOnMouseLeave) {
        _this.close();
      }
    };
    _this.actions = {
      close: _this.close
    };
    return _this;
  }
  (0, _inherits2["default"])(ClosablePopover, _PureComponent);
  return (0, _createClass2["default"])(ClosablePopover, [{
    key: "_isControlled",
    value: function _isControlled() {
      return typeof this.props.opened === 'boolean';
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
        content = _this$props.content,
        skin = _this$props.skin,
        target = _this$props.target,
        children = _this$props.children,
        onClose = _this$props.onClose,
        onOpen = _this$props.onOpen,
        initiallyOpened = _this$props.initiallyOpened,
        closeOnMouseLeave = _this$props.closeOnMouseLeave,
        hideDelay = _this$props.hideDelay,
        rest = (0, _objectWithoutProperties2["default"])(_this$props, _excluded);
      var open = this._isControlled() ? this.props.opened : this.state.open;
      var popoverProps = _objectSpread(_objectSpread({
        appendTo: 'parent'
      }, rest), {}, {
        skin: skin,
        animate: true,
        open: open,
        onMouseEnter: this._handleMouseEnter,
        onMouseLeave: this._handleMouseLeave
      });
      return /*#__PURE__*/_react["default"].createElement(_PopoverNext["default"], (0, _extends2["default"])({}, popoverProps, {
        onOpenChange: function onOpenChange() {},
        moveBy: {
          y: 3
        },
        focusManagerEnabled: false,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 93,
          columnNumber: 7
        }
      }), /*#__PURE__*/_react["default"].createElement(_PopoverNext["default"].Trigger, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 101,
          columnNumber: 9
        }
      }, /*#__PURE__*/_react["default"].createElement("span", {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 102,
          columnNumber: 11
        }
      }, target)), /*#__PURE__*/_react["default"].createElement(_PopoverNext["default"].Content, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 104,
          columnNumber: 9
        }
      }, content(this.actions)));
    }
  }]);
}(_react.PureComponent);
ClosablePopover.defaultProps = {
  hideDelay: 0,
  initiallyOpened: true,
  closeOnMouseLeave: true
};
var _default = exports["default"] = ClosablePopover;