"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PopupState = exports["default"] = void 0;

var _jsxRuntime = require("react/jsx-runtime");

var _handle = require("@enact/core/handle");

var _hoc = _interopRequireDefault(require("@enact/core/hoc"));

var _FloatingLayer = _interopRequireDefault(require("@enact/ui/FloatingLayer"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _react = require("react");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var OpenState = {
  CLOSED: 0,
  OPENING: 1,
  OPEN: 2
};
var forwardHide = (0, _handle.forward)('onHide');
var PopupState = (0, _hoc["default"])(function (config, Wrapped) {
  var _class, _temp;

  // eslint-disable-line no-unused-vars
  return _temp = _class = /*#__PURE__*/function (_Component) {
    _inherits(_class, _Component);

    var _super = _createSuper(_class);

    function _class(props) {
      var _this;

      _classCallCheck(this, _class);

      _this = _super.call(this, props);

      _this.handleFloatingLayerOpen = function () {
        if (!_this.props.noAnimation && _this.state.popupOpen !== OpenState.OPEN) {
          _this.setState({
            popupOpen: OpenState.OPENING
          });
        }
      };

      _this.handlePopupHide = function () {
        forwardHide(null, _this.props);

        _this.setState({
          floatLayerOpen: false,
          activator: null
        });
      };

      _this.state = {
        floatLayerOpen: _this.props.open,
        popupOpen: _this.props.open ? OpenState.OPEN : OpenState.CLOSED,
        prevOpen: _this.props.open
      };
      return _this;
    }

    _createClass(_class, [{
      key: "render",
      value: function render() {
        var _this$props = this.props,
            noAutoDismiss = _this$props.noAutoDismiss,
            onClose = _this$props.onClose,
            scrimType = _this$props.scrimType,
            rest = _objectWithoutProperties(_this$props, ["noAutoDismiss", "onClose", "scrimType"]);

        delete rest.spotlightRestrict;
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_FloatingLayer["default"], {
          noAutoDismiss: noAutoDismiss,
          open: this.state.floatLayerOpen,
          onOpen: this.handleFloatingLayerOpen,
          onDismiss: onClose,
          scrimType: scrimType,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread(_objectSpread({}, rest), {}, {
            open: this.state.popupOpen >= OpenState.OPENING,
            onClose: onClose,
            onHide: this.handlePopupHide
          }))
        });
      }
    }], [{
      key: "getDerivedStateFromProps",
      value: function getDerivedStateFromProps(props, state) {
        if (props.open !== state.prevOpen) {
          if (props.open) {
            return {
              popupOpen: props.noAnimation || state.floatLayerOpen ? OpenState.OPEN : OpenState.CLOSED,
              floatLayerOpen: true,
              prevOpen: props.open
            };
          } else {
            return {
              popupOpen: OpenState.CLOSED,
              floatLayerOpen: state.popupOpen !== OpenState.CLOSED ? !props.noAnimation : false,
              prevOpen: props.open
            };
          }
        }

        return null;
      }
    }]);

    return _class;
  }(_react.Component), _class.displayName = 'PopupState', _class.propTypes = {
    noAnimation: _propTypes["default"].bool,
    noAutoDismiss: _propTypes["default"].bool,
    onClose: _propTypes["default"].func,
    open: _propTypes["default"].bool,
    scrimType: _propTypes["default"].oneOf(['transparent', 'translucent', 'none'])
  }, _class.defaultProps = {
    noAnimation: false,
    noAutoDismiss: false,
    open: false,
    scrimType: 'translucent'
  }, _temp;
});
exports.PopupState = PopupState;
var _default = PopupState;
exports["default"] = _default;