import { Form } from "../../../utilities/form/Form.mjs";
import { Field } from "../../../utilities/form/Field.mjs";
import { FluentTextField } from "../../../UI/inputs/textField/FluentTextField.mjs";
import { UdpStepper } from "../../../UI/stepper/udp-stepper.mjs";
import { UserFormFields } from "./form/UserFormFields.mjs";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useSnackbar } from "notistack";
import { useTranslation } from "react-i18next";
//#region src/shell/ui/user/AddUser.jsx
const USER_EMAIL_STEP = 0;
const USER_INFO_STEP = 1;
const AddUser = (props) => {
	const { getUserByEmail, handleCreateUser, handleUpdateAndAddToTenant, rolesData, setSidesheetLoading, sidesheetLoading, altFieldMapping } = props;
	const userMapping = useMemo(() => {
		if (altFieldMapping) return altFieldMapping;
		else return {
			userId: "userId",
			firstName: "firstName",
			lastName: "lastName",
			email: "email",
			roles: "systemWideSecurityRoles"
		};
	}, [altFieldMapping]);
	const { t } = useTranslation();
	const { enqueueSnackbar } = useSnackbar();
	const [activeStep, setActiveStep] = useState(USER_EMAIL_STEP);
	const [completed, setCompleted] = useState({});
	const [existingUser, setExistingUser] = useState(null);
	const [passwordType, setPasswordType] = useState("autoGeneratePassword");
	const [creationMethod, setCreationMethod] = useState("standard");
	const handleSubmitRef = useRef(null);
	const emailFieldRef = useRef(null);
	const handlePasswordTypeChange = useCallback((event) => {
		setPasswordType(event.target.value);
	}, []);
	const handleCreationMethodChange = useCallback((event_0) => {
		setCreationMethod(event_0.target.value);
	}, []);
	const [steps, setSteps] = useState([{
		stepDisplay: {
			inProcessIcon: "1",
			completeIcon: "✓",
			title: t("Enter Email"),
			subTitle: t("Enter the email you will use to log in")
		},
		stepTitle: "Step 1",
		showTitle: "true",
		buttons: []
	}, {
		stepDisplay: {
			inProcessIcon: "2",
			completeIcon: "✓",
			title: t("User Info"),
			subTitle: t("Fill in additional info for the new user")
		},
		stepTitle: "Step 2",
		showTitle: "true",
		buttons: []
	}]);
	const goToEmailStep = useCallback(() => {
		setExistingUser(null);
		setCompleted((prev) => ({
			...prev,
			[USER_INFO_STEP]: false
		}));
		setActiveStep(USER_EMAIL_STEP);
	}, []);
	const goToInfoStep = useCallback(async () => {
		const email = emailFieldRef.current.value;
		setExistingUser(null);
		setSidesheetLoading(true);
		const user = (await getUserByEmail(email)).data;
		if (user) setExistingUser(user);
		setSidesheetLoading(false);
		setCompleted((prev_0) => ({
			...prev_0,
			[USER_EMAIL_STEP]: true
		}));
		setActiveStep(USER_INFO_STEP);
	}, [getUserByEmail, setSidesheetLoading]);
	const onSubmit = useCallback(async (values) => {
		setSidesheetLoading(true);
		if (creationMethod === "invite") values.isInvite = true;
		else values.isInvite = false;
		if (passwordType === "autoGeneratePassword") values.passwordProfile = null;
		if (values.email) values.email = values.email.toLowerCase().trim();
		try {
			if (existingUser) {
				existingUser[userMapping.firstName] = values[userMapping.firstName];
				existingUser[userMapping.lastName] = values[userMapping.lastName];
				existingUser[userMapping.roles] = values[userMapping.roles];
				await handleUpdateAndAddToTenant(existingUser[userMapping.userId], existingUser);
			} else await handleCreateUser(values);
		} catch (err) {
			enqueueSnackbar(err.message, {
				variant: "error",
				anchorOrigin: {
					vertical: "top",
					horizontal: "center"
				}
			});
		}
		setSidesheetLoading(false);
	}, [
		existingUser,
		enqueueSnackbar,
		handleCreateUser,
		handleUpdateAndAddToTenant,
		setSidesheetLoading,
		creationMethod,
		passwordType,
		userMapping
	]);
	const submitForm = useCallback(() => {
		if (handleSubmitRef.current) handleSubmitRef.current();
	}, []);
	useEffect(() => {
		setSteps((prev_1) => {
			const updated = [...prev_1];
			updated[USER_EMAIL_STEP].buttons = [{
				title: t("Next"),
				action: 2,
				variant: "outline",
				customAction: goToInfoStep
			}];
			updated[USER_INFO_STEP].buttons = [{
				title: t("Back"),
				action: 1,
				variant: "outline",
				customAction: goToEmailStep
			}, {
				title: t("Create user"),
				action: 3,
				variant: "outline",
				customAction: submitForm,
				disabled: sidesheetLoading
			}];
			return updated;
		});
	}, [
		goToInfoStep,
		goToEmailStep,
		submitForm,
		t,
		sidesheetLoading
	]);
	useEffect(() => {
		if (existingUser) setSteps((prev_2) => {
			const updated_0 = [...prev_2];
			updated_0[USER_INFO_STEP].stepDisplay.subTitle = t("A user with that email already exists. Edit values before adding to the tenant");
			updated_0[USER_INFO_STEP].buttons[1].title = t("Add user to tenant");
			return updated_0;
		});
		else setSteps((prev_3) => {
			const updated_1 = [...prev_3];
			updated_1[USER_INFO_STEP].stepDisplay.subTitle = t("Fill in additional info for the new user");
			updated_1[USER_INFO_STEP].buttons[1].title = t("Create user");
			return updated_1;
		});
	}, [existingUser, t]);
	return /* @__PURE__ */ React.createElement(Form, {
		onSubmit,
		render: ({ handleSubmit }) => {
			handleSubmitRef.current = handleSubmit;
			return /* @__PURE__ */ React.createElement(UdpStepper, {
				steps,
				activeStep,
				completed,
				orientation: "vertical",
				labelDisplay: "all",
				labelPosition: "right"
			}, activeStep === 0 && /* @__PURE__ */ React.createElement(Field, {
				ref: emailFieldRef,
				component: FluentTextField,
				label: t("Email"),
				id: "email",
				name: "email",
				fullWidth: true,
				variant: "outlined",
				margin: "normal",
				size: "small",
				required: true,
				validate: (value) => {
					if (!/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(value) || 64 < value.split("@")[0].length) return t("Invalid email address");
				},
				onKeyDown: (e) => {
					if (e.key === "Enter") {
						e.preventDefault();
						goToInfoStep();
					}
				}
			}), activeStep === 1 && /* @__PURE__ */ React.createElement(UserFormFields, {
				rolesData,
				editedUser: existingUser,
				handlePasswordTypeChange,
				passwordType,
				handleCreationMethodChange,
				creationMethod,
				hideEmailField: true,
				altFieldMapping
			}));
		}
	});
};
//#endregion
export { AddUser as default };

//# sourceMappingURL=AddUser.mjs.map