"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _styles = _interopRequireDefault(require("./styles"));
var _reactNativeCloudCa = require("react-native-cloud-ca");
var _reactRouterNative = require("react-router-native");
var _components = require("./components");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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); }
const RootViewContainer = props => {
  const {
    headerProps,
    goBack,
    onDone
  } = props;
  const cloudCAProviderContext = _react.default.useContext(_reactNativeCloudCa.CloudCAProviderContext);
  const {
    themeColor
  } = cloudCAProviderContext;
  const [listDevices, setListDevices] = (0, _react.useState)([]);
  const [isLoading, setIsLoading] = (0, _react.useState)(false);
  const [isShowError, setIsShowError] = (0, _react.useState)(false);
  const [errorResponse, setErrorResponse] = (0, _react.useState)('');
  const [allResult, setAllResult] = (0, _react.useState)();
  let location = (0, _reactRouterNative.useLocation)();
  (0, _react.useEffect)(() => {
    fetchListRegisteredDevices();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [location]);
  const fetchListRegisteredDevices = async () => {
    setIsLoading(true);
    try {
      await (0, _reactNativeCloudCa.validateToken)();
      const result = await (0, _reactNativeCloudCa.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.default.createElement(_reactNative.View, {
      style: _styles.default.emptyContainer
    }, /*#__PURE__*/_react.default.createElement(_reactNative.Image, {
      style: _styles.default.emptyImage,
      source: require('../../asset/image/IMG_EMPTY_REQUEST.png')
    }), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      style: _styles.default.emptyText
    }, "Kh\xF4ng c\xF3 thi\u1EBFt b\u1ECB \u0111\xE3 \u0111\u0103ng k\xFD."), /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, {
      onPress: fetchListRegisteredDevices
    }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      style: [_styles.default.reloadRequestText, {
        color: themeColor
      }]
    }, "T\u1EA3i l\u1EA1i y\xEAu c\u1EA7u")));
  };
  const renderItem = _ref => {
    let {
      item,
      index
    } = _ref;
    return /*#__PURE__*/_react.default.createElement(_reactRouterNative.Link, {
      to: `/deviceInfo/${item.device_id}/${item.device_name}/${item.secure_element}/${item.biometric}`,
      underlayColor: "rgba(0,0,0,.01)"
    }, /*#__PURE__*/_react.default.createElement(_components.DeviceItem, {
      key: index,
      deviceInfo: item
    }));
  };
  const handleError = () => {
    setIsShowError(false);
  };
  return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: _styles.default.container
  }, /*#__PURE__*/_react.default.createElement(_reactNativeCloudCa.Header, _extends({}, headerProps, {
    goBack: goBack
  })), /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, listDevices !== null && listDevices !== void 0 && listDevices.length ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: _styles.default.numberOfDevices
  }, "Thi\u1EBFt b\u1ECB \u0111\xE3 \u0111\u0103ng k\xFD (", listDevices === null || listDevices === void 0 ? void 0 : listDevices.length, ")"), /*#__PURE__*/_react.default.createElement(_reactNative.FlatList, {
    data: listDevices,
    keyExtractor: (_item, index) => index.toString(),
    renderItem: renderItem,
    style: _styles.default.listConatiner,
    contentContainerStyle: _styles.default.contentContainerStyle
  })) : renderEmptyRequest()), /*#__PURE__*/_react.default.createElement(_reactNativeCloudCa.Dialogue, {
    modalType: "ERROR",
    onClose: handleError,
    visible: isShowError
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: _styles.default.contentStyle
  }, errorResponse)), isLoading && /*#__PURE__*/_react.default.createElement(_reactNativeCloudCa.Loading, null));
};
const ListRegisteredDevicesView = props => {
  const renderRootView = () => {
    return /*#__PURE__*/_react.default.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.default.createElement(_reactNativeCloudCa.DeviceInfoView, {
      onDone: props === null || props === void 0 ? void 0 : props.onDone
    });
  };
  return /*#__PURE__*/_react.default.createElement(_reactRouterNative.NativeRouter, null, /*#__PURE__*/_react.default.createElement(_reactRouterNative.Routes, null, /*#__PURE__*/_react.default.createElement(_reactRouterNative.Route, {
    path: "/",
    Component: renderRootView
  }), /*#__PURE__*/_react.default.createElement(_reactRouterNative.Route, {
    path: "/deviceInfo/:device_id/:device_name/:secure_element/:biometric",
    Component: renderDeviceInfoView
  })));
};
var _default = ListRegisteredDevicesView;
exports.default = _default;
//# sourceMappingURL=ListRegisteredDevicesView.js.map