"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _reactIntl = require("react-intl");

var _Button = _interopRequireDefault(require("@material-ui/core/Button"));

var _styles = require("@material-ui/core/styles");

var _extendType = _interopRequireDefault(require("./_extendType"));

var _dummy2 = _interopRequireDefault(require("./_dummy"));

var _react = require("react");

var _Typography = _interopRequireDefault(require("@material-ui/core/Typography"));

var _jsxRuntime = require("react/jsx-runtime");

var useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    button: {
      marginBottom: '15px'
    }
  };
});

function ButtonWidget(props) {
  var _options$element, _options$element$meta, _options$element$meta2, _options$element2, _options$element3, _options$element4, _props$schema, _props$schema2, _options$element5, _options$element5$met, _props$schema3;

  var classes = useStyles();
  var options = props.options,
      onChange = props.onChange;

  var _useState = (0, _react.useState)(props.value),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      value = _useState2[0],
      setValue = _useState2[1];

  var successText = (_options$element = options.element) === null || _options$element === void 0 ? void 0 : (_options$element$meta = _options$element.meta) === null || _options$element$meta === void 0 ? void 0 : (_options$element$meta2 = _options$element$meta.buttonActionProps) === null || _options$element$meta2 === void 0 ? void 0 : _options$element$meta2.successText;
  var buttonText = (options === null || options === void 0 ? void 0 : (_options$element2 = options.element) === null || _options$element2 === void 0 ? void 0 : _options$element2.useLabel) ? options === null || options === void 0 ? void 0 : (_options$element3 = options.element) === null || _options$element3 === void 0 ? void 0 : _options$element3.label : options === null || options === void 0 ? void 0 : (_options$element4 = options.element) === null || _options$element4 === void 0 ? void 0 : _options$element4.title;
  var onClickAction = ((_props$schema = props.schema) === null || _props$schema === void 0 ? void 0 : _props$schema.buttonActions) && ((_props$schema2 = props.schema) === null || _props$schema2 === void 0 ? void 0 : _props$schema2.buttonActions[(_options$element5 = options.element) === null || _options$element5 === void 0 ? void 0 : (_options$element5$met = _options$element5.meta) === null || _options$element5$met === void 0 ? void 0 : _options$element5$met.buttonOnClickAction]);

  var onClick = /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var _options$element6, _options$element6$met, response, _options$element7;

      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!onClickAction) {
                _context.next = 10;
                break;
              }

              _context.t0 = onClickAction;

              if (!_context.t0) {
                _context.next = 6;
                break;
              }

              _context.next = 5;
              return onClickAction((_options$element6 = options.element) === null || _options$element6 === void 0 ? void 0 : (_options$element6$met = _options$element6.meta) === null || _options$element6$met === void 0 ? void 0 : _options$element6$met.buttonActionProps);

            case 5:
              _context.t0 = _context.sent;

            case 6:
              response = _context.t0;

              if (response === true) {
                setValue(true);
              }

              _context.next = 11;
              break;

            case 10:
              if (options.element.onClickAction) {
                options === null || options === void 0 ? void 0 : (_options$element7 = options.element) === null || _options$element7 === void 0 ? void 0 : _options$element7.onClickAction();
              }

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function onClick() {
      return _ref.apply(this, arguments);
    };
  }();

  (0, _react.useEffect)(function () {
    setTimeout(function () {
      onChange(value);
    });
  }, [value]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    children: value === true && successText ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Typography["default"], {
      children: successText
    }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
      variant: "contained",
      color: "Primary",
      size: "small",
      value: value,
      disabled: props === null || props === void 0 ? void 0 : (_props$schema3 = props.schema) === null || _props$schema3 === void 0 ? void 0 : _props$schema3.disabled,
      className: classes.button,
      onClick: onClick,
      children: buttonText
    })
  });
}

var _default = (0, _extendType["default"])(_dummy2["default"], function (props) {
  return function () {
    var _props$fillProps;

    return {
      schema: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
        type: 'boolean'
      }, props.buttonActions === undefined ? {
        "default": false
      } : undefined), {}, {
        buttonActions: props.buttonActions,
        disabled: props === null || props === void 0 ? void 0 : (_props$fillProps = props.fillProps) === null || _props$fillProps === void 0 ? void 0 : _props$fillProps.disableElementButtons
      }),
      uiSchema: {
        'ui:widget': ButtonWidget
      }
    };
  };
});

exports["default"] = _default;
var name = (0, _reactIntl.defineMessage)({
  id: "+To+Xi",
  defaultMessage: [{
    "type": 0,
    "value": "Nappi"
  }]
});
exports.name = name;
var elementType = 'field';
exports.elementType = elementType;
//# sourceMappingURL=button.cjs.map