"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const styles_1 = require("@mui/material/styles");
const react_intl_1 = require("react-intl");
const material_1 = require("@mui/material");
const api_services_1 = require("@selfcommunity/api-services");
const utils_1 = require("@selfcommunity/utils");
const react_core_1 = require("@selfcommunity/react-core");
const BaseDialog_1 = tslib_1.__importDefault(require("../../../shared/BaseDialog"));
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const Errors_1 = require("../../../constants/Errors");
const lab_1 = require("@mui/lab");
const constants_1 = require("../constants");
const messages = (0, react_intl_1.defineMessages)({
    name: {
        id: 'ui.incubatorListWidget.createIncubatorDialog.formPlaceholder.name',
        defaultMessage: 'ui.incubatorListWidget.createIncubatorDialog.formPlaceholder.name'
    },
    slogan: {
        id: 'ui.incubatorListWidget.createIncubatorDialog.formPlaceholder.slogan',
        defaultMessage: 'ui.incubatorListWidget.createIncubatorDialog.formPlaceholder.slogan'
    }
});
const classes = {
    root: `${constants_1.PREFIX}-create-dialog-root`,
    form: `${constants_1.PREFIX}-form`,
    name: `${constants_1.PREFIX}-name`,
    slogan: `${constants_1.PREFIX}-slogan`,
    intro: `${constants_1.PREFIX}-intro`,
    submittedMessage: `${constants_1.PREFIX}-submitted-message`
};
const Root = (0, styles_1.styled)(BaseDialog_1.default, {
    name: constants_1.PREFIX,
    slot: 'CreateDialogRoot'
})(() => ({}));
function CreateIncubatorDialog(props) {
    // PROPS
    const { className, open, onClose } = props, rest = tslib_1.__rest(props, ["className", "open", "onClose"]);
    // CONTEXT
    const scUserContext = (0, react_core_1.useSCUser)();
    // INTL
    const intl = (0, react_intl_1.useIntl)();
    // STATE
    const [name, setName] = (0, react_1.useState)('');
    const [slogan, setSlogan] = (0, react_1.useState)('');
    const [isSubmitting, setIsSubmitting] = (0, react_1.useState)(false);
    const [submitted, setSubmitted] = (0, react_1.useState)(false);
    const [error, setError] = (0, react_1.useState)('');
    // HANDLERS
    const handleSubmit = (event) => {
        event.preventDefault();
        setIsSubmitting(true);
        api_services_1.http
            .request({
            url: api_services_1.Endpoints.CreateAnIncubator.url(),
            method: api_services_1.Endpoints.CreateAnIncubator.method,
            data: {
                name: name,
                slogan: slogan
            }
        })
            .then((res) => {
            setIsSubmitting(false);
            setSubmitted(true);
        })
            .catch((error) => {
            setError(error);
            setIsSubmitting(false);
            utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error);
        });
    };
    if (!scUserContext.user) {
        return null;
    }
    let dialogContent;
    if (submitted) {
        dialogContent = ((0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: 'subtitle2', className: classes.submittedMessage }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.incubatorListWidget.createIncubatorDialog.propose.success", defaultMessage: "ui.incubatorListWidget.createIncubatorDialog.propose.success" }) })));
    }
    else {
        dialogContent = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ component: 'span', className: classes.intro }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.incubatorsListDialog.createIncubatorDialog.intro", defaultMessage: "ui.incubatorsListDialog.createIncubatorDialog.intro" }) })), (0, jsx_runtime_1.jsxs)(material_1.FormGroup, Object.assign({ className: classes.form }, { children: [(0, jsx_runtime_1.jsx)(material_1.TextField, { required: true, error: Boolean(error), className: classes.name, placeholder: `${intl.formatMessage(messages.name)}`, margin: "normal", value: name, onChange: (event) => setName(event.target.value) }), (0, jsx_runtime_1.jsx)(material_1.TextField, { multiline: true, error: Boolean(error), className: classes.slogan, placeholder: `${intl.formatMessage(messages.slogan)}`, margin: "normal", value: slogan, onChange: (event) => setSlogan(event.target.value) })] })), (0, jsx_runtime_1.jsx)(lab_1.LoadingButton, Object.assign({ loading: isSubmitting, disabled: !name || Boolean(error), variant: "contained", onClick: handleSubmit }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.incubatorListWidget.createIncubatorDialog.button", defaultMessage: "ui.incubatorListWidget.createIncubatorDialog.button" }) }))] }));
    }
    // RENDER
    return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ title: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.incubatorListWidget.createIncubatorDialog.title", defaultMessage: "ui.incubatorListWidget.createIncubatorDialog.title" }), open: open, onClose: onClose, className: (0, classnames_1.default)(classes.root, className) }, rest, { children: dialogContent })));
}
exports.default = CreateIncubatorDialog;
