"use strict";

Object.defineProperty(exports, "__esModule", {
  value: 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 _Fab = _interopRequireDefault(require("@material-ui/core/Fab"));

var _KeyboardBackspace = _interopRequireDefault(require("@material-ui/icons/KeyboardBackspace"));

var _TextField = _interopRequireDefault(require("@material-ui/core/TextField"));

var _Grid = _interopRequireDefault(require("@material-ui/core/Grid"));

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

var _styles = require("@material-ui/core/styles");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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',
    color: 'white'
  },
  gridItem: {
    justifyContent: 'center',
    textAlign: 'center'
  },
  fab: {
    fontSize: '20pt'
  },
  button: {
    fontSize: '16pt'
  },
  textInput: {
    fontSize: '20pt'
  },
  textField: {
    paddingTop: '2em'
  }
});

const AuthCodeSource = props => {
  const inputRef = (0, _react.useRef)(null);

  const submit = () => {
    _AuthExchangeActions.default.sourceSubmit(inputRef.current.value).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) {
      if (props.handleClose) props.handleClose();
    }).catch(function (err) {
      console.error(err);
    });
  };

  const handleButtonClick = e => {
    let value = e.target.dataset.value;
    if (value === undefined) value = e.target.parentElement.dataset.value;

    if (value === 'DEL') {
      let s = inputRef.current.value;
      s = s.substring(0, s.length - 1);
      inputRef.current.value = s;
    } else if (value) {
      inputRef.current.value = inputRef.current.value + value;
    }
  };

  const {
    classes
  } = 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, "Enter the specified digits from the other device's screen."), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    container: true,
    spacing: 4,
    justify: "center",
    alignItems: "center"
  }, /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    className: classes.gridItem,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Fab.default, {
    color: "primary",
    className: classes.fab,
    autoFocus: true,
    onClick: handleButtonClick,
    "data-value": "1"
  }, "1")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    className: classes.gridItem,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Fab.default, {
    color: "primary",
    className: classes.fab,
    onClick: handleButtonClick,
    "data-value": "2"
  }, "2")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    className: classes.gridItem,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Fab.default, {
    color: "primary",
    className: classes.fab,
    onClick: handleButtonClick,
    "data-value": "3"
  }, "3")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    className: classes.gridItem,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Fab.default, {
    color: "primary",
    className: classes.fab,
    onClick: handleButtonClick,
    "data-value": "4"
  }, "4")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    className: classes.gridItem,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Fab.default, {
    color: "primary",
    className: classes.fab,
    onClick: handleButtonClick,
    "data-value": "5"
  }, "5")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    className: classes.gridItem,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Fab.default, {
    color: "primary",
    className: classes.fab,
    onClick: handleButtonClick,
    "data-value": "6"
  }, "6")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    className: classes.gridItem,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Fab.default, {
    color: "primary",
    className: classes.fab,
    onClick: handleButtonClick,
    "data-value": "7"
  }, "7")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    className: classes.gridItem,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Fab.default, {
    color: "primary",
    className: classes.fab,
    onClick: handleButtonClick,
    "data-value": "8"
  }, "8")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    className: classes.gridItem,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Fab.default, {
    color: "primary",
    className: classes.fab,
    onClick: handleButtonClick,
    "data-value": "9"
  }, "9")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    className: classes.gridItem,
    xs: 4
  }, "\xA0"), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    className: classes.gridItem,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Fab.default, {
    color: "primary",
    className: classes.fab,
    onClick: handleButtonClick,
    "data-value": "0"
  }, "0")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    className: classes.gridItem,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Fab.default, {
    color: "secondary",
    className: classes.fab,
    onClick: handleButtonClick,
    "data-value": "DEL"
  }, /*#__PURE__*/_react.default.createElement(_KeyboardBackspace.default, {
    "data-value": "DEL"
  })))), /*#__PURE__*/_react.default.createElement(_TextField.default, {
    className: classes.textField,
    InputProps: {
      className: classes.textInput
    },
    fullWidth: true,
    inputRef: inputRef
  })), /*#__PURE__*/_react.default.createElement(_DialogActions.default, null, /*#__PURE__*/_react.default.createElement(_Button.default, {
    className: classes.button,
    onClick: props.handleClose
  }, "Cancel"), /*#__PURE__*/_react.default.createElement(_Button.default, {
    className: classes.button,
    onClick: submit
  }, "Submit")));
};

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

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

exports.default = _default;