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, Platform } from 'react-native';
import { CloudCAProviderContext, Dialogue, DialogueConfirm, Loading, authenticateClient, authenticateUser, registerDevice, verifyOTP } from 'react-native-cloud-ca';
import styles from './styles';
const MAX_LENGTH_OTP = 6;
const DeviceRegistrationView = props => {
  const {
    buttonLabel,
    onDone,
    children,
    registerDeviceParams,
    buttonLabelStyle,
    ...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();
  const [isOtpError, setIsOtpError] = useState(false);
  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();
      setAllResult({
        ...allResult,
        authenticateClientResponse: authenticateClientResult
      });
      await getOtp();
      handleShowRequestRegister(false);
      setIsLoading(false);
    } catch (error) {
      setIsOtpError(false);
      setAllResult({
        ...allResult,
        error: error
      });
      setErrorResponse(`${error === null || error === void 0 ? void 0 : error.code} - ${error === null || error === void 0 ? void 0 : error.message}`);
      handleShowRequestRegister(false);
      setIsShowError(true);
      setIsLoading(false);
    }
  };
  const getOtp = async () => {
    try {
      const authenticateUserResult = await authenticateUser();
      if ((authenticateUserResult === null || authenticateUserResult === void 0 ? void 0 : authenticateUserResult.auth_type) === 'OTP') {
        setOtpTimeLeft(120);
        setIsShowOtp(true);
      }
      setAllResult({
        ...allResult,
        authenticateUserResponse: authenticateUserResult
      });
    } catch (error) {
      setIsOtpError(false);
      setAllResult({
        ...allResult,
        error: error
      });
      setErrorResponse(`${error === null || error === void 0 ? void 0 : error.code} - ${error === null || error === void 0 ? void 0 : error.message}`);
      setIsShowError(true);
    }
  };
  const onConfirmOtp = async () => {
    try {
      const otpResult = await verifyOTP({
        otpSms: otpSms,
        otpMail: '',
        biometricApiType: registerDeviceParams === null || registerDeviceParams === void 0 ? void 0 : registerDeviceParams.biometricApiType
      });
      setOtpSms('');
      setIsShowOtp(false);
      if (Platform.OS === 'android') {
        setSuccessResponse('Đã đăng ký thiết bị thành công');
        setIsShowSuccess(true);
        const androidOtpResult = otpResult;
        setAllResult({
          ...allResult,
          registerDeviceResponse: androidOtpResult === null || androidOtpResult === void 0 ? void 0 : androidOtpResult.registerDeviceResponse,
          verifyOTPResponse: androidOtpResult === null || androidOtpResult === void 0 ? void 0 : androidOtpResult.verifyOTPResponse
        });
      } else {
        setAllResult({
          ...allResult,
          verifyOTPResponse: otpResult
        });
        await onRegisterDevices();
      }
    } catch (error) {
      setIsOtpError(true);
      setOtpSms('');
      setAllResult({
        ...allResult,
        error: error
      });
      setErrorResponse(`${error === null || error === void 0 ? void 0 : error.code} - ${error === null || error === void 0 ? void 0 : error.message}`);
      setIsShowError(true);
      setIsShowOtp(false);
    }
  };
  const onRegisterDevices = async () => {
    setIsLoading(true);
    try {
      const result = await registerDevice(registerDeviceParams);
      setSuccessResponse('Đã đăng ký thiết bị thành công');
      setIsShowSuccess(true);
      setAllResult({
        ...allResult,
        registerDeviceResponse: result
      });
      setIsLoading(false);
    } catch (error) {
      setIsLoading(false);
      setIsOtpError(false);
      setAllResult({
        ...allResult,
        error: error
      });
      setErrorResponse(`${error === null || error === void 0 ? void 0 : error.code} - ${error === null || error === void 0 ? void 0 : error.message}`);
      setIsShowError(true);
    }
  };
  const handleDone = () => {
    setIsShowSuccess(false);
    onDone === null || onDone === void 0 ? void 0 : onDone(allResult);
  };
  const handleError = () => {
    setIsShowError(false);
    if (isOtpError) {
      setIsOtpError(false);
      setIsShowOtp(true);
    }
    onDone === null || onDone === void 0 ? void 0 : onDone(allResult);
  };
  const renderChildren = () => {
    return /*#__PURE__*/React.createElement(React.Fragment, null, children ? children : /*#__PURE__*/React.createElement(Text, {
      style: buttonLabelStyle
    }, buttonLabel));
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(TouchableOpacity, _extends({
    onPress: () => handleShowRequestRegister(true)
  }, rest), renderChildren()), isLoading && /*#__PURE__*/React.createElement(Loading, null), /*#__PURE__*/React.createElement(Dialogue, {
    visible: isShowSuccess,
    onClose: handleDone
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.contentStyle
  }, successResponse)), /*#__PURE__*/React.createElement(Dialogue, {
    modalType: "ERROR",
    onClose: handleError,
    visible: isShowError,
    animationType: isOtpError ? 'none' : 'fade'
  }, /*#__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",
    animationType: isOtpError ? 'none' : 'fade',
    visible: isShowOtp,
    closeLabel: "B\u1ECF qua",
    closeOnPress: () => {
      setOtpSms('');
      setIsShowOtp(false);
    },
    confirmLabel: "Ti\u1EBFp t\u1EE5c",
    confirmOnPress: onConfirmOtp,
    confirmDisable: (otpSms === null || otpSms === void 0 ? void 0 : otpSms.length) < MAX_LENGTH_OTP || isLoading
  }, /*#__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',
    maxLength: MAX_LENGTH_OTP
  }), /*#__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