"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Connection = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactRedux = require("react-redux");
var _ui = require("@redux-devtools/ui");
var _actions = require("../../actions");
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 && Object.prototype.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 defaultSchema = {
  type: 'object',
  required: [],
  properties: {
    type: {
      title: 'Connection settings (for getting reports and remote debugging)',
      type: 'string',
      enum: ['disabled', 'custom'],
      enumNames: ['no remote connection', 'use local (custom) server']
    },
    hostname: {
      type: 'string'
    },
    port: {
      type: 'number'
    },
    secure: {
      type: 'boolean'
    }
  }
};
const uiSchema = {
  type: {
    'ui:widget': 'radio'
  }
};
class Connection extends _react.Component {
  setFormData = (type, changed) => {
    let schema;
    if (type !== 'custom') {
      schema = {
        type: 'object',
        properties: {
          type: defaultSchema.properties.type
        }
      };
    } else {
      schema = defaultSchema;
    }
    return {
      formData: {
        type,
        ...this.props.options
      },
      type,
      schema,
      changed
    };
  };
  state = this.setFormData(this.props.type);
  shouldComponentUpdate(nextProps, nextState) {
    return this.state !== nextState;
  }
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (this.props.options !== nextProps.options) {
      this.setState({
        formData: {
          ...nextProps.options,
          type: nextProps.type
        }
      });
    }
  }
  handleSave = data => {
    this.props.saveSettings(data.formData);
    this.setState({
      changed: false
    });
  };
  handleChange = data => {
    const formData = data.formData;
    const type = formData.type;
    if (type !== this.state.type) {
      this.setState(this.setFormData(type, true));
    } else if (!this.state.changed) {
      this.setState({
        changed: true,
        formData
      });
    }
  };
  render() {
    const type = this.state.type || 'disabled';
    const changed = this.state.changed;
    const disabled = type === 'disabled';
    return /*#__PURE__*/_react.default.createElement(_ui.Container, null, /*#__PURE__*/_react.default.createElement(_ui.Form, {
      primaryButton: changed,
      noSubmit: disabled && !changed,
      submitText: disabled ? 'Disconnect' : 'Connect',
      formData: this.state.formData,
      schema: this.state.schema,
      uiSchema: uiSchema,
      onChange: this.handleChange,
      onSubmit: this.handleSave
    }));
  }
}
exports.Connection = Connection;
const mapStateToProps = state => state.connection;
const actionCreators = {
  saveSettings: _actions.saveSocketSettings
};
var _default = exports.default = (0, _reactRedux.connect)(mapStateToProps, actionCreators)(Connection);