import { RoleIdEnums } from "../../enums/unitySystemEnums.mjs";
//#region src/utilities/auth/user.ts
function assertNonEmptyString(value, fieldName) {
	if (typeof value !== "string" || value.trim().length === 0) throw new Error(`User is missing required field: ${fieldName}`);
}
function getHttpStatusCodeFromError() {
	return 401;
}
function isApiResponse(value) {
	if (!value || typeof value !== "object") return false;
	const v = value;
	return typeof v.userId === "string" && typeof v.externalUUID === "string" && typeof v.name === "string" && Array.isArray(v.roles);
}
/**
* Canonical User model.
*
* - Construct from the new API response (preferred)
* - Or rehydrate from a previously-serialized User instance
*
* NOTE: This model intentionally does NOT read from storage or config.
*/
var User = class {
	constructor(arg, currentTenantIdParam) {
		if (isApiResponse(arg)) {
			assertNonEmptyString(arg.defaultTenantId, "defaultTenantId");
			assertNonEmptyString(arg.currentTenantId, "currentTenantId");
			assertNonEmptyString(arg.userId, "userId");
			assertNonEmptyString(arg.externalUUID, "externalUUID");
			this.userId = arg.userId;
			this.externalUUID = arg.externalUUID;
			this.name = arg.name ?? "";
			this.firstName = arg.firstName ?? "";
			this.familyName = arg.familyName ?? "";
			this.jobTitle = arg.jobTitle ?? null;
			this.email = arg.email ?? "";
			this.mobile = arg.mobile ?? null;
			this.phone = arg.phone ?? null;
			this.location = arg.location ?? null;
			this.isUnitySystemAdmin = Boolean(arg.isUnitySystemAdmin);
			this.roles = Array.isArray(arg.roles) ? arg.roles : [];
			this.permissions = Array.isArray(arg.permissions) ? arg.permissions : [];
			this.can = arg.can ?? {};
			this.defaultTenantId = arg.defaultTenantId;
			this.currentTenantId = arg.currentTenantId;
			this.accessToken = void 0;
			this.products = void 0;
			this.tenantIds = void 0;
			this.additionalProperties = void 0;
			this.statusCode = typeof arg.statusCode === "number" ? arg.statusCode : 200;
			return;
		}
		const u = arg;
		assertNonEmptyString(u.defaultTenantId, "defaultTenantId");
		assertNonEmptyString(u.currentTenantId, "currentTenantId");
		assertNonEmptyString(u.userId, "userId");
		assertNonEmptyString(u.externalUUID, "externalUUID");
		this.userId = u.userId;
		this.externalUUID = u.externalUUID;
		this.name = u.name ?? "";
		this.firstName = u.firstName ?? "";
		this.familyName = u.familyName ?? "";
		this.jobTitle = u.jobTitle ?? null;
		this.email = u.email ?? "";
		this.mobile = u.mobile ?? null;
		this.phone = u.phone ?? null;
		this.location = u.location ?? null;
		this.isUnitySystemAdmin = Boolean(u.isUnitySystemAdmin ?? this.hasRole(RoleIdEnums.Unity_System_Administrator));
		this.roles = u.roles ?? [];
		this.permissions = u.permissions ?? [];
		this.can = u.can ?? {};
		this.defaultTenantId = u.defaultTenantId;
		this.currentTenantId = u.currentTenantId;
		this.accessToken = u.accessToken;
		this.products = u.products;
		this.tenantIds = u.tenantIds;
		this.additionalProperties = u.additionalProperties;
		this.statusCode = typeof u.statusCode === "number" ? u.statusCode : 200;
	}
	/** @deprecated Prefer `userId` */
	get id() {
		return this.userId;
	}
	/** @deprecated Prefer `userId` */
	get unityId() {
		return this.userId;
	}
	/** @deprecated Prefer `externalUUID` */
	get aadId() {
		return this.externalUUID;
	}
	/** @deprecated Prefer `roles` */
	get roleIds() {
		return (this.roles ?? []).map((r) => r.roleId);
	}
	/** @deprecated Prefer `roles` */
	get roleNames() {
		return (this.roles ?? []).map((r) => r.name);
	}
	hasRole(roleId) {
		return this.roleIds.includes(roleId);
	}
	toJSON() {
		return {
			userId: this.userId,
			externalUUID: this.externalUUID,
			name: this.name,
			firstName: this.firstName,
			familyName: this.familyName,
			jobTitle: this.jobTitle,
			email: this.email,
			mobile: this.mobile,
			phone: this.phone,
			location: this.location,
			isUnitySystemAdmin: this.isUnitySystemAdmin,
			roles: this.roles,
			permissions: this.permissions,
			can: this.can,
			defaultTenantId: this.defaultTenantId,
			currentTenantId: this.currentTenantId,
			accessToken: this.accessToken,
			products: this.products,
			tenantIds: this.tenantIds,
			additionalProperties: this.additionalProperties,
			statusCode: this.statusCode
		};
	}
};
/**
* @deprecated Prefer `new User(apiResponse)`.
* This wrapper exists to reduce migration churn.
*/
function createUser(userInfo) {
	try {
		return new User(userInfo);
	} catch {
		return new User({
			userId: "unknown",
			externalUUID: "unknown",
			name: "",
			firstName: "",
			familyName: "",
			jobTitle: null,
			email: "",
			mobile: null,
			phone: null,
			location: null,
			isUnitySystemAdmin: false,
			roles: [],
			permissions: [],
			can: {},
			defaultTenantId: "unknown",
			currentTenantId: "unknown",
			statusCode: getHttpStatusCodeFromError()
		});
	}
}
/**
* @deprecated Prefer `new User(userLike)`.
* This wrapper exists to reduce migration churn.
*/
function createUserFromUserObject(userObject) {
	return new User(userObject);
}
//#endregion
export { User, createUser, createUserFromUserObject };

//# sourceMappingURL=user.mjs.map