function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
import React, { useState } from 'react';
import { TouchableOpacity, Text, TextInput, View } from 'react-native';
import { CloudCAProviderContext, Dialogue, DialogueConfirm, authenticateClient, authenticateUser, verifyOTP } from 'react-native-cloud-ca';
import styles from './styles';
const DeviceRegistrationView = props => {
  const {
    buttonLabel,
    clientId,
    clientSecret,
    grantType,
    userId,
    onDone,
    ...rest
  } = props;
  const cloudCAProviderContext = React.useContext(CloudCAProviderContext);
  const {
    themeColor
  } = cloudCAProviderContext;
  const [isShowRequestRegister, setIsShowRequestRegister] = useState(false);
  const [isShowError, setIsShowError] = useState(false);
  const [isShowSuccess, setIsShowSuccess] = useState(false);
  const [isShowOtp, setIsShowOtp] = useState(false);
  const [isLoading, setIsLoading] = useState(false);
  const [errorResponse, setErrorResponse] = useState('');
  const [successResponse, setSuccessResponse] = useState('');
  const [otpSms, setOtpSms] = useState('');
  const [otpTimeLeft, setOtpTimeLeft] = React.useState(0);
  const [allResult, setAllResult] = useState();
  React.useEffect(() => {
    if (otpTimeLeft === 0) return;
    const intervalId = setInterval(() => {
      setOtpTimeLeft(prevTime => prevTime > 0 ? prevTime - 1 : 0);
    }, 1000);
    return () => clearInterval(intervalId);
  }, [otpTimeLeft]);
  const handleShowRequestRegister = state => {
    setIsShowRequestRegister(state);
  };
  const onRegister = async () => {
    setIsLoading(true);
    try {
      const authenticateClientResult = await authenticateClient({
        clientId: clientId,
        clientSecret: clientSecret,
        grantType: grantType
      });
      setAllResult({
        ...allResult,
        authenticateClientResponse: authenticateClientResult
      });
      await getOtp();
      handleShowRequestRegister(false);
      setIsLoading(false);
    } catch (error) {
      console.log('error', error);
      setErrorResponse(error === null || error === void 0 ? void 0 : error.message);
      handleShowRequestRegister(false);
      setIsShowError(true);
      setIsLoading(false);
    }
  };
  const getOtp = async () => {
    try {
      const authenticateUserResult = await authenticateUser({
        userId: userId
      });
      if ((authenticateUserResult === null || authenticateUserResult === void 0 ? void 0 : authenticateUserResult.auth_type) === 'OTP') {
        setOtpTimeLeft(120);
        setIsShowOtp(true);
      }
      setAllResult({
        ...allResult,
        authenticateUserResponse: authenticateUserResult
      });
    } catch (error) {
      console.log('error', error);
      setErrorResponse(error === null || error === void 0 ? void 0 : error.message);
      setIsShowError(true);
    }
  };
  const onConfirmOtp = async () => {
    try {
      const otpResult = await verifyOTP({
        otpSms: otpSms,
        otpMail: ''
      });
      setAllResult({
        ...allResult,
        verifyOTPResponse: otpResult
      });
      setSuccessResponse('Đã đăng ký thiết bị thành công');
      setIsShowOtp(false);
      setIsShowSuccess(true);
    } catch (error) {
      setErrorResponse(error === null || error === void 0 ? void 0 : error.message);
      setIsShowError(true);
      setIsShowOtp(false);
    }
  };
  const handleDone = () => {
    setIsShowSuccess(false);
    onDone === null || onDone === void 0 ? void 0 : onDone(allResult);
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(TouchableOpacity, _extends({
    onPress: () => handleShowRequestRegister(true)
  }, rest), /*#__PURE__*/React.createElement(Text, null, buttonLabel)), /*#__PURE__*/React.createElement(Dialogue, {
    visible: isShowSuccess,
    onClose: handleDone
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.contentStyle
  }, successResponse)), /*#__PURE__*/React.createElement(Dialogue, {
    modalType: "ERROR",
    onClose: () => setIsShowError(false),
    visible: isShowError
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.contentStyle
  }, errorResponse)), /*#__PURE__*/React.createElement(DialogueConfirm, {
    title: "\u0110\u0103ng k\xFD thi\u1EBFt b\u1ECB",
    closeOnPress: () => handleShowRequestRegister(false),
    closeLabel: "B\u1ECF qua",
    confirmOnPress: onRegister,
    confirmLabel: "Ti\u1EBFp t\u1EE5c",
    confirmDisable: isLoading,
    visible: isShowRequestRegister
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.contentStyle
  }, "B\u1EA1n c\xF3 mu\u1ED1n \u0111\u0103ng k\xFD thi\u1EBFt b\u1ECB n\xE0y \u0111\u1EC3 u\u1EF7 quy\u1EC1n t\u1EEB xa")), /*#__PURE__*/React.createElement(DialogueConfirm, {
    title: "X\xE1c th\u1EF1c ng\u01B0\u1EDDi d\xF9ng",
    visible: isShowOtp,
    closeLabel: "B\u1ECF qua",
    closeOnPress: () => setIsShowOtp(false),
    confirmLabel: "Ti\u1EBFp t\u1EE5c",
    confirmOnPress: onConfirmOtp,
    confirmDisable: !((otpSms === null || otpSms === void 0 ? void 0 : otpSms.length) > 0)
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.contentStyle
  }, "Nh\u1EADp m\xE3 x\xE1c th\u1EF1c \u0111\u01B0\u1EE3c g\u1EEDi v\u1EC1 s\u1ED1 \u0111i\u1EC7n tho\u1EA1i \u0111\xE3 \u0111\u0103ng k\xFD"), /*#__PURE__*/React.createElement(Text, {
    style: styles.otpLabel
  }, "OTP"), /*#__PURE__*/React.createElement(View, {
    style: styles.otpContainer
  }, /*#__PURE__*/React.createElement(TextInput, {
    value: otpSms,
    onChangeText: setOtpSms,
    style: styles.otpInputContainer,
    keyboardType: "number-pad",
    selectionColor: '#121517'
  }), /*#__PURE__*/React.createElement(TouchableOpacity, {
    disabled: otpTimeLeft !== 0,
    onPress: getOtp
  }, /*#__PURE__*/React.createElement(Text, {
    style: [styles.resendText, {
      color: themeColor
    }]
  }, "G\u1EEDi l\u1EA1i (", otpTimeLeft, "s)")))));
};
export default DeviceRegistrationView;
//# sourceMappingURL=DeviceRegistrationView.js.map