"use strict";

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 _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactFlexboxGrid = require("react-flexbox-grid");

var _filter = require("./filter");

var _DynamicComponent = require("../components/DynamicComponent");

var _mui = _interopRequireDefault(require("../config/mui"));

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

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

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

var LIBMap = {
  MUI: {
    map: _mui["default"]
  }
};
var customguid = 'modeldialog';

var getFieldValue = function getFieldValue() {
  var a = arguments.length <= 0 ? undefined : arguments[0];
  var type = a.type;
  var value = null;

  switch (type) {
    case 'textfield':
      value = arguments.length <= 2 ? undefined : arguments[2];
      break;

    case 'selectfield':
      value = arguments.length <= 3 ? undefined : arguments[3];
      break;

    case 'toggle':
      value = arguments.length <= 2 ? undefined : arguments[2];
      break;

    case 'autocomplete':
      value = arguments.length <= 1 ? undefined : arguments[1];
      break;

    case 'datetimepicker':
      value = arguments.length <= 1 ? undefined : arguments[1];
      break;

    case 'datepicker':
      value = arguments.length <= 1 ? undefined : arguments[1];
      break;

    case 'timepicker':
      value = arguments.length <= 1 ? undefined : arguments[1];
      break;

    case 'radio':
      value = arguments.length <= 2 ? undefined : arguments[2];
      break;

    case 'checkbox':
      value = arguments.length <= 2 ? undefined : arguments[2];
      break;

    case 'textfieldaddform':
      value = arguments.length <= 2 ? undefined : arguments[2];
      break;

    case 'selectfieldaddform':
      value = arguments.length <= 3 ? undefined : arguments[3];
      break;

    default:
      value = '';
  }

  return value;
};

var state = {};
state[customguid] = {};

var handleData = function handleData(guid) {
  for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
    args[_key - 1] = arguments[_key];
  }

  var controls = args[0];
  state[guid][controls.id] = getFieldValue.apply(void 0, args);
};

var SelectFieldCreateDialog = /*#__PURE__*/function (_React$Component) {
  _inherits(SelectFieldCreateDialog, _React$Component);

  var _super = _createSuper(SelectFieldCreateDialog);

  function SelectFieldCreateDialog(props) {
    var _this;

    _classCallCheck(this, SelectFieldCreateDialog);

    _this = _super.call(this, props);
    _this.state = {};
    state[customguid] = {};
    _this.saveDialog = _this.saveDialog.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(SelectFieldCreateDialog, [{
    key: "saveDialog",
    value: function saveDialog() {
      var props = this.props;
      props.handleSave(state.modeldialog);
      state[customguid] = {};
    }
  }, {
    key: "render",
    value: function render() {
      var props = this.props;
      var open = props.open,
          model = props.model,
          library = props.library,
          handleClose = props.handleClose;
      var DIALOG = library.Dialog;
      var DIALOGTITLE = library.DialogTitle;
      var DIALOGCONTENT = library.DialogContent;
      var DIVIDER = library.Divider;
      var BUTTON = library.Button;
      var DIALOGACTIONS = library.DialogActions;
      var data = model.data,
          dialog = model.dialog,
          dialogtitle = model.dialogtitle,
          dialogcontent = model.dialogcontent;
      var layout = (0, _filter.generateLayout)(data);
      var config = LIBMap.MUI;
      config.modules = library;
      return /*#__PURE__*/_react["default"].createElement(DIALOG, _extends({
        open: open,
        onClose: handleClose,
        "aria-labelledby": "alert-dialog-title",
        "aria-describedby": "alert-dialog-description"
      }, dialog), /*#__PURE__*/_react["default"].createElement(DIALOGTITLE, _extends({}, dialogtitle, {
        id: "alert-dialog-title"
      }), dialogtitle.title), /*#__PURE__*/_react["default"].createElement(DIVIDER, null), /*#__PURE__*/_react["default"].createElement(DIALOGCONTENT, dialogcontent, /*#__PURE__*/_react["default"].createElement("div", null, layout.wrows.map(function (row, i) {
        return /*#__PURE__*/_react["default"].createElement(_reactFlexboxGrid.Row, {
          key: i
        }, row.map(function (field, index) {
          return /*#__PURE__*/_react["default"].createElement(_reactFlexboxGrid.Col, {
            xs: field.layout.xs ? field.layout.xs.col : 6,
            sm: field.layout.sm ? field.layout.sm.col : 6,
            md: field.layout.md ? field.layout.md.col : 6,
            lg: field.layout.lg ? field.layout.lg.col : 6,
            style: field.style,
            className: "".concat(field.className, " ").concat(field.visible === false ? 'hidden' : 'show'),
            key: index
          }, /*#__PURE__*/_react["default"].createElement(_DynamicComponent.DynamicComponent, {
            component: config.map[field.type].type,
            map: config.map[field.type].map,
            option: config.map[field.type].options ? config.map[field.type].options.type : '',
            control: field,
            library: config.modules,
            attributes: field.props,
            rules: field.rules,
            formatter: field.formatter,
            onChange: function onChange() {
              for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
                args[_key2] = arguments[_key2];
              }

              handleData.apply(void 0, [customguid].concat(args));

              if (typeof props.onChange === 'function') {
                props.onChange.apply(props, args);
              }
            },
            onBlur: props.onBlur,
            onFocus: props.onFocus,
            onCheck: function onCheck() {
              for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
                args[_key3] = arguments[_key3];
              }

              handleData.apply(void 0, [customguid].concat(args));

              if (typeof props.onCheck === 'function') {
                props.onCheck.apply(props, args);
              }
            },
            onToggle: function onToggle() {
              for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
                args[_key4] = arguments[_key4];
              }

              handleData.apply(void 0, [customguid].concat(args));

              if (typeof props.onToggle === 'function') {
                props.onToggle.apply(props, args);
              }
            },
            onShow: props.onShow,
            onDismiss: props.onDismiss,
            onClick: props.onClick,
            onUpdateInput: function onUpdateInput() {
              for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
                args[_key5] = arguments[_key5];
              }

              handleData.apply(void 0, [customguid].concat(args));

              if (typeof props.onUpdateInput === 'function') {
                props.onUpdateInput.apply(props, args);
              }
            },
            onNewRequest: props.onNewRequest,
            filter: props.filter
          }));
        }));
      }))), /*#__PURE__*/_react["default"].createElement(DIVIDER, null), /*#__PURE__*/_react["default"].createElement(DIALOGACTIONS, null, /*#__PURE__*/_react["default"].createElement(BUTTON, {
        onClick: handleClose,
        color: "primary"
      }, "Cancel"), /*#__PURE__*/_react["default"].createElement(BUTTON, {
        onClick: this.saveDialog,
        color: "primary",
        autoFocus: true
      }, "Save")));
    }
  }]);

  return SelectFieldCreateDialog;
}(_react["default"].Component);

SelectFieldCreateDialog.propTypes = process.env.NODE_ENV !== "production" ? {
  open: _propTypes["default"].bool,
  library: _propTypes["default"].object,
  handleClose: _propTypes["default"].func,
  model: _propTypes["default"].object,
  handleSave: _propTypes["default"].func,
  onChange: _propTypes["default"].func,
  onBlur: _propTypes["default"].func,
  onClick: _propTypes["default"].func,
  onCheck: _propTypes["default"].func,
  onToggle: _propTypes["default"].func,
  onShow: _propTypes["default"].func,
  onFocus: _propTypes["default"].func,
  onDismiss: _propTypes["default"].func,
  onNewRequest: _propTypes["default"].func,
  filter: _propTypes["default"].func,
  onUpdateInput: _propTypes["default"].func
} : {};
SelectFieldCreateDialog.defaultProps = {
  open: false,
  library: null,
  handleClose: null,
  model: {},
  handleSave: null,
  onChange: null,
  onBlur: null,
  onClick: null,
  onCheck: null,
  onToggle: null,
  onShow: null,
  onFocus: null,
  onDismiss: null,
  onNewRequest: null,
  filter: null,
  onUpdateInput: null
};
var _default = SelectFieldCreateDialog;
exports["default"] = _default;