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, FlatList, TouchableOpacity, Image } from 'react-native';
import styles from './styles';
import { DeviceInfoView, Header, Loading, listRegisteredDevices, validateToken, CloudCAProviderContext, Dialogue } from 'react-native-cloud-ca';
import { NativeRouter, Routes, Route, Link, useLocation } from 'react-router-native';
import { DeviceItem } from './components';
const RootViewContainer = props => {
  const {
    headerProps,
    goBack,
    onDone
  } = props;
  const cloudCAProviderContext = React.useContext(CloudCAProviderContext);
  const {
    themeColor
  } = cloudCAProviderContext;
  const [listDevices, setListDevices] = useState([]);
  const [isLoading, setIsLoading] = useState(false);
  const [isShowError, setIsShowError] = useState(false);
  const [errorResponse, setErrorResponse] = useState('');
  const [allResult, setAllResult] = useState();
  let location = useLocation();
  useEffect(() => {
    fetchListRegisteredDevices();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [location]);
  const fetchListRegisteredDevices = async () => {
    setIsLoading(true);
    try {
      await validateToken();
      const result = await listRegisteredDevices();
      setListDevices(result);
      setIsLoading(false);
      setAllResult({
        ...allResult,
        listRegisteredDevicesResponse: result
      });
      onDone === null || onDone === void 0 ? void 0 : onDone(allResult);
    } catch (error) {
      setIsShowError(true);
      setErrorResponse(`${error === null || error === void 0 ? void 0 : error.code} - ${error === null || error === void 0 ? void 0 : error.message}`);
      setIsLoading(false);
      setAllResult({
        ...allResult,
        error: error
      });
      onDone === null || onDone === void 0 ? void 0 : onDone(allResult);
    }
  };
  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 thi\u1EBFt b\u1ECB \u0111\xE3 \u0111\u0103ng k\xFD."), /*#__PURE__*/React.createElement(TouchableOpacity, {
      onPress: fetchListRegisteredDevices
    }, /*#__PURE__*/React.createElement(Text, {
      style: [styles.reloadRequestText, {
        color: themeColor
      }]
    }, "T\u1EA3i l\u1EA1i y\xEAu c\u1EA7u")));
  };
  const renderItem = _ref => {
    let {
      item,
      index
    } = _ref;
    return /*#__PURE__*/React.createElement(Link, {
      to: `/deviceInfo/${item.device_id}/${item.device_name}/${item.secure_element}/${item.biometric}`,
      underlayColor: "rgba(0,0,0,.01)"
    }, /*#__PURE__*/React.createElement(DeviceItem, {
      key: index,
      deviceInfo: item
    }));
  };
  const handleError = () => {
    setIsShowError(false);
  };
  return /*#__PURE__*/React.createElement(View, {
    style: styles.container
  }, /*#__PURE__*/React.createElement(Header, _extends({}, headerProps, {
    goBack: goBack
  })), /*#__PURE__*/React.createElement(React.Fragment, null, listDevices !== null && listDevices !== void 0 && listDevices.length ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Text, {
    style: styles.numberOfDevices
  }, "Thi\u1EBFt b\u1ECB \u0111\xE3 \u0111\u0103ng k\xFD (", listDevices === null || listDevices === void 0 ? void 0 : listDevices.length, ")"), /*#__PURE__*/React.createElement(FlatList, {
    data: listDevices,
    keyExtractor: (_item, index) => index.toString(),
    renderItem: renderItem,
    style: styles.listConatiner,
    contentContainerStyle: styles.contentContainerStyle
  })) : renderEmptyRequest()), /*#__PURE__*/React.createElement(Dialogue, {
    modalType: "ERROR",
    onClose: handleError,
    visible: isShowError
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.contentStyle
  }, errorResponse)), isLoading && /*#__PURE__*/React.createElement(Loading, null));
};
const ListRegisteredDevicesView = props => {
  const renderRootView = () => {
    return /*#__PURE__*/React.createElement(RootViewContainer, {
      goBack: props === null || props === void 0 ? void 0 : props.goBack,
      headerProps: props === null || props === void 0 ? void 0 : props.headerProps
    });
  };
  const renderDeviceInfoView = () => {
    return /*#__PURE__*/React.createElement(DeviceInfoView, {
      onDone: props === null || props === void 0 ? void 0 : props.onDone
    });
  };
  return /*#__PURE__*/React.createElement(NativeRouter, null, /*#__PURE__*/React.createElement(Routes, null, /*#__PURE__*/React.createElement(Route, {
    path: "/",
    Component: renderRootView
  }), /*#__PURE__*/React.createElement(Route, {
    path: "/deviceInfo/:device_id/:device_name/:secure_element/:biometric",
    Component: renderDeviceInfoView
  })));
};
export default ListRegisteredDevicesView;
//# sourceMappingURL=ListRegisteredDevicesView.js.map