"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.LoadingPlaceholderWorkflowType = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _Button = _interopRequireWildcard(require("../../components/buttons/Button"));
var _testable = require("../../utils/testable");
var _theme = require("../../contexts/theme");
var _reactI18next = require("react-i18next");
var _InfoTextBox = _interopRequireDefault(require("../../components/texts/InfoTextBox"));
var _InfoBox = require(".././misc/InfoBox");
var _ProgressBar = _interopRequireDefault(require("./ProgressBar"));
var _RecordLoading = _interopRequireDefault(require("../animated/RecordLoading"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const LoadingPlaceholderWorkflowType = exports.LoadingPlaceholderWorkflowType = {
  Connection: 'Connection',
  ReceiveOffer: 'ReceiveOffer',
  ProofRequested: 'ProofRequested'
};
// TODO:(jl) Add `AccessibilityInfo.announceForAccessibility(t('Connection.TakingTooLong'))
// when the timeout is triggered.

const LoadingPlaceholder = ({
  workflowType,
  timeoutDurationInMs = 10000,
  loadingProgressPercent = 0,
  onCancelTouched,
  onTimeoutTriggered,
  testID
}) => {
  const {
    ListItems,
    TextTheme
  } = (0, _theme.useTheme)();
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const timerRef = (0, _react.useRef)(null);
  const [overtime, setOvertime] = (0, _react.useState)(false);
  const styles = _reactNative.StyleSheet.create({
    container: {
      marginTop: 8,
      marginHorizontal: 20
    },
    buttonContainer: {
      marginVertical: 25
    },
    link: {
      ...ListItems.recordAttributeText,
      ...ListItems.recordLink,
      paddingVertical: 2
    },
    loadingAnimationContainer: {
      flex: 1,
      flexGrow: 1,
      borderRadius: 15
    },
    slowLoadTitle: {
      ...TextTheme.title
    },
    slowLoadBody: {
      ...TextTheme.normal
    },
    infoTextBoxContainer: {
      flexShrink: 1
    }
  });
  (0, _react.useEffect)(() => {
    if (timeoutDurationInMs === 0) {
      return;
    }
    if (timerRef.current) {
      clearTimeout(timerRef.current);
    }
    timerRef.current = setTimeout(() => {
      setOvertime(true);
      if (onTimeoutTriggered) {
        onTimeoutTriggered();
      }
    }, timeoutDurationInMs);
    return () => {
      if (timerRef.current) {
        clearTimeout(timerRef.current);
      }
    };
  }, [timeoutDurationInMs, onTimeoutTriggered]);
  const textForProgressIndication = (0, _react.useCallback)(() => {
    switch (workflowType) {
      case LoadingPlaceholderWorkflowType.Connection:
        return t('LoadingPlaceholder.Connecting');
      case LoadingPlaceholderWorkflowType.ProofRequested:
        return t('LoadingPlaceholder.ProofRequest');
      case LoadingPlaceholderWorkflowType.ReceiveOffer:
        return t('LoadingPlaceholder.CredentialOffer');
    }
  }, [workflowType, t]);
  const textForWorkflowType = (0, _react.useCallback)(() => {
    switch (workflowType) {
      case LoadingPlaceholderWorkflowType.ProofRequested:
        return t('LoadingPlaceholder.YourRequest');
      case LoadingPlaceholderWorkflowType.ReceiveOffer:
        return t('LoadingPlaceholder.YourOffer');
      default:
        return t('LoadingPlaceholder.Connecting');
    }
  }, [workflowType, t]);
  return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    testID: testID ?? (0, _testable.testIdWithKey)('LoadingPlaceholder')
  }, loadingProgressPercent > 0 && /*#__PURE__*/_react.default.createElement(_ProgressBar.default, {
    progressPercent: loadingProgressPercent
  }), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.container
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.label, {
      textAlign: 'center',
      fontWeight: 'normal'
    }]
  }, textForProgressIndication()), /*#__PURE__*/_react.default.createElement(_reactNative.View, null, overtime && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_InfoTextBox.default, {
    type: _InfoBox.InfoBoxType.Info,
    style: {
      marginTop: 20
    }
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.infoTextBoxContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [styles.slowLoadTitle, {
      fontWeight: 'bold',
      marginBottom: 10
    }],
    testID: (0, _testable.testIdWithKey)('SlowLoadTitle')
  }, t('LoadingPlaceholder.SlowLoadingTitle')), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [styles.slowLoadBody],
    testID: (0, _testable.testIdWithKey)('SlowLoadBody')
  }, t('LoadingPlaceholder.SlowLoadingBody'))))), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.normal, {
      marginTop: 25,
      marginBottom: 10
    }]
  }, textForWorkflowType()), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.loadingAnimationContainer
  }, /*#__PURE__*/_react.default.createElement(_RecordLoading.default, null), /*#__PURE__*/_react.default.createElement(_RecordLoading.default, {
    style: {
      marginTop: 10
    }
  }))), onCancelTouched && /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.buttonContainer
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    title: t('Global.Cancel'),
    accessibilityLabel: t('Global.Cancel'),
    testID: (0, _testable.testIdWithKey)('Cancel'),
    buttonType: _Button.ButtonType.Primary,
    onPress: onCancelTouched
  }))));
};
var _default = exports.default = LoadingPlaceholder;
//# sourceMappingURL=LoadingPlaceholder.js.map