import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["fillProps"];
import { forwardRef, useState } from 'react';
import { makeStyles } from '@material-ui/core/styles';
import { FormattedMessage, useIntl } from 'react-intl';
import Button from '@material-ui/core/Button';
import ReCAPTCHA from 'react-google-recaptcha';
import { useTheme } from '@material-ui/core';
import { useConfig } from './client';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var useStyles = makeStyles(function (theme) {
  return {
    recaptcha: {
      marginTop: theme.spacing(1),
      marginBottom: theme.spacing(1)
    },
    buttonContainer: {
      '& > *': {
        marginRight: theme.spacing(1),
        '@media print': {
          display: 'none'
        }
      }
    }
  };
});
export default function withRecaptcha(Form) {
  return /*#__PURE__*/forwardRef(function (_ref, ref) {
    var _otherProps$config;

    var fillProps = _ref.fillProps,
        otherProps = _objectWithoutProperties(_ref, _excluded);

    var intl = useIntl();
    var classes = useStyles();
    var theme = useTheme().palette.type;

    var _useState = useState(),
        _useState2 = _slicedToArray(_useState, 2),
        captchaChallenge = _useState2[0],
        setCaptchaChallenge = _useState2[1];

    var _useState3 = useState(),
        _useState4 = _slicedToArray(_useState3, 2),
        recaptchaComponent = _useState4[0],
        setRecaptchaComponent = _useState4[1];

    var _useState5 = useState(false),
        _useState6 = _slicedToArray(_useState5, 2),
        hideCaptcha = _useState6[0],
        setHideCaptcha = _useState6[1];

    var _useConfig = 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$config = otherProps.config) === null || _otherProps$config === void 0 ? void 0 : _otherProps$config.publicForm) && ((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__*/_jsxs(Form, _objectSpread(_objectSpread({
        ref: ref,
        fillProps: fillProps
      }, otherProps), {}, {
        captcha: captchaChallenge,
        onCloseDialog: onCloseDialog,
        children: [/*#__PURE__*/_jsx("div", {
          style: hideCaptcha ? {
            display: 'none'
          } : {},
          children: /*#__PURE__*/_jsx(ReCAPTCHA, {
            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__*/_jsxs("div", {
          className: classes.buttonContainer,
          children: [(_fillProps$actions = fillProps === null || fillProps === void 0 ? void 0 : fillProps.actions) !== null && _fillProps$actions !== void 0 ? _fillProps$actions : null, /*#__PURE__*/_jsx(Button, {
            type: "submit",
            variant: "contained",
            color: "primary",
            disabled: !captchaChallenge,
            onClick: function onClick() {
              setHideCaptcha(true);
            },
            children: /*#__PURE__*/_jsx(FormattedMessage, {
              id: "6jFMKd",
              defaultMessage: [{
                "type": 0,
                "value": "Lähetä"
              }]
            })
          })]
        })]
      }));
    } else {
      return /*#__PURE__*/_jsx(Form, _objectSpread({
        ref: ref
      }, otherProps));
    }
  });
}
//# sourceMappingURL=withRecaptcha.js.map