Liferay.Loader.define("layout-content-page-editor-web@2.0.166/page_editor/app/thunks/addFragment", ['module', 'exports', 'require', '../actions/addFragmentEntryLinks', '../config/constants/fragmentTypes', '../services/FragmentService'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = addFragment;

    var _addFragmentEntryLinks = _interopRequireDefault(require("../actions/addFragmentEntryLinks"));

    var _fragmentTypes = require("../config/constants/fragmentTypes");

    var _FragmentService = _interopRequireDefault(require("../services/FragmentService"));

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

    /**
     * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
     * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
     */
    function addFragment(_ref) {
      var fragmentEntryKey = _ref.fragmentEntryKey,
          groupId = _ref.groupId,
          parentItemId = _ref.parentItemId,
          position = _ref.position,
          _ref$selectItem = _ref.selectItem,
          selectItem = _ref$selectItem === void 0 ? function () {} : _ref$selectItem,
          store = _ref.store,
          type = _ref.type;
      return function (dispatch) {
        var segmentsExperienceId = store.segmentsExperienceId;
        var params = {
          fragmentEntryKey: fragmentEntryKey,
          groupId: groupId,
          onNetworkStatus: dispatch,
          parentItemId: parentItemId,
          position: position,
          segmentsExperienceId: segmentsExperienceId,
          type: type
        };

        var updateState = function updateState(fragmentEntryLinks, layoutData, itemId) {
          dispatch((0, _addFragmentEntryLinks.default)({
            addedItemId: itemId,
            fragmentEntryLinks: fragmentEntryLinks,
            layoutData: layoutData
          }));
          selectItem(itemId);
        };

        if (type === _fragmentTypes.FRAGMENT_TYPES.composition) {
          _FragmentService.default.addFragmentEntryLinks(params).then(function (_ref2) {
            var addedItemId = _ref2.addedItemId,
                fragmentEntryLinks = _ref2.fragmentEntryLinks,
                layoutData = _ref2.layoutData;
            updateState(Object.values(fragmentEntryLinks), layoutData, addedItemId);
          });
        } else {
          _FragmentService.default.addFragmentEntryLink(params).then(function (_ref3) {
            var addedItemId = _ref3.addedItemId,
                fragmentEntryLink = _ref3.fragmentEntryLink,
                layoutData = _ref3.layoutData;
            updateState([fragmentEntryLink], layoutData, addedItemId);
          });
        }
      };
    }
    //# sourceMappingURL=addFragment.js.map
  }
});
//# sourceMappingURL=addFragment.js.map