"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = withRecaptcha;

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

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

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

var _react = require("react");

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

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

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

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

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

var _client = require("./client");

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

var _excluded = ["fillProps", "onError"];
var useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    recaptcha: {
      marginTop: theme.spacing(1),
      marginBottom: theme.spacing(1)
    },
    buttonContainer: {
      '& > *': {
        marginRight: theme.spacing(1),
        '@media print': {
          display: 'none'
        }
      }
    }
  };
});

function withRecaptcha(Form) {
  return /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
    var _otherProps$config;

    var fillProps = _ref.fillProps,
        _onError = _ref.onError,
        otherProps = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
    var intl = (0, _reactIntl.useIntl)();
    var classes = useStyles();
    var theme = (0, _core.useTheme)().palette.type;

    var _useState = (0, _react.useState)(),
        _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
        captchaChallenge = _useState2[0],
        setCaptchaChallenge = _useState2[1];

    var _useState3 = (0, _react.useState)(),
        _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
        recaptchaComponent = _useState4[0],
        setRecaptchaComponent = _useState4[1];

    var _useState5 = (0, _react.useState)(false),
        _useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
        hideCaptcha = _useState6[0],
        setHideCaptcha = _useState6[1];

    var _useConfig = (0, _client.useConfig)(),
        recaptcha = _useConfig.recaptcha;

    var onCloseDialog = function onCloseDialog() {
      setCaptchaChallenge(undefined);
      setHideCaptcha(false);
      recaptchaComponent === null || recaptchaComponent === void 0 ? void 0 : recaptchaComponent.reset();
    };

    if (!(otherProps === null || otherProps === void 0 ? void 0 : otherProps.noReCaptcha) && (otherProps === null || otherProps === void 0 ? void 0 : (_otherProps$config = otherProps.config) === null || _otherProps$config === void 0 ? void 0 : _otherProps$config.requireCaptcha) && ((otherProps === null || otherProps === void 0 ? void 0 : otherProps.isLastStep) === true || (otherProps === null || otherProps === void 0 ? void 0 : otherProps.isLastStep) === undefined)) {
      var _intl$locale, _fillProps$actions;

      return /*#__PURE__*/(0, _jsxRuntime.jsxs)(Form, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
        ref: ref,
        fillProps: fillProps
      }, otherProps), {}, {
        onError: function onError(props) {
          setHideCaptcha(false);

          _onError(props);
        },
        captcha: captchaChallenge,
        onCloseDialog: onCloseDialog,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          style: hideCaptcha ? {
            display: 'none'
          } : {},
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactGoogleRecaptcha["default"], {
            ref: function ref(e) {
              return setRecaptchaComponent(e);
            },
            sitekey: recaptcha.sitekey,
            theme: theme,
            hl: (_intl$locale = intl.locale) === null || _intl$locale === void 0 ? void 0 : _intl$locale.split('-')[0],
            onChange: setCaptchaChallenge,
            className: classes.recaptcha
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: classes.buttonContainer,
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
            type: "submit",
            variant: "contained",
            color: "primary",
            disabled: !captchaChallenge,
            onClick: function onClick() {
              return setHideCaptcha(true);
            },
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactIntl.FormattedMessage, {
              id: "6jFMKd",
              defaultMessage: [{
                "type": 0,
                "value": "Lähetä"
              }]
            })
          }), (_fillProps$actions = fillProps === null || fillProps === void 0 ? void 0 : fillProps.actions) !== null && _fillProps$actions !== void 0 ? _fillProps$actions : null]
        })]
      }));
    } else {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(Form, (0, _objectSpread2["default"])({
        ref: ref,
        onError: _onError
      }, otherProps));
    }
  });
}
//# sourceMappingURL=withRecaptcha.cjs.map