"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Dialog = _interopRequireDefault(require("@material-ui/core/Dialog"));

var _DialogActions = _interopRequireDefault(require("@material-ui/core/DialogActions"));

var _DialogContent = _interopRequireDefault(require("@material-ui/core/DialogContent"));

var _DialogContentText = _interopRequireDefault(require("@material-ui/core/DialogContentText"));

var _DialogTitle = _interopRequireDefault(require("@material-ui/core/DialogTitle"));

var _Button = _interopRequireDefault(require("@material-ui/core/Button"));

var _Typography = _interopRequireDefault(require("@material-ui/core/Typography"));

var _AuthExchangeActions = _interopRequireDefault(require("subfirelib/api/AuthExchangeActions"));

var _CredentialsContext = require("subfirelib/hooks/contexts/CredentialsContext");

var _Tea = _interopRequireDefault(require("subfirelib/utils/Tea"));

var _styles = require("@material-ui/core/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; }

const styles = theme => ({
  centerHeader: {
    textAlign: 'center',
    fontSize: '40pt',
    letterSpacing: '.5ch'
  }
});

const AuthCodeClient = props => {
  const [digits] = (0, _react.useState)(_AuthExchangeActions.default.allocateDigits());
  const [isActive, setIsActive] = (0, _react.useState)(true);
  const value = (0, _CredentialsContext.useCredentials)();
  const [original, creds, originalKey, setNewCurrent, updateCreds, deleteCreds, importCreds] = value; // eslint-disable-line

  console.log('digits', digits);
  (0, _react.useEffect)(() => {
    _AuthExchangeActions.default.clientRequest(digits).then(function (response, err) {
      if (response) console.log(response.ok);
      if (err) console.error(err);
      if (response.ok) return response.json();
      return response.json();
    }).then(function (j) {
      console.log('back', j);

      if (j.status === 200) {
        // if parse failure, let the exception fire
        let data = _Tea.default.decrypt(j.data, digits);

        console.log(data);
        data = JSON.parse(data);
        console.log(data);
        data = JSON.parse(data.configs);
        console.log(data); // CredentialsActions.importCredentials(configs);

        console.log('are we active?', isActive);

        if (isActive) {
          importCreds(data);
        }

        if (props.handleClose) props.handleClose();
      }
    }).catch(function (err) {
      console.error(err);
      if (props.handleClose) props.handleClose();
    });

    return () => {
      // unmounting
      setIsActive(false);
    };
  }, []); // eslint-disable-line

  const {
    classes,
    handleClose
  } = props;
  return /*#__PURE__*/_react.default.createElement(_Dialog.default, {
    disableBackdropClick: true,
    open: true,
    className: classes.paperFullScreen
  }, /*#__PURE__*/_react.default.createElement(_DialogTitle.default, null, 'Authenticate Via Existing Source'), /*#__PURE__*/_react.default.createElement(_DialogContent.default, null, /*#__PURE__*/_react.default.createElement(_DialogContentText.default, null, "On another device where you are already authenticated, enter the following ", (digits + '').length, " digits."), /*#__PURE__*/_react.default.createElement(_Typography.default, {
    variant: "h1",
    className: classes.centerHeader
  }, digits)), /*#__PURE__*/_react.default.createElement(_DialogActions.default, null, /*#__PURE__*/_react.default.createElement(_Button.default, {
    onClick: handleClose
  }, "Cancel")));
};

AuthCodeClient.propTypes = {
  classes: _propTypes.default.any,
  handleClose: _propTypes.default.func
};

var _default = (0, _styles.withStyles)(styles)(AuthCodeClient);

exports.default = _default;