"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _reactNativeCloudCa = require("react-native-cloud-ca");
var _styles = _interopRequireDefault(require("./styles"));
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 DeleteDeviceView = props => {
  const {
    buttonLabel,
    deviceId,
    onDone,
    children,
    buttonLabelStyle,
    ...rest
  } = props;
  const [isShowRequestDelete, setIsShowRequestDelete] = (0, _react.useState)(false);
  const [isShowError, setIsShowError] = (0, _react.useState)(false);
  const [isShowSuccess, setIsShowSuccess] = (0, _react.useState)(false);
  const [errorResponse, setErrorResponse] = (0, _react.useState)('');
  const [successResponse, setSuccessResponse] = (0, _react.useState)();
  const onDeleteDevice = async () => {
    handleShowRequestDelete(false);
    try {
      await (0, _reactNativeCloudCa.validateToken)();
      const deleteDeviceResult = await (0, _reactNativeCloudCa.deleteDevice)({
        deviceId: deviceId || ''
      });
      setSuccessResponse({
        deleteResponse: deleteDeviceResult
      });
      setIsShowSuccess(true);
    } catch (error) {
      setErrorResponse(`${error === null || error === void 0 ? void 0 : error.code} - ${error === null || error === void 0 ? void 0 : error.message}`);
      setIsShowError(true);
      setSuccessResponse({
        error: error
      });
    }
  };
  const handleDone = () => {
    setIsShowSuccess(false);
    onDone === null || onDone === void 0 ? void 0 : onDone(successResponse);
  };
  const handleShowRequestDelete = state => {
    setIsShowRequestDelete(state);
  };
  const renderChildren = () => {
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, children ? children : /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      style: buttonLabelStyle
    }, buttonLabel));
  };
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, _extends({
    onPress: () => handleShowRequestDelete(true)
  }, rest), renderChildren()), /*#__PURE__*/_react.default.createElement(_reactNativeCloudCa.Dialogue, {
    visible: isShowSuccess,
    onClose: handleDone
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: _styles.default.contentStyle
  }, "Hu\u1EF7 thi\u1EBFt b\u1ECB th\xE0nh c\xF4ng")), /*#__PURE__*/_react.default.createElement(_reactNativeCloudCa.Dialogue, {
    modalType: "ERROR",
    onClose: () => setIsShowError(false),
    visible: isShowError
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: _styles.default.contentStyle
  }, errorResponse)), /*#__PURE__*/_react.default.createElement(_reactNativeCloudCa.DialogueConfirm, {
    modalType: "WARNING",
    title: "Th\xF4ng b\xE1o",
    closeOnPress: () => handleShowRequestDelete(false),
    closeLabel: "Quay l\u1EA1i",
    confirmOnPress: onDeleteDevice,
    confirmLabel: "Hu\u1EF7 \u0111\u0103ng k\xFD",
    visible: isShowRequestDelete
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: _styles.default.contentStyle
  }, "B\u1EA1n c\xF3 ch\u1EAFc mu\u1ED1n hu\u1EF7 \u0111\u0103ng k\xFD tr\xEAn thi\u1EBFt b\u1ECB n\xE0y kh\xF4ng?")));
};
var _default = DeleteDeviceView;
exports.default = _default;
//# sourceMappingURL=DeleteDeviceView.js.map