Liferay.Loader.define("style-book-web@1.0.11/js/style-book-editor/StyleBookEditor", ['module', 'exports', 'require', 'frontend-js-web', 'frontend-js-react-web$react', './LayoutPreview', './Sidebar', './StyleBookContext', './config', './constants/draftStatusConstants', './useCloseProductMenu'], 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");

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

    var _LayoutPreview = _interopRequireDefault(require("./LayoutPreview"));

    var _Sidebar = _interopRequireDefault(require("./Sidebar"));

    var _StyleBookContext = require("./StyleBookContext");

    var _config = require("./config");

    var _draftStatusConstants = require("./constants/draftStatusConstants");

    var _useCloseProductMenu = require("./useCloseProductMenu");

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

    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 StyleBookEditor = function StyleBookEditor(_ref) {
      var initialFrontendTokensValues = _ref.frontendTokensValues,
          initialPreviewLayout = _ref.initialPreviewLayout;
      (0, _useCloseProductMenu.useCloseProductMenu)();

      var _useState = (0, _react.useState)(initialFrontendTokensValues),
          _useState2 = _slicedToArray(_useState, 2),
          frontendTokensValues = _useState2[0],
          setFrontendTokensValues = _useState2[1];

      var _useState3 = (0, _react.useState)(_draftStatusConstants.DRAFT_STATUS.notSaved),
          _useState4 = _slicedToArray(_useState3, 2),
          draftStatus = _useState4[0],
          setDraftStatus = _useState4[1];

      var _useState5 = (0, _react.useState)(initialPreviewLayout),
          _useState6 = _slicedToArray(_useState5, 2),
          previewLayout = _useState6[0],
          setPreviewLayout = _useState6[1];

      (0, _react.useEffect)(function () {
        if (frontendTokensValues === initialFrontendTokensValues) {
          return;
        }

        setDraftStatus(_draftStatusConstants.DRAFT_STATUS.saving);
        saveDraft(frontendTokensValues, _config.config.styleBookEntryId).then(function () {
          setDraftStatus(_draftStatusConstants.DRAFT_STATUS.draftSaved);
        })["catch"](function (error) {

          setDraftStatus(_draftStatusConstants.DRAFT_STATUS.notSaved);
          (0, _frontendJsWeb.openToast)({
            message: error.message,
            type: 'danger'
          });
        });
      }, [initialFrontendTokensValues, frontendTokensValues]);
      return _react["default"].createElement(_StyleBookContext.StyleBookContextProvider, {
        value: {
          draftStatus: draftStatus,
          frontendTokensValues: frontendTokensValues,
          previewLayout: previewLayout,
          setFrontendTokensValues: setFrontendTokensValues,
          setPreviewLayout: setPreviewLayout
        }
      }, _react["default"].createElement("div", {
        className: "style-book-editor"
      }, _react["default"].createElement(_LayoutPreview["default"], null), _react["default"].createElement(_Sidebar["default"], null)));
    };

    function _default() {
      var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          _ref2$frontendTokenDe = _ref2.frontendTokenDefinition,
          frontendTokenDefinition = _ref2$frontendTokenDe === void 0 ? [] : _ref2$frontendTokenDe,
          initialPreviewLayout = _ref2.initialPreviewLayout,
          namespace = _ref2.namespace,
          publishURL = _ref2.publishURL,
          layoutsTreeURL = _ref2.layoutsTreeURL,
          redirectURL = _ref2.redirectURL,
          saveDraftURL = _ref2.saveDraftURL,
          styleBookEntryId = _ref2.styleBookEntryId,
          _ref2$frontendTokensV = _ref2.frontendTokensValues,
          frontendTokensValues = _ref2$frontendTokensV === void 0 ? {} : _ref2$frontendTokensV;

      (0, _config.initializeConfig)({
        frontendTokenDefinition: frontendTokenDefinition,
        initialPreviewLayout: initialPreviewLayout,
        layoutsTreeURL: layoutsTreeURL,
        namespace: namespace,
        publishURL: publishURL,
        redirectURL: redirectURL,
        saveDraftURL: saveDraftURL,
        styleBookEntryId: styleBookEntryId
      });
      return _react["default"].createElement(StyleBookEditor, {
        frontendTokensValues: frontendTokensValues,
        initialPreviewLayout: initialPreviewLayout
      });
    }

    function saveDraft(frontendTokensValues, styleBookEntryId) {
      var _objectToFormData;

      var body = (0, _frontendJsWeb.objectToFormData)((_objectToFormData = {}, _defineProperty(_objectToFormData, "".concat(_config.config.namespace, "frontendTokensValues"), JSON.stringify(frontendTokensValues)), _defineProperty(_objectToFormData, "".concat(_config.config.namespace, "styleBookEntryId"), styleBookEntryId), _objectToFormData));
      return (0, _frontendJsWeb.fetch)(_config.config.saveDraftURL, {
        body: body,
        method: 'post'
      }).then(function (response) {
        return response.clone().json()["catch"](function () {
          return response.text();
        }).then(function (body) {
          return [response, body];
        });
      }).then(function (_ref3) {
        var _ref4 = _slicedToArray(_ref3, 2),
            response = _ref4[0],
            body = _ref4[1];

        if (response.status >= 400 || _typeof(body) !== 'object') {
          throw new Error(Liferay.Language.get('an-unexpected-error-occurred'));
        }

        if (body.error) {
          throw new Error(body.error);
        }

        return body;
      });
    }
    //# sourceMappingURL=StyleBookEditor.js.map
  }
});
//# sourceMappingURL=StyleBookEditor.js.map