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

    var _frontendJsWeb = require("frontend-js-web");

    /**
     * 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 getRedirectEntryChainCauseURL = _ref.getRedirectEntryChainCauseURL,
          initialDestinationURL = _ref.initialDestinationURL,
          initialIsPermanent = _ref.initialIsPermanent,
          namespace = _ref.namespace;
      var form = document["".concat(namespace, "fm")];
      form.addEventListener('submit', function saveRedirectEntry() {
        var destinationURL = form.elements["".concat(namespace, "destinationURL")];
        var sourceURL = form.elements["".concat(namespace, "sourceURL")];

        if (!sourceURL.value) {
          sourceURL.focus();
        } else if (!destinationURL.value || destinationURL.closest('.form-group').classList.contains('has-error')) {
          destinationURL.focus();
          destinationURL.blur();
        } else {
          Liferay.Util.fetch(Liferay.Util.PortletURL.createResourceURL(getRedirectEntryChainCauseURL, {
            destinationURL: destinationURL.value,
            sourceURL: sourceURL.value
          })).then(function (response) {
            return response.json();
          }).then(function (response) {
            if (response.redirectEntryChainCause) {
              showModal(response.redirectEntryChainCause);
            } else {
              submitForm(form);
            }
          }).catch(function () {
            (0, _frontendJsWeb.openToast)({
              message: Liferay.Language.get('an-unexpected-error-occurred'),
              type: 'danger'
            });
          });
        }
      });
      var typeInfoAlert = document.getElementById("".concat(namespace, "typeInfoAlert"));
      var destinationURLInput = document.getElementById("".concat(namespace, "destinationURL"));
      var permanentSelect = document.getElementById("".concat(namespace, "permanent"));

      if (typeInfoAlert && initialIsPermanent) {
        destinationURLInput.addEventListener('input', showTypeInfoAlert);
        permanentSelect.addEventListener('input', showTypeInfoAlert);
      }

      function showTypeInfoAlert() {
        typeInfoAlert.classList.toggle('hide', permanentSelect.value === 'true' && destinationURLInput.value === initialDestinationURL);
      }

      function showModal(redirectEntryChainCause) {
        Liferay.componentReady("".concat(namespace, "RedirectsChainedRedirections")).then(function (ChainedRedirections) {
          ChainedRedirections.open(redirectEntryChainCause, function (updateChainedRedirectEntries) {
            Liferay.Util.postForm(form, {
              data: {
                updateChainedRedirectEntries: updateChainedRedirectEntries
              }
            });
          });
        });
      }
    }
    //# sourceMappingURL=editRedirectEntry.js.map
  }
});
//# sourceMappingURL=editRedirectEntry.js.map