"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireDefault(require("react"));

var _modularityFront = _interopRequireDefault(require("modularity-front"));

var _reactMaterialize = require("react-materialize");

var _reactGoogleRecaptcha = _interopRequireDefault(require("react-google-recaptcha"));

var _DropdownFilter = _interopRequireDefault(require("./UserInputs/DropdownFilter"));

var _Editor = _interopRequireDefault(require("./UserInputs/Editor"));

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

function _typeof(obj) { 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 _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, 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 _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; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }

function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

var Input = _reactMaterialize.TextInput;
var Module = _modularityFront.default.Module;

var FormModule =
/*#__PURE__*/
function (_Modularity$Module) {
  _inherits(FormModule, _Modularity$Module);

  function FormModule(props) {
    var _this;

    _classCallCheck(this, FormModule);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(FormModule).call(this, props));
    _this.state = {};

    _this.FormInputs.onUpdate(function (e) {
      _this.setState(_this.updateFromInput());
    });

    _this.state = _objectSpread({}, _this.state, _this.updateFromInput());
    _this.formRef = _react.default.createRef();
    return _this;
  }

  _createClass(FormModule, [{
    key: "updateFromInput",
    value: function updateFromInput() {
      var _this2 = this;

      return Object.keys(this.FormInputs.value).reduce(function (obj, key, index) {
        return _objectSpread({}, obj, _defineProperty({}, key, _this2.FormInputs.value[key].value));
      }, {});
    }
  }, {
    key: "updateField",
    value: function updateField(key, value, filter) {
      var field = _defineProperty({}, key, value);

      var state = _objectSpread({}, this.state, field);

      this.setState(_objectSpread({}, field));
      this.formData.set(state);
    }
  }, {
    key: "isValid",
    value: function isValid() {
      console.log(this.state);

      for (var i in this.FormInputs.value) {
        var item = this.FormInputs.value[i];
        console.log(item, item.filter.required, this.state[i]);
        if (item.filter && item.filter.required && !this.state[i]) return false;
      }

      return true;
    }
  }, {
    key: "captchaComplete",
    value: function captchaComplete(token) {
      if (!token || token.legth < 10) return;
      this.setState({
        captcha: token.toString()
      });
    }
  }, {
    key: "submitData",
    value: function submitData(e) {
      e.preventDefault();
      this.onSubmit.set(this.state);
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      return _react.default.createElement("div", {
        className: " " + this.props.className
      }, _react.default.createElement("form", {
        ref: this.formRef,
        className: " ",
        onSubmit: function onSubmit(e) {
          return _this3.submitData(e);
        }
      }, _react.default.createElement("div", {
        className: "row"
      }, Object.keys(this.FormInputs.value).map(function (key, index) {
        var input = _this3.FormInputs.value[key];

        if (input) {
          if (input.filter && ["manytomany"].includes(input.filter.type)) {
            return _react.default.createElement("div", {
              className: "input-field " + input.columns,
              key: key
            }, _react.default.createElement(_DropdownFilter.default, {
              filter: input.filter,
              defaultValue: (input.value || [])[0],
              Filters: function Filters(data) {
                return data ? _this3.updateField(input.filter.field, data.values, input.filter) : null;
              }
            }));
          } else if (input.filter && ["onetoone"].includes(input.filter.type)) {
            return _react.default.createElement("div", {
              className: "input-field " + input.columns,
              key: key
            }, _react.default.createElement(_DropdownFilter.default, {
              filter: input.filter,
              defaultValue: input.value.name,
              Filters: function Filters(data) {
                return data ? _this3.updateField(input.filter.field, data.values[0], input.filter) : null;
              }
            }));
          } else if (input.filter && ["exact", "select"].includes(input.filter.type)) {
            return _react.default.createElement("div", {
              className: "input-field " + input.columns,
              key: key
            }, _react.default.createElement(_DropdownFilter.default, {
              filter: input.filter,
              defaultValue: input.value,
              Filters: function Filters(data) {
                return data ? _this3.updateField(input.filter.field, data.values[0], input.filter) : null;
              }
            }));
          } else if (input.filter && ["editor"].includes(input.filter.type)) {
            // Editor here
            return _react.default.createElement("div", {
              className: "input-field col s12",
              key: key
            }, _react.default.createElement(_Editor.default, {
              Name: input.filter.name || key,
              data: input.value,
              onChange: function onChange(data) {
                return data ? _this3.updateField(input.filter.field, data, input.filter) : null;
              }
            }));
          } else if (input.filter && ["textarea"].includes(input.filter.type)) {
            // todo textarea
            return _react.default.createElement("div", {
              className: "input-field col s12",
              key: key
            }, _react.default.createElement(_reactMaterialize.Textarea, {
              Name: input.filter.name || key,
              data: input.value,
              onChange: function onChange(data) {
                return _this3.updateField(input.filter.field, data.target.value, input.filter);
              },
              label: input.placeholder,
              noLayout: true,
              email: input.filter.email,
              password: input.filter.password,
              validate: input.filter.required,
              success: input.filter.success,
              error: input.filter.error || "Required field",
              required: input.filter.required
            }));
          } else {
            input.filter = input.filter || {};
            return _react.default.createElement("div", {
              className: input.columns,
              key: key
            }, _react.default.createElement(Input, {
              noLayout: true,
              key: key,
              value: input.value === undefined || Number.isNaN(input.value) ? input.value || '' : '' + input.value,
              onChange: function onChange(e) {
                return _this3.updateField(key, e.target.value, input.filter);
              },
              label: input.placeholder,
              email: input.filter.email,
              password: input.filter.password,
              validate: input.filter.required,
              success: input.filter.success,
              error: input.filter.error || "Required field",
              required: input.filter.required
            }));
          }
        }
      })), this.withCaptcha.value && this.CaptchaKey.value && _react.default.createElement("div", null, _react.default.createElement(_reactGoogleRecaptcha.default, {
        sitekey: this.CaptchaKey.value,
        onChange: function onChange(e) {
          return _this3.captchaComplete(e);
        }
      }), ","), this.activeButton.value ? _react.default.createElement("div", {
        className: 'row center-children'
      }, _react.default.createElement("div", {
        className: "col s" + this.button.columns
      }, _react.default.createElement(_reactMaterialize.Button, {
        type: "submit",
        className: this.button.class,
        waves: "light"
      }, this.button.value || "Submit"))) : ''), _get(_getPrototypeOf(FormModule.prototype), "render", this).call(this));
    }
  }]);

  return FormModule;
}(_modularityFront.default.Module); //[{label:"Email",type:"email",placeholder:"",columns:12,disabled:false},
//		{label:"Password",type:"password",placeholder:"",columns:12,disabled:false}]


FormModule.EditPosition = "top-right";
FormModule.Inputs = {
  FormInputs: {
    type: Module.Types.Object,
    defaultValue: {
      "email": {
        type: "email",
        placeholder: "Email",
        columns: 12
      },
      "password": {
        type: "password",
        placeholder: "Password",
        columns: 12
      }
    }
  },
  activeButton: {
    type: Module.Types.Boolean
  },
  button: {
    type: Module.Types.Text,
    defaultValue: "Submit",
    class: "empty",
    columns: 12
  },
  withCaptcha: {
    type: Module.Types.Boolean
  },
  CaptchaKey: {
    type: Module.Types.Text
  }
};
FormModule.Outputs = {
  //
  formData: {},
  onSubmit: {}
};
FormModule.Category = "sxoinas";
var _default = FormModule;
exports.default = _default;