"use strict";

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

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

var _deepFreeze = _interopRequireDefault(require("deep-freeze"));

var _dispatcher = require("@enact/core/dispatcher");

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

var _immer = require("immer");

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 _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 Context = /*#__PURE__*/(0, _react.createContext)();
exports.AppContext = Context;
var defaultConfig = {
  pauseOnBlur: false,
  state: {}
};
var ProviderDecorator = (0, _hoc["default"])(defaultConfig, function (config, Wrapped) {
  var _class, _temp;

  var pauseOnBlur = config.pauseOnBlur,
      defaultState = config.state;
  var PureWrapped = /*#__PURE__*/(0, _react.memo)(Wrapped);
  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.handleBlur = function () {
        _this.focused = false;
      };

      _this.handleFocus = function () {
        _this.focused = true;

        while (_this.queue.length) {
          _this.setState(_this.queue.shift());
        }
      };

      _this.updateAppState = function (cb) {
        var updater = (0, _immer.produce)(cb);

        if (_this.focused) {
          _this.setState(updater);
        } else {
          _this.queue.push(updater);
        }
      };

      _this.state = defaultState;
      _this.focused = true;
      _this.queue = [];
      return _this;
    }

    _createClass(_class, [{
      key: "componentDidMount",
      value: function componentDidMount() {
        if (pauseOnBlur) {
          (0, _dispatcher.on)('blur', this.handleBlur, window);
          (0, _dispatcher.on)('focus', this.handleFocus, window);
        }
      }
    }, {
      key: "componentWillUnount",
      value: function componentWillUnount() {
        if (pauseOnBlur) {
          (0, _dispatcher.off)('blur', this.handleBlur, window);
          (0, _dispatcher.off)('focus', this.handleFocus, window);
        }
      }
    }, {
      key: "getState",
      value: function getState() {
        if (process.env.NODE_ENV !== "production") {
          return (0, _deepFreeze["default"])(this.state);
        }

        return this.state;
      }
    }, {
      key: "render",
      value: function render() {
        var context = {
          state: this.getState(this.state),
          updateAppState: this.updateAppState
        };
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(Context.Provider, {
          value: context,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(PureWrapped, _objectSpread({}, this.props))
        });
      }
    }]);

    return _class;
  }(_react.Component), _class.displayName = 'ProviderDecorator', _temp;
});
exports.ProviderDecorator = ProviderDecorator;
var _default = ProviderDecorator;
exports["default"] = _default;