Liferay.Loader.define("layout-content-page-editor-web@2.0.161/page_editor/app/store/index", ['module', 'exports', 'require', 'frontend-js-react-web', 'frontend-js-react-web$react', '../components/undo/useUndo'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.useSelector = exports.useSelectorCallback = exports.useDispatch = exports.StoreContextProvider = exports.StoreAPIContextProvider = void 0;

    var _frontendJsReactWeb = require("frontend-js-react-web");

    var _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    var _useUndo = _interopRequireDefault(require("../components/undo/useUndo"));

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

    function _getRequireWildcardCache() {
      if (typeof WeakMap !== "function") return null;var cache = new WeakMap();_getRequireWildcardCache = function _getRequireWildcardCache() {
        return cache;
      };return cache;
    }

    function _interopRequireWildcard(obj) {
      if (obj && obj.__esModule) {
        return obj;
      }if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
        return { default: obj };
      }var cache = _getRequireWildcardCache();if (cache && cache.has(obj)) {
        return cache.get(obj);
      }var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;if (desc && (desc.get || desc.set)) {
            Object.defineProperty(newObj, key, desc);
          } else {
            newObj[key] = obj[key];
          }
        }
      }newObj.default = obj;if (cache) {
        cache.set(obj, newObj);
      }return newObj;
    }

    function _slicedToArray(arr, i) {
      return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
    }

    function _nonIterableRest() {
      throw new TypeError("Invalid attempt to destructure non-iterable instance");
    }

    function _iterableToArrayLimit(arr, i) {
      if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
        return;
      }var _arr = [];var _n = true;var _d = false;var _e = undefined;try {
        for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
          _arr.push(_s.value);if (i && _arr.length === i) break;
        }
      } catch (err) {
        _d = true;_e = err;
      } finally {
        try {
          if (!_n && _i["return"] != null) _i["return"]();
        } finally {
          if (_d) throw _e;
        }
      }return _arr;
    }

    function _arrayWithHoles(arr) {
      if (Array.isArray(arr)) return arr;
    }

    function _toConsumableArray(arr) {
      return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
    }

    function _nonIterableSpread() {
      throw new TypeError("Invalid attempt to spread non-iterable instance");
    }

    function _iterableToArray(iter) {
      if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
    }

    function _arrayWithoutHoles(arr) {
      if (Array.isArray(arr)) {
        for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
          arr2[i] = arr[i];
        }return arr2;
      }
    }

    var StoreDispatchContext = /*#__PURE__*/_react.default.createContext(function () {});

    var StoreGetStateContext = /*#__PURE__*/_react.default.createContext(null);

    var StoreSubscriptionContext = /*#__PURE__*/_react.default.createContext([function () {}, function () {}]);

    var DEFAULT_COMPARE_EQUAL = function DEFAULT_COMPARE_EQUAL(a, b) {
      return a === b;
    };

    var DEFAULT_DISPATCH = function DEFAULT_DISPATCH() {};

    var DEFAULT_GET_STATE = function DEFAULT_GET_STATE() {
      return {};
    };
    /**
     * Although StoreContextProvider creates a full functional store,
     * sometimes mocking dispatchs and/or store state may be necessary
     * for testing purposes.
     *
     * This component wraps it's children with an usable StoreContext
     * that calls dispatch and getState methods instead of using a real
     * reducer internally.
     */

    var StoreAPIContextProvider = function StoreAPIContextProvider(_ref) {
      var children = _ref.children,
          _ref$dispatch = _ref.dispatch,
          dispatch = _ref$dispatch === void 0 ? DEFAULT_DISPATCH : _ref$dispatch,
          _ref$getState = _ref.getState,
          getState = _ref$getState === void 0 ? DEFAULT_GET_STATE : _ref$getState;
      var state = getState();
      var subscribers = (0, _react.useRef)([]);
      var subscribe = (0, _react.useCallback)(function (subscriber) {
        subscribers.current = [].concat(_toConsumableArray(subscribers.current), [subscriber]);
      }, []);
      var unsubscribe = (0, _react.useCallback)(function (subscriber) {
        subscribers.current = subscribers.current.filter(function (_subscriber) {
          return _subscriber !== subscriber;
        });
      }, []);
      var subscriptionContext = (0, _react.useRef)([subscribe, unsubscribe]);
      (0, _react.useEffect)(function () {
        subscribers.current.forEach(function (subscriber) {
          return subscriber(state);
        });
      }, [state]);
      return (/*#__PURE__*/_react.default.createElement(StoreSubscriptionContext.Provider, {
          value: subscriptionContext.current
        }, /*#__PURE__*/_react.default.createElement(StoreDispatchContext.Provider, {
          value: dispatch
        }, /*#__PURE__*/_react.default.createElement(StoreGetStateContext.Provider, {
          value: getState
        }, children)))
      );
    };
    /**
     * StoreContext is a black box for components: they should
     * get information from state and dispatch actions by using
     * given useSelector and useDispatch hooks.
     *
     * That's why we only provide a custom StoreContextProvider instead
     * of the raw React context.
     */

    exports.StoreAPIContextProvider = StoreAPIContextProvider;

    var StoreContextProvider = function StoreContextProvider(_ref2) {
      var children = _ref2.children,
          initialState = _ref2.initialState,
          reducer = _ref2.reducer;

      var _useThunk = (0, _frontendJsReactWeb.useThunk)((0, _useUndo.default)((0, _react.useReducer)(reducer, initialState))),
          _useThunk2 = _slicedToArray(_useThunk, 2),
          state = _useThunk2[0],
          dispatch = _useThunk2[1];

      var stateRef = (0, _react.useRef)(state);
      var getState = (0, _react.useCallback)(function () {
        return stateRef.current;
      }, []);
      stateRef.current = state;
      return (/*#__PURE__*/_react.default.createElement(StoreAPIContextProvider, {
          dispatch: dispatch,
          getState: getState
        }, children)
      );
    };
    /**
     * @see https://react-redux.js.org/api/hooks#usedispatch
     */

    exports.StoreContextProvider = StoreContextProvider;

    var useDispatch = function useDispatch() {
      return (0, _react.useContext)(StoreDispatchContext);
    };

    exports.useDispatch = useDispatch;

    var useSelectorCallback = function useSelectorCallback(selector, dependencies) {
      var compareEqual = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_COMPARE_EQUAL;
      var getState = (0, _react.useContext)(StoreGetStateContext);

      var _useContext = (0, _react.useContext)(StoreSubscriptionContext),
          _useContext2 = _slicedToArray(_useContext, 2),
          subscribe = _useContext2[0],
          unsubscribe = _useContext2[1];

      var initialState = (0, _react.useMemo)(function () {
        return selector(getState());
      }, // We really want to call selector here just on component mount.
      // This provides an initial value that will be recalculated when
      // store suscription has been called.
      // eslint-disable-next-line
      []);

      var _useReducer = (0, _react.useReducer)(function (state, nextState) {
        return compareEqual(state, nextState) ? state : nextState;
      }, initialState),
          _useReducer2 = _slicedToArray(_useReducer, 2),
          selectorState = _useReducer2[0],
          setSelectorState = _useReducer2[1];
      /* eslint-disable-next-line react-hooks/exhaustive-deps */

      var selectorCallback = (0, _react.useCallback)(selector, dependencies);
      (0, _react.useEffect)(function () {
        var onStoreChange = function onStoreChange(nextState) {
          setSelectorState(selectorCallback(nextState));
        };

        setSelectorState(selectorCallback(getState()));
        subscribe(onStoreChange);
        return function () {
          unsubscribe(onStoreChange);
        };
      }, [getState, selectorCallback, subscribe, unsubscribe]);
      return selectorState;
    };
    /**
     * @see https://react-redux.js.org/api/hooks#useselector
     */

    exports.useSelectorCallback = useSelectorCallback;

    var useSelector = function useSelector(selector) {
      var compareEqual = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_COMPARE_EQUAL;
      return useSelectorCallback(selector, [], compareEqual);
    };

    exports.useSelector = useSelector;
    //# sourceMappingURL=index.js.map
  }
});
//# sourceMappingURL=index.js.map