"use strict";

require("core-js/modules/web.dom-collections.iterator");

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 _DialogTitle = _interopRequireDefault(require("@material-ui/core/DialogTitle"));

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

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

var _Select = _interopRequireDefault(require("@material-ui/core/Select"));

var _InputLabel = _interopRequireDefault(require("@material-ui/core/InputLabel"));

var _MenuItem = _interopRequireDefault(require("@material-ui/core/MenuItem"));

var _FormControl = _interopRequireDefault(require("@material-ui/core/FormControl"));

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

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

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; }

function _extends() { _extends = Object.assign || 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 NEW = '__new__';

const styles = theme => ({
  root: {
    display: 'flex',
    flexWrap: 'wrap'
  },
  formControl: {
    margin: 0,
    width: '100%'
  },
  selectEmpty: {
    marginTop: theme.spacing(2)
  }
});

const LoginCredentials = props => {
  const value = (0, _react.useContext)(_CredentialsContext.CredentialsContext);
  const [original, creds, originalKey, setNewCurrent, updateCreds, deleteCreds] = value;
  const [credentials, setCredentials] = (0, _react.useState)(original);
  const [current, setCurrent] = (0, _react.useState)(originalKey);

  const handleClose = () => {
    if (props.handleClose) props.handleClose();
  };

  const handleSubmit = () => {
    const creds = credentials;
    if (creds.server.endsWith('/')) creds.server = creds.server.substring(0, creds.server.length - 1);
    console.log('SUBMIT', current, creds);
    updateCreds(current, creds);
    handleClose();
  };

  const handleDelete = () => {
    console.log('DELETE', current);
    deleteCreds(current);
    handleClose();
  };

  const handlePasswordKeyDown = evt => {
    if (evt.keyCode + '' === '13') {
      handleSubmit();
      return false;
    }

    return true;
  };

  const handleChangeCurrent = e => {
    setCurrent(e.target.value);
    setCredentials(creds[e.target.value] || {
      name: '',
      server: '',
      username: '',
      password: '',
      bitrate: '0'
    });
  };

  const handleChange = property => e => {
    credentials[property] = e.target.value;
    setCredentials({ ...credentials
    });
  };

  const {
    classes,
    native
  } = props;
  const isDeleteDisabled = original === current || current === NEW;

  const currentSelectorField = /*#__PURE__*/_react.default.createElement(_FormControl.default, {
    className: classes.formControl
  }, /*#__PURE__*/_react.default.createElement(_InputLabel.default, {
    htmlFor: "current-selection"
  }, "Current Selection"), /*#__PURE__*/_react.default.createElement(_Select.default, {
    native: native,
    id: "LC_current-selection",
    value: current,
    onChange: handleChangeCurrent
  }, Object.entries(creds).map(o => {
    if (native) return /*#__PURE__*/_react.default.createElement("option", {
      key: o[0],
      value: o[0]
    }, o[1].name);
    return /*#__PURE__*/_react.default.createElement(_MenuItem.default, {
      key: o[0],
      value: o[0]
    }, o[1].name);
  }), "console.warn(current, credentials);", native && /*#__PURE__*/_react.default.createElement("option", {
    key: NEW,
    value: NEW
  }, current === NEW ? credentials.name || 'New Server' : 'New Server'), !native && /*#__PURE__*/_react.default.createElement(_MenuItem.default, {
    key: NEW,
    value: NEW
  }, current === NEW ? credentials.name || 'New Server' : 'New Server')));

  let passwordValues = {};

  if (original && original.password === credentials.password) {
    passwordValues.label = 'Password: unchanged';
    passwordValues.value = '';
  } else {
    passwordValues.label = 'Password';
    passwordValues.value = credentials.password;
  }

  return /*#__PURE__*/_react.default.createElement(_Dialog.default, {
    disableBackdropClick: true,
    open: true,
    className: classes.paperFullScreen
  }, /*#__PURE__*/_react.default.createElement(_DialogTitle.default, null, 'Server Credentials'), /*#__PURE__*/_react.default.createElement(_DialogContent.default, null, currentSelectorField, /*#__PURE__*/_react.default.createElement(_TextField.default, {
    id: "LC_name",
    label: "Name",
    margin: "dense",
    fullWidth: true,
    onChange: handleChange('name'),
    value: credentials.name
  }), /*#__PURE__*/_react.default.createElement(_TextField.default, {
    id: "LC_server",
    label: "Server",
    margin: "dense",
    helperText: "http(s)://host:port",
    fullWidth: true,
    onChange: handleChange('server'),
    value: credentials.server
  }), /*#__PURE__*/_react.default.createElement(_TextField.default, {
    id: "LC_username",
    label: "Username",
    margin: "dense",
    fullWidth: true,
    onChange: handleChange('username'),
    value: credentials.username
  }), /*#__PURE__*/_react.default.createElement(_TextField.default, _extends({
    id: "LC_password",
    type: "password",
    margin: "dense",
    fullWidth: true,
    onChange: handleChange('password'),
    onKeyDown: handlePasswordKeyDown
  }, passwordValues))), /*#__PURE__*/_react.default.createElement(_DialogActions.default, null, /*#__PURE__*/_react.default.createElement(_Button.default, {
    disabled: isDeleteDisabled,
    onClick: handleDelete
  }, "Delete"), /*#__PURE__*/_react.default.createElement(_Button.default, {
    onClick: handleClose
  }, "Cancel"), /*#__PURE__*/_react.default.createElement(_Button.default, {
    color: "primary",
    onClick: handleSubmit
  }, "Submit")));
};

LoginCredentials.propTypes = {
  classes: _propTypes.default.any,
  native: _propTypes.default.bool,
  handleClose: _propTypes.default.func
};
LoginCredentials.defaultProps = {};

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

exports.default = _default;