import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { defineMessage } from 'react-intl';
import Button from '@material-ui/core/Button';
import { makeStyles } from '@material-ui/core/styles';
import extendType from './_extendType';
import _dummy from './_dummy';
import { useEffect, useState } from 'react';
import Typography from '@material-ui/core/Typography';
import { jsx as _jsx } from "react/jsx-runtime";
var useStyles = 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 = useState(props.value),
      _useState2 = _slicedToArray(_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 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var _options$element6, _options$element6$met;

      var response;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.t0 = onClickAction;

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

              _context.next = 4;
              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 4:
              _context.t0 = _context.sent;

            case 5:
              response = _context.t0;

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

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

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

  useEffect(function () {
    setTimeout(function () {
      onChange(value);
    });
  }, [value]);
  return /*#__PURE__*/_jsx("div", {
    children: value === true && successText ? /*#__PURE__*/_jsx(Typography, {
      children: successText
    }) : /*#__PURE__*/_jsx(Button, {
      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
    })
  });
}

export default extendType(_dummy, function (props) {
  return function () {
    var _props$fillProps;

    return {
      schema: _objectSpread(_objectSpread({
        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
      }
    };
  };
});
export var name = defineMessage({
  id: "+To+Xi",
  defaultMessage: [{
    "type": 0,
    "value": "Nappi"
  }]
});
export var elementType = 'field';
//# sourceMappingURL=button.js.map