Liferay.Loader.define("layout-admin-web@4.0.136/js/AddLayout", ['module', 'exports', 'require', './checkFriendlyURL'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = _default;

    var _checkFriendlyURL = require("./checkFriendlyURL");

    /**
     * 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
     */

    /**
     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
     *
     * This library is free software; you can redistribute it and/or modify it under
     * the terms of the GNU Lesser General Public License as published by the Free
     * Software Foundation; either version 2.1 of the License, or (at your option)
     * any later version.
     *
     * This library is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
     * details.
     */
    function _default(_ref) {
      var getFriendlyURLWarningURL = _ref.getFriendlyURLWarningURL,
          namespace = _ref.namespace,
          shouldCheckFriendlyURL = _ref.shouldCheckFriendlyURL;
      var addButton = document.getElementById("".concat(namespace, "addButton"));
      var form = document.getElementById("".concat(namespace, "fm"));
      form.addEventListener('submit', function (event) {
        event.preventDefault();
        event.stopPropagation();

        if (addButton.disabled) {
          return;
        }

        addButton.disabled = true;
        var formData = new FormData(form);

        if (shouldCheckFriendlyURL) {
          (0, _checkFriendlyURL.checkFriendlyURL)(getFriendlyURLWarningURL, formData).then(function (response) {
            if (!response.shouldSubmit) {
              addButton.disabled = false;
              return;
            }

            Liferay.Util.fetch(form.action, {
              body: formData,
              method: 'POST'
            }).then(function (response) {
              return response.json();
            }).then(function (response) {
              if (response.redirectURL) {
                var redirectURL = new URL(response.redirectURL, window.location.origin);
                redirectURL.searchParams.set('p_p_state', 'normal');
                var opener = Liferay.Util.getOpener();
                opener.Liferay.fire('closeModal', {
                  id: "".concat(namespace, "addLayoutDialog"),
                  redirect: redirectURL.toString()
                });
              } else {
                Liferay.Util.openToast({
                  message: response.errorMessage,
                  type: 'danger'
                });
                addButton.disabled = false;
              }
            });
          });
        } else {
          Liferay.Util.fetch(form.action, {
            body: formData,
            method: 'POST'
          }).then(function (response) {
            return response.json();
          }).then(function (response) {
            if (response.redirectURL) {
              var redirectURL = new URL(response.redirectURL, window.location.origin);
              redirectURL.searchParams.set('p_p_state', 'normal');
              var opener = Liferay.Util.getOpener();
              opener.Liferay.fire('closeModal', {
                id: "".concat(namespace, "addLayoutDialog"),
                redirect: redirectURL.toString()
              });
            } else {
              Liferay.Util.openToast({
                message: response.errorMessage,
                type: 'danger'
              });
              addButton.disabled = false;
            }
          });
        }
      });
    }
    //# sourceMappingURL=AddLayout.js.map
  }
});
//# sourceMappingURL=AddLayout.js.map