Liferay.Loader.define("commerce-frontend-js@3.0.33/utilities/forms/DDMFormHandler", ['module', 'exports', 'require', '../AJAX/index', '../eventsDefinitions', './formsHelper'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;

    var _index = _interopRequireDefault(require("../AJAX/index"));

    var _eventsDefinitions = require("../eventsDefinitions");

    var _formsHelper = require("./formsHelper");

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

    function _classCallCheck(instance, Constructor) {
      if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
      }
    }

    function _defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    function _createClass(Constructor, protoProps, staticProps) {
      if (protoProps) _defineProperties(Constructor.prototype, protoProps);if (staticProps) _defineProperties(Constructor, staticProps);return Constructor;
    }

    var DDMFormHandler = /*#__PURE__*/function () {
      function DDMFormHandler(_ref) {
        var DDMFormInstance = _ref.DDMFormInstance,
            actionURL = _ref.actionURL,
            _ref$addToCartId = _ref.addToCartId,
            addToCartId = _ref$addToCartId === void 0 ? '' : _ref$addToCartId,
            portletId = _ref.portletId;

        _classCallCheck(this, DDMFormHandler);

        this.actionURL = actionURL;
        this.addToCartId = addToCartId;
        this.DDMFormInstance = DDMFormInstance;
        this.portletId = portletId;
        this.fields = (0, _formsHelper.getDefaultFieldsShape)(DDMFormInstance);

        this._attachFormListener();

        this.checkCPInstance();
      }

      _createClass(DDMFormHandler, [{
        key: "_attachFormListener",
        value: function _attachFormListener() {
          var _this = this;

          this.DDMFormInstance.on('fieldEdited', function (field) {
            _this.fields = (0, _formsHelper.updateFields)(_this.fields, field);

            _this.checkCPInstance();
          });
        }
      }, {
        key: "checkCPInstance",
        value: function checkCPInstance() {
          var _this2 = this;

          var fieldsParam = new FormData();
          fieldsParam.append("_".concat(this.portletId, "_ddmFormValues"), JSON.stringify(this.fields));

          _index.default.POST(this.actionURL, null, {
            body: fieldsParam,
            headers: new Headers({
              'x-csrf-token': Liferay.authToken
            })
          }).then(function (cpInstance) {
            if (cpInstance.cpInstanceExist) {
              var dispatchedPayload = {
                addToCartId: _this2.addToCartId,
                cpInstance: cpInstance,
                formFields: _this2.fields
              };
              Liferay.fire(_eventsDefinitions.CP_INSTANCE_CHANGED, dispatchedPayload);
            }
          });
        }
      }]);

      return DDMFormHandler;
    }();

    Liferay.component('DDMFormHandler', function () {
      return {
        attach: function attach(configuration) {
          return new DDMFormHandler(configuration);
        }
      };
    }());
    var _default = DDMFormHandler;
    exports.default = _default;
    //# sourceMappingURL=DDMFormHandler.js.map
  }
});
//# sourceMappingURL=DDMFormHandler.js.map