"use strict";

exports.__esModule = true;
exports.default = CreateIncubatorDialog;

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

var _styles = require("@mui/material/styles");

var _reactIntl = require("react-intl");

var _material = require("@mui/material");

var _core = require("@selfcommunity/core");

var _BaseDialog = _interopRequireDefault(require("../../../shared/BaseDialog"));

var _classnames = _interopRequireDefault(require("classnames"));

var _useThemeProps = _interopRequireDefault(require("@mui/material/styles/useThemeProps"));

var _Errors = require("../../../constants/Errors");

var _lab = require("@mui/lab");

const _excluded = ["className", "open", "onClose"];

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

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

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

const messages = (0, _reactIntl.defineMessages)({
  name: {
    id: 'ui.incubatorsList.createIncubatorDialog.formPlaceholder.name',
    defaultMessage: 'ui.incubatorsList.createIncubatorDialog.formPlaceholder.name'
  },
  slogan: {
    id: 'ui.incubatorsList.createIncubatorDialog.formPlaceholder.slogan',
    defaultMessage: 'ui.incubatorsList.createIncubatorDialog.formPlaceholder.slogan'
  }
});
const PREFIX = 'SCCreateIncubatorDialog';
const classes = {
  root: `${PREFIX}-root`,
  form: `${PREFIX}-form`,
  name: `${PREFIX}-name`,
  slogan: `${PREFIX}-slogan`,
  intro: `${PREFIX}-intro`,
  submittedMessage: `${PREFIX}-submitted-message`
};
const Root = (0, _styles.styled)(_BaseDialog.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  margin: 2,
  [theme.breakpoints.down(500)]: {
    minWidth: 300
  },
  [`& .${classes.intro}`]: {
    whiteSpace: 'pre-line'
  },
  [`& .${classes.form}`]: {
    marginTop: theme.spacing(1),
    marginBottom: theme.spacing(1)
  },
  [`& .${classes.name}`]: {
    '& .MuiInputBase-root': {
      height: '40px'
    }
  },
  [`& .${classes.submittedMessage}`]: {
    padding: theme.spacing(1),
    borderRadius: '8px',
    backgroundColor: '#bdd5bd'
  }
}));

function CreateIncubatorDialog(inProps) {
  // PROPS
  const props = (0, _useThemeProps.default)({
    props: inProps,
    name: PREFIX
  });

  const {
    className,
    open,
    onClose
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // CONTEXT


  const scUserContext = (0, _core.useSCUser)(); // INTL

  const intl = (0, _reactIntl.useIntl)(); // STATE

  const [name, setName] = (0, _react.useState)('');
  const [slogan, setSlogan] = (0, _react.useState)('');
  const [isSubmitting, setIsSubmitting] = (0, _react.useState)(false);
  const [submitted, setSubmitted] = (0, _react.useState)(false);
  const [error, setError] = (0, _react.useState)(''); // HANDLERS

  const handleSubmit = event => {
    event.preventDefault();
    setIsSubmitting(true);

    _core.http.request({
      url: _core.Endpoints.CreateAnIncubator.url(),
      method: _core.Endpoints.CreateAnIncubator.method,
      data: {
        name: name,
        slogan: slogan
      }
    }).then(res => {
      setIsSubmitting(false);
      setSubmitted(true);
    }).catch(error => {
      setError((0, _core.formatHttpError)(error));
      setIsSubmitting(false);

      _core.Logger.error(_Errors.SCOPE_SC_UI, error);
    });
  };

  if (!scUserContext.user) {
    return null;
  }

  let dialogContent;

  if (submitted) {
    dialogContent = /*#__PURE__*/_react.default.createElement(_material.Typography, {
      variant: 'subtitle2',
      className: classes.submittedMessage
    }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.incubatorsList.createIncubatorDialog.propose.success",
      defaultMessage: "ui.incubatorsList.createIncubatorDialog.propose.success"
    }));
  } else {
    dialogContent = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_material.Typography, {
      component: 'span',
      className: classes.intro
    }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.incubatorsList.createIncubatorDialog.intro",
      defaultMessage: "ui.incubatorsList.createIncubatorDialog.intro"
    })), /*#__PURE__*/_react.default.createElement(_material.FormGroup, {
      className: classes.form
    }, /*#__PURE__*/_react.default.createElement(_material.TextField, {
      required: true,
      error: Boolean(error),
      className: classes.name,
      placeholder: `${intl.formatMessage(messages.name)}`,
      margin: "normal",
      value: name,
      onChange: event => setName(event.target.value)
    }), /*#__PURE__*/_react.default.createElement(_material.TextField, {
      multiline: true,
      error: Boolean(error),
      className: classes.slogan,
      placeholder: `${intl.formatMessage(messages.slogan)}`,
      margin: "normal",
      value: slogan,
      onChange: event => setSlogan(event.target.value)
    })), /*#__PURE__*/_react.default.createElement(_lab.LoadingButton, {
      loading: isSubmitting,
      disabled: !name || Boolean(error),
      variant: "contained",
      onClick: handleSubmit
    }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.incubatorsList.createIncubatorDialog.button",
      defaultMessage: "ui.incubatorsList.createIncubatorDialog.button"
    })));
  } // RENDER


  return /*#__PURE__*/_react.default.createElement(Root, _extends({
    title: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.incubatorsList.createIncubatorDialog.title",
      defaultMessage: "ui.incubatorsList.createIncubatorDialog.title"
    }),
    open: open,
    onClose: onClose,
    className: (0, _classnames.default)(classes.root, className)
  }, rest), dialogContent);
}