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, useEffect } from 'react';
import { View, Text, Image } from 'react-native';
import { Dialogue, DialogueConfirm, Header, InfoField, Loading, OutlineButton, PrimaryButton, authorisationPendingRequest, cancelPendingRequest, decodeRequestBase64, getPendingAuthorisationRequest, ListDocumentView, CloudCAProviderContext } from 'react-native-cloud-ca';
import styles from './styles';
import { Link, NativeRouter, Route, Routes } from 'react-router-native';
const GetPendingAuthorisationRequestView = props => {
  const {
    headerProps,
    authorisationPendingInfo,
    goBack
  } = props;
  const cloudCAProviderContext = React.useContext(CloudCAProviderContext);
  const {
    themeColor
  } = cloudCAProviderContext;
  const [isLoading, setIsLoading] = useState(false);
  const [isShowRequest, setIsShowRequest] = useState(false);
  const [isShowError, setIsShowError] = useState(false);
  const [isShowSuccess, setIsShowSuccess] = useState(false);
  const [errorResponse, setErrorResponse] = useState('');
  const [pendingAuthoriastion, setPendingAuthoriastion] = useState();
  const [requestType, setRequestType] = useState('Author');
  const [requestDecoded, setRequestDecoded] = useState();
  const [authorTimeleft, setAuthorTimeleft] = useState('');
  useEffect(() => {
    fetchPendingAuthorisation();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  const fetchPendingAuthorisation = async () => {
    setIsLoading(true);
    try {
      var _authorRequest$Author, _authorRequest$Author2;
      const result = await getPendingAuthorisationRequest();
      setPendingAuthoriastion(result);
      const authorRequest = await decodeRequestBase64(result === null || result === void 0 ? void 0 : result.request);
      setRequestDecoded(authorRequest);
      getTimeleft(authorRequest === null || authorRequest === void 0 ? void 0 : (_authorRequest$Author = authorRequest.AuthorisationData) === null || _authorRequest$Author === void 0 ? void 0 : (_authorRequest$Author2 = _authorRequest$Author.ValidityPeriod) === null || _authorRequest$Author2 === void 0 ? void 0 : _authorRequest$Author2.ValidTo);
      setIsLoading(false);
    } catch (error) {
      setIsShowError(true);
      setErrorResponse(error === null || error === void 0 ? void 0 : error.message);
      setIsLoading(false);
    }
  };
  const getTimeleft = validTo => {
    const now = new Date().getTime();
    const endtime = new Date(validTo).getTime();
    const secondsLeft = (endtime - now) / 1000;
    if (secondsLeft > 0) {
      const hours = Math.floor(secondsLeft / 3600);
      const minutes = Math.floor(secondsLeft % 3600 / 60);
      const seconds = Math.floor(secondsLeft % 60);
      const hourString = hours < 10 ? '0' + hours : hours;
      const minuteString = minutes < 10 ? '0' + minutes : minutes;
      const secondString = seconds < 10 ? '0' + seconds : seconds;
      const timeLeft = `${hourString}:${minuteString}:${secondString}`;
      setAuthorTimeleft(timeLeft);
    } else {
      setAuthorTimeleft('Đã hết hạn');
    }
  };
  const authorisationRequest = async () => {
    setIsLoading(true);
    try {
      const params = {
        biometricApiType: authorisationPendingInfo === null || authorisationPendingInfo === void 0 ? void 0 : authorisationPendingInfo.biometricApiType,
        hashAlgorithm: authorisationPendingInfo === null || authorisationPendingInfo === void 0 ? void 0 : authorisationPendingInfo.hashAlgorithm,
        localizedReason: authorisationPendingInfo === null || authorisationPendingInfo === void 0 ? void 0 : authorisationPendingInfo.localizedReason,
        request: authorisationPendingInfo === null || authorisationPendingInfo === void 0 ? void 0 : authorisationPendingInfo.request,
        transactionID: authorisationPendingInfo === null || authorisationPendingInfo === void 0 ? void 0 : authorisationPendingInfo.transactionID
      };
      await authorisationPendingRequest(params);
      setIsLoading(false);
      setIsShowSuccess(true);
    } catch (error) {
      setIsShowError(true);
      setErrorResponse(error === null || error === void 0 ? void 0 : error.message);
      setIsLoading(false);
    }
  };
  const cancelRequest = async () => {
    setIsLoading(true);
    try {
      const params = {
        hashAlgorithm: authorisationPendingInfo === null || authorisationPendingInfo === void 0 ? void 0 : authorisationPendingInfo.hashAlgorithm,
        request: authorisationPendingInfo === null || authorisationPendingInfo === void 0 ? void 0 : authorisationPendingInfo.request,
        transactionID: authorisationPendingInfo === null || authorisationPendingInfo === void 0 ? void 0 : authorisationPendingInfo.transactionID
      };
      await cancelPendingRequest(params);
      setIsLoading(false);
      setIsShowSuccess(true);
    } catch (error) {
      setIsShowError(true);
      setErrorResponse(error === null || error === void 0 ? void 0 : error.message);
      setIsLoading(false);
    }
  };
  const handleCancelRequest = () => {
    setRequestType('Cancel');
    setIsShowRequest(true);
  };
  const handleAcceptRequest = () => {
    setRequestType('Author');
    setIsShowRequest(true);
  };
  const onConfirmOnPress = () => {
    if (requestType === 'Author') {
      authorisationRequest();
    } else if (requestType === 'Cancel') {
      cancelRequest();
    }
  };
  const handleDone = () => {
    setIsShowSuccess(false);
  };
  const renderDocumentItem = (item, index) => {
    return /*#__PURE__*/React.createElement(Text, {
      numberOfLines: 1,
      key: index,
      style: styles.documentItemText
    }, `${index + 1}. ${item === null || item === void 0 ? void 0 : item.Name}`);
  };
  const renderListDocuments = () => {
    var _requestDecoded$Autho, _requestDecoded$Autho2, _documents$slice;
    const documents = requestDecoded === null || requestDecoded === void 0 ? void 0 : (_requestDecoded$Autho = requestDecoded.AuthorisationData) === null || _requestDecoded$Autho === void 0 ? void 0 : (_requestDecoded$Autho2 = _requestDecoded$Autho.Documents) === null || _requestDecoded$Autho2 === void 0 ? void 0 : _requestDecoded$Autho2.Document;
    return /*#__PURE__*/React.createElement(View, {
      style: styles.listDocumentContainer
    }, documents === null || documents === void 0 ? void 0 : (_documents$slice = documents.slice(0, 3)) === null || _documents$slice === void 0 ? void 0 : _documents$slice.map((item, index) => renderDocumentItem(item, index)), documents && (documents === null || documents === void 0 ? void 0 : documents.length) > 3 && /*#__PURE__*/React.createElement(Link, {
      to: `/listDocumentView/${JSON.stringify(documents)}`,
      underlayColor: "rgba(0,0,0,.01)"
    }, /*#__PURE__*/React.createElement(Text, {
      numberOfLines: 1,
      style: styles.viewMoreText
    }, "Xem th\xEAm")));
  };
  const renderDocuments = () => {
    var _requestDecoded$Autho3, _requestDecoded$Autho4, _Name;
    const document = requestDecoded === null || requestDecoded === void 0 ? void 0 : (_requestDecoded$Autho3 = requestDecoded.AuthorisationData) === null || _requestDecoded$Autho3 === void 0 ? void 0 : (_requestDecoded$Autho4 = _requestDecoded$Autho3.Documents) === null || _requestDecoded$Autho4 === void 0 ? void 0 : _requestDecoded$Autho4.Document;
    if (document !== null && document !== void 0 && document.length) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, renderListDocuments());
    } else if (document !== null && document !== void 0 && (_Name = document.Name) !== null && _Name !== void 0 && _Name.length) {
      const singleDocument = document;
      return /*#__PURE__*/React.createElement(InfoField, {
        title: "T\xEAn t\xE0i li\u1EC7u",
        info: singleDocument === null || singleDocument === void 0 ? void 0 : singleDocument.Name
      });
    } else {
      return /*#__PURE__*/React.createElement(View, null);
    }
  };
  const renderRequestInfo = () => {
    var _requestDecoded$Autho5;
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(View, null, renderDocuments(), /*#__PURE__*/React.createElement(InfoField, {
      title: "Ng\u01B0\u1EDDi chia s\u1EBB",
      info: requestDecoded === null || requestDecoded === void 0 ? void 0 : (_requestDecoded$Autho5 = requestDecoded.AuthorisationData) === null || _requestDecoded$Autho5 === void 0 ? void 0 : _requestDecoded$Autho5.OriginatorID
    }), /*#__PURE__*/React.createElement(InfoField, {
      title: "Th\u1EDDi gian x\xE1c th\u1EF1c k\xFD c\xF2n l\u1EA1i",
      info: authorTimeleft
    })), /*#__PURE__*/React.createElement(View, {
      style: styles.buttonContainer
    }, /*#__PURE__*/React.createElement(OutlineButton, {
      label: "Hu\u1EF7 y\xEAu c\u1EA7u",
      style: styles.cancelButton,
      onPress: handleCancelRequest
    }), /*#__PURE__*/React.createElement(PrimaryButton, {
      label: "U\u1EF7 quy\u1EC1n",
      style: styles.confirmButton,
      onPress: handleAcceptRequest
    })));
  };
  const renderEmptyRequest = () => {
    return /*#__PURE__*/React.createElement(View, {
      style: styles.emptyContainer
    }, /*#__PURE__*/React.createElement(Image, {
      style: styles.emptyImage,
      source: require('../../asset/image/IMG_EMPTY_REQUEST.png')
    }), /*#__PURE__*/React.createElement(Text, {
      style: styles.emptyText
    }, "Kh\xF4ng c\xF3 y\xEAu c\u1EA7u c\u1EA7n k\xFD. "), /*#__PURE__*/React.createElement(Text, {
      style: [styles.reloadRequestText, {
        color: themeColor
      }]
    }, "T\u1EA3i l\u1EA1i y\xEAu c\u1EA7u"));
  };
  const rootViewContainer = () => {
    return /*#__PURE__*/React.createElement(View, {
      style: styles.container
    }, /*#__PURE__*/React.createElement(Header, _extends({}, headerProps, {
      label: "U\u1EF7 quy\u1EC1n x\xE1c th\u1EF1c",
      goBack: goBack
    })), /*#__PURE__*/React.createElement(View, {
      style: styles.contentContainer
    }, !pendingAuthoriastion ? renderRequestInfo() : renderEmptyRequest()), /*#__PURE__*/React.createElement(Dialogue, {
      visible: isShowSuccess,
      onClose: handleDone
    }, /*#__PURE__*/React.createElement(Text, {
      style: styles.contentStyle
    }, requestType === 'Author' ? 'Bạn đã uỷ quyền xác thực thành công.' : 'Bạn đã huỷ yêu cầu xác thực thành công.')), /*#__PURE__*/React.createElement(Dialogue, {
      modalType: "ERROR",
      onClose: () => setIsShowError(false),
      visible: isShowError
    }, /*#__PURE__*/React.createElement(Text, {
      style: styles.contentStyle
    }, errorResponse)), /*#__PURE__*/React.createElement(DialogueConfirm, {
      title: requestType === 'Cancel' ? 'Huỷ yêu cầu xác thực' : 'Uỷ quyền xác thực',
      closeOnPress: () => setIsShowRequest(false),
      closeLabel: "B\u1ECF qua",
      confirmOnPress: onConfirmOnPress,
      confirmLabel: "Ti\u1EBFp t\u1EE5c",
      visible: isShowRequest
    }, /*#__PURE__*/React.createElement(Text, {
      style: styles.contentStyle
    }, requestType === 'Cancel' ? 'Bạn có chắc chắn muốn huỷ yêu cầu xác thực không?' : 'Bạn có chắc chắn muốn uỷ quyền xác thực cho yêu cầu này không?')), isLoading && /*#__PURE__*/React.createElement(Loading, null));
  };
  return /*#__PURE__*/React.createElement(NativeRouter, null, /*#__PURE__*/React.createElement(Routes, null, /*#__PURE__*/React.createElement(Route, {
    path: "/",
    Component: rootViewContainer
  }), /*#__PURE__*/React.createElement(Route, {
    path: "/listDocumentView/:documents",
    Component: ListDocumentView
  })));
};
export default GetPendingAuthorisationRequestView;
//# sourceMappingURL=GetPendingAuthorisationRequestView.js.map