import { Field } from "../../../../utilities/form/Field.mjs";
import { useUser } from "../../../../utilities/auth/useUser.mjs";
import { FluentTextField } from "../../../../UI/inputs/textField/FluentTextField.mjs";
import { FluentCheckbox } from "../../../../UI/inputs/checkbox/FluentCheckbox.mjs";
import PasswordValidator, { isPasswordValid } from "../PasswordValidator.mjs";
import { UserRoleAutoComplete } from "./UserRoleAutoComplete.mjs";
import { FluentRadioGroup } from "../../../../UI/inputs/radio/FluentRadioGroup.mjs";
import { FluentRadio } from "../../../../UI/inputs/radio/FluentRadio.mjs";
import { c } from "react-compiler-runtime";
import React, { useState } from "react";
import { FormControlLabel, FormLabel, IconButton, InputAdornment, makeStyles } from "@material-ui/core";
import { useTranslation } from "react-i18next";
import { Visibility, VisibilityOff } from "@material-ui/icons";
//#region src/shell/ui/user/form/UserFormFields.jsx
const useStyles = makeStyles((theme) => ({
	field: { marginBottom: theme.spacing(2) },
	selectors: {
		display: "flex",
		flexDirection: "row",
		marginBottom: theme.spacing(1)
	},
	alert: {
		marginTop: theme.spacing(-2),
		marginBottom: theme.spacing(2)
	},
	accountDisabledCheckbox: { height: "auto" },
	accountDisabledCheckboxLabel: { marginTop: theme.spacing(1) }
}));
const UserFormFields = (props) => {
	const $ = c(57);
	const { creationMethod, editedUser, handlePasswordTypeChange, handleCreationMethodChange, hideEmailField, passwordType, rolesData, showRolesField: t0, altFieldMapping } = props;
	const showRolesField = t0 === void 0 ? true : t0;
	const classes = useStyles();
	const user = useUser();
	const [showPassword, setShowPassword] = useState(false);
	const { t } = useTranslation();
	let t1;
	if (altFieldMapping) t1 = altFieldMapping;
	else {
		let t2;
		if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
			t2 = {
				userId: "userId",
				firstName: "firstName",
				lastName: "lastName",
				email: "email",
				roles: "systemWideSecurityRoles"
			};
			$[0] = t2;
		} else t2 = $[0];
		t1 = t2;
	}
	const userMapping = t1;
	let t2;
	bb0: {
		if (!rolesData) {
			let t3;
			if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
				t3 = [];
				$[1] = t3;
			} else t3 = $[1];
			t2 = t3;
			break bb0;
		}
		rolesData.forEach(_temp);
		let t3;
		if ($[2] !== rolesData) {
			t3 = rolesData.sort(_temp2).sort(_temp3);
			$[2] = rolesData;
			$[3] = t3;
		} else t3 = $[3];
		t2 = t3;
	}
	const roles = t2;
	let t3;
	bb1: {
		if (!editedUser) {
			let t4;
			if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
				t4 = [];
				$[4] = t4;
			} else t4 = $[4];
			t3 = t4;
			break bb1;
		}
		let t4;
		if ($[5] !== editedUser || $[6] !== roles || $[7] !== userMapping.roles) {
			let t5;
			if ($[9] !== editedUser || $[10] !== userMapping.roles) {
				t5 = (r_0) => !!editedUser[userMapping.roles]?.find((edur) => edur.roleId === r_0.roleId);
				$[9] = editedUser;
				$[10] = userMapping.roles;
				$[11] = t5;
			} else t5 = $[11];
			t4 = roles.filter(t5);
			$[5] = editedUser;
			$[6] = roles;
			$[7] = userMapping.roles;
			$[8] = t4;
		} else t4 = $[8];
		t3 = t4;
	}
	const initialRolesValue = t3;
	let t4;
	if ($[12] !== showPassword) {
		t4 = () => {
			setShowPassword(!showPassword);
		};
		$[12] = showPassword;
		$[13] = t4;
	} else t4 = $[13];
	const handleClickShowPassword = t4;
	let t5;
	if ($[14] !== editedUser || $[15] !== userMapping.userId) {
		t5 = editedUser && /* @__PURE__ */ React.createElement(Field, {
			component: FluentTextField,
			label: "User ID",
			id: "user-id",
			name: userMapping.userId,
			fullWidth: true,
			variant: "outlined",
			margin: "normal",
			size: "small",
			initialValue: editedUser?.[userMapping.userId],
			disabled: true
		});
		$[14] = editedUser;
		$[15] = userMapping.userId;
		$[16] = t5;
	} else t5 = $[16];
	let t6;
	if ($[17] !== creationMethod || $[18] !== editedUser || $[19] !== handleCreationMethodChange || $[20] !== t) {
		t6 = !editedUser && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(FormLabel, { component: "legend" }, t("Creation Method")), /* @__PURE__ */ React.createElement(FluentRadioGroup, {
			"aria-labelledby": "creation-method",
			name: "creation-method",
			value: creationMethod,
			onChange: handleCreationMethodChange
		}, /* @__PURE__ */ React.createElement(FormControlLabel, {
			value: "standard",
			control: /* @__PURE__ */ React.createElement(FluentRadio, null),
			label: t("Standard (Email & Password)")
		}), /* @__PURE__ */ React.createElement(FormControlLabel, {
			value: "invite",
			control: /* @__PURE__ */ React.createElement(FluentRadio, null),
			label: t("Invite Entra User (Secure Email Invite Link)")
		})));
		$[17] = creationMethod;
		$[18] = editedUser;
		$[19] = handleCreationMethodChange;
		$[20] = t;
		$[21] = t6;
	} else t6 = $[21];
	const t7 = editedUser?.[userMapping.firstName];
	const t8 = !user.can.CAN_MANAGE_USERS;
	let t9;
	if ($[22] !== t7 || $[23] !== t8 || $[24] !== userMapping.firstName) {
		t9 = /* @__PURE__ */ React.createElement(Field, {
			component: FluentTextField,
			label: "First Name",
			id: "first-name",
			name: userMapping.firstName,
			fullWidth: true,
			variant: "outlined",
			margin: "normal",
			size: "small",
			required: true,
			initialValue: t7,
			disabled: t8
		});
		$[22] = t7;
		$[23] = t8;
		$[24] = userMapping.firstName;
		$[25] = t9;
	} else t9 = $[25];
	const t10 = editedUser?.[userMapping.lastName];
	const t11 = !user.can.CAN_MANAGE_USERS;
	let t12;
	if ($[26] !== t10 || $[27] !== t11 || $[28] !== userMapping.lastName) {
		t12 = /* @__PURE__ */ React.createElement(Field, {
			component: FluentTextField,
			label: "Last Name",
			id: "last-name",
			name: userMapping.lastName,
			fullWidth: true,
			variant: "outlined",
			margin: "normal",
			size: "small",
			required: true,
			initialValue: t10,
			disabled: t11
		});
		$[26] = t10;
		$[27] = t11;
		$[28] = userMapping.lastName;
		$[29] = t12;
	} else t12 = $[29];
	let t13;
	if ($[30] !== editedUser || $[31] !== hideEmailField || $[32] !== user.can.CAN_MODIFY_EMAIL || $[33] !== userMapping.email) {
		t13 = !hideEmailField && /* @__PURE__ */ React.createElement(Field, {
			component: FluentTextField,
			label: "Email",
			id: "email",
			name: userMapping.email,
			fullWidth: true,
			variant: "outlined",
			margin: "normal",
			size: "small",
			required: true,
			initialValue: editedUser?.[userMapping.email],
			disabled: editedUser && !user.can.CAN_MODIFY_EMAIL,
			validate: _temp4
		});
		$[30] = editedUser;
		$[31] = hideEmailField;
		$[32] = user.can.CAN_MODIFY_EMAIL;
		$[33] = userMapping.email;
		$[34] = t13;
	} else t13 = $[34];
	let t14;
	if ($[35] !== initialRolesValue || $[36] !== roles || $[37] !== showRolesField || $[38] !== userMapping.roles) {
		t14 = showRolesField && /* @__PURE__ */ React.createElement(Field, {
			component: UserRoleAutoComplete,
			ComponentProps: {
				initialValue: initialRolesValue,
				isFormField: true
			},
			label: "Select Role",
			id: "roles",
			name: userMapping.roles,
			fullWidth: true,
			variant: "outlined",
			margin: "normal",
			size: "small",
			options: roles || [],
			initialValue: initialRolesValue,
			textFieldProps: {
				id: "roles",
				name: "roles"
			},
			optionLabel: "label",
			groupBy: "productName",
			required: true
		});
		$[35] = initialRolesValue;
		$[36] = roles;
		$[37] = showRolesField;
		$[38] = userMapping.roles;
		$[39] = t14;
	} else t14 = $[39];
	let t15;
	if ($[40] !== classes || $[41] !== creationMethod || $[42] !== editedUser || $[43] !== handleClickShowPassword || $[44] !== handlePasswordTypeChange || $[45] !== passwordType || $[46] !== showPassword || $[47] !== user.can.CAN_MANAGE_USERS) {
		t15 = !editedUser && creationMethod !== "invite" && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(FluentRadioGroup, {
			"aria-labelledby": "password-type",
			name: "password-type",
			value: passwordType,
			onChange: handlePasswordTypeChange
		}, /* @__PURE__ */ React.createElement(FormControlLabel, {
			value: "autoGeneratePassword",
			control: /* @__PURE__ */ React.createElement(FluentRadio, null),
			label: "Auto-generate password"
		}), /* @__PURE__ */ React.createElement(FormControlLabel, {
			value: "createPassword",
			control: /* @__PURE__ */ React.createElement(FluentRadio, null),
			label: "Let me create the password"
		})), passwordType === "createPassword" && /* @__PURE__ */ React.createElement(Field, {
			component: FluentTextField,
			label: "Password",
			id: "passwordProfile",
			name: "passwordProfile",
			type: showPassword ? "text" : "password",
			fullWidth: true,
			variant: "outlined",
			margin: "normal",
			size: "small",
			autoComplete: "new-password",
			validate: _temp5,
			InputProps: { endAdornment: /* @__PURE__ */ React.createElement(InputAdornment, { position: "end" }, /* @__PURE__ */ React.createElement(IconButton, {
				"aria-label": "toggle password visibility",
				onClick: handleClickShowPassword
			}, showPassword ? /* @__PURE__ */ React.createElement(Visibility, null) : /* @__PURE__ */ React.createElement(VisibilityOff, null))) }
		}), /* @__PURE__ */ React.createElement(FormControlLabel, {
			className: classes.accountDisabledCheckboxLabel,
			label: "Do not activate account and do not send a welcome email.",
			control: /* @__PURE__ */ React.createElement(Field, {
				component: FluentCheckbox,
				id: "account-locked",
				name: "locked",
				type: "checkbox",
				disabled: !user.can.CAN_MANAGE_USERS,
				classes
			})
		}));
		$[40] = classes;
		$[41] = creationMethod;
		$[42] = editedUser;
		$[43] = handleClickShowPassword;
		$[44] = handlePasswordTypeChange;
		$[45] = passwordType;
		$[46] = showPassword;
		$[47] = user.can.CAN_MANAGE_USERS;
		$[48] = t15;
	} else t15 = $[48];
	let t16;
	if ($[49] !== t12 || $[50] !== t13 || $[51] !== t14 || $[52] !== t15 || $[53] !== t5 || $[54] !== t6 || $[55] !== t9) {
		t16 = /* @__PURE__ */ React.createElement(React.Fragment, null, t5, t6, t9, t12, t13, t14, t15);
		$[49] = t12;
		$[50] = t13;
		$[51] = t14;
		$[52] = t15;
		$[53] = t5;
		$[54] = t6;
		$[55] = t9;
		$[56] = t16;
	} else t16 = $[56];
	return t16;
};
function _temp(r) {
	r.label = r.name;
	r.value = r.name;
}
function _temp2(a, b) {
	return a.label.localeCompare(b.label);
}
function _temp3(a_0, b_0) {
	return a_0.productName.localeCompare(b_0.productName);
}
function _temp4(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 "Invalid email address";
}
function _temp5(value_0) {
	return !isPasswordValid(value_0, 8, true, true, true, true) && /* @__PURE__ */ React.createElement(PasswordValidator, {
		password: value_0,
		numberRestriction: true,
		lowerCaseRestriction: true,
		upperCaseRestriction: true,
		lengthRestriction: 8,
		specialCharacterRestriction: true
	});
}
//#endregion
export { UserFormFields };

//# sourceMappingURL=UserFormFields.mjs.map