Liferay.Loader.define("app-builder-web@1.0.58/js/pages/form-view/FormViewUpperToolbar.es", ['module', 'exports', 'require', 'data-engine-taglib', 'frontend-js-react-web$react', '../../AppContext.es', '../../components/upper-toolbar/UpperToolbar.es', '../../utils/toast.es', '../../utils/utils.es', './FormViewContext.es'], 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["default"] = void 0;

    var _dataEngineTaglib = require("data-engine-taglib");

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

    var _AppContext = require("../../AppContext.es");

    var _UpperToolbar = _interopRequireDefault(require("../../components/upper-toolbar/UpperToolbar.es"));

    var _toast = require("../../utils/toast.es");

    var _utils = require("../../utils/utils.es");

    var _FormViewContext = _interopRequireDefault(require("./FormViewContext.es"));

    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 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 _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;
    }

    var _default = function _default(_ref) {
      var newCustomObject = _ref.newCustomObject,
          showTranslationManager = _ref.showTranslationManager;

      var _useState = (0, _react.useState)(''),
          _useState2 = _slicedToArray(_useState, 2),
          defaultLanguageId = _useState2[0],
          setDefaultLanguageId = _useState2[1];

      var _useState3 = (0, _react.useState)(''),
          _useState4 = _slicedToArray(_useState3, 2),
          editingLanguageId = _useState4[0],
          setEditingLanguageId = _useState4[1];

      var _useState5 = (0, _react.useState)(false),
          _useState6 = _slicedToArray(_useState5, 2),
          isLoading = _useState6[0],
          setLoading = _useState6[1];

      var _useContext = (0, _react.useContext)(_FormViewContext["default"]),
          _useContext2 = _slicedToArray(_useContext, 2),
          state = _useContext2[0],
          dispatch = _useContext2[1];

      var dataDefinition = state.dataDefinition,
          dataDefinitionId = state.dataDefinitionId,
          dataLayout = state.dataLayout,
          initialAvailableLanguageIds = state.initialAvailableLanguageIds;
      (0, _react.useEffect)(function () {
        if (dataDefinition.defaultLanguageId) {
          setDefaultLanguageId(dataDefinition.defaultLanguageId);
          onEditingLanguageIdChange(dataDefinition.defaultLanguageId);
        }
      }, [dataDefinition.defaultLanguageId, onEditingLanguageIdChange]);

      var _useContext3 = (0, _react.useContext)(_AppContext.AppContext),
          basePortletURL = _useContext3.basePortletURL;

      var listUrl = "".concat(basePortletURL, "/#/custom-object/").concat(dataDefinitionId, "/form-views");

      var onEditingLanguageIdChange = (0, _react.useCallback)(function (editingLanguageId) {
        setEditingLanguageId(editingLanguageId);
        dispatch({
          payload: editingLanguageId,
          type: _dataEngineTaglib.DataLayoutBuilderActions.UPDATE_EDITING_LANGUAGE_ID
        });
      }, [dispatch]);

      var onError = function onError(error) {
        var title = error.title;
        (0, _toast.errorToast)(title);
      };

      var onSuccess = function onSuccess() {
        (0, _toast.successToast)(Liferay.Language.get('the-form-view-was-saved-successfully'));
        Liferay.Util.navigate(listUrl);
      };

      if (!defaultLanguageId) {
        return null;
      }

      return _react["default"].createElement(_UpperToolbar["default"], null, showTranslationManager && _react["default"].createElement(_UpperToolbar["default"].Group, null, _react["default"].createElement(_dataEngineTaglib.TranslationManager, {
        defaultLanguageId: defaultLanguageId,
        editingLanguageId: editingLanguageId,
        onEditingLanguageIdChange: onEditingLanguageIdChange,
        translatedLanguageIds: _objectSpread({}, dataLayout.name, {}, initialAvailableLanguageIds.reduce(function (acc, cur) {
          acc[cur] = cur;
          return acc;
        }, {}))
      })), _react["default"].createElement(_UpperToolbar["default"].Input, {
        onChange: function onDataLayoutNameChange(_ref2) {
          var value = _ref2.target.value;
          dispatch({
            payload: {
              name: _objectSpread({}, dataLayout.name, _defineProperty({}, editingLanguageId, value))
            },
            type: _dataEngineTaglib.DataLayoutBuilderActions.UPDATE_DATA_LAYOUT_NAME
          });
        },
        onKeyDown: function onKeyDown(event) {
          if (event.keyCode === 13) {
            event.preventDefault();
            event.target.blur();
          }
        },
        placeholder: Liferay.Language.get('untitled-form-view'),
        value: dataLayout.name[editingLanguageId] || ''
      }), _react["default"].createElement(_UpperToolbar["default"].Group, null, _react["default"].createElement(_UpperToolbar["default"].Button, {
        displayType: "secondary",
        onClick: function onCancel() {
          if (newCustomObject) {
            Liferay.Util.navigate(basePortletURL);
          } else {
            Liferay.Util.navigate(listUrl);
          }
        }
      }, Liferay.Language.get('cancel')), _react["default"].createElement(_UpperToolbar["default"].Button, {
        disabled: isLoading || !dataLayout.name[editingLanguageId] || _dataEngineTaglib.DataLayoutVisitor.isDataLayoutEmpty(dataLayout.dataLayoutPages),
        onClick: function onSave() {
          if (!dataLayout.name[defaultLanguageId]) {
            dataLayout.name[defaultLanguageId] = dataLayout.name[editingLanguageId];
          }

          dataLayout.name[defaultLanguageId] = (0, _utils.getValidName)(Liferay.Language.get('untitled-form-view'), dataLayout.name[defaultLanguageId]);
          setLoading(true);
          (0, _dataEngineTaglib.saveDataDefinition)(state).then(onSuccess)["catch"](function (error) {
            onError(error);
            setLoading(false);
          });
        }
      }, Liferay.Language.get('save'))));
    };

    exports["default"] = _default;
    //# sourceMappingURL=FormViewUpperToolbar.es.js.map
  }
});
//# sourceMappingURL=FormViewUpperToolbar.es.js.map