import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { useMemo } from "react";
//#region src/pro/Login2.0/hooks/usePasswordValidationRules.ts
const DEFAULT_MESSAGES = {
	required: "Password is required",
	minLength: "Password must be at least {length} characters long",
	requireUppercase: "at least one uppercase letter",
	requireLowercase: "at least one lowercase letter",
	requireNumber: "at least one number",
	requireSpecialCharacters: "at least one special character",
	summary: "Password must contain: {requirements}"
};
const formatMessage = (template, variables) => Object.keys(variables).reduce((message, key) => {
	var _variables$key;
	return message.replace(new RegExp(`\{${key}\}`, "g"), String((_variables$key = variables[key]) !== null && _variables$key !== void 0 ? _variables$key : ""));
}, template);
const resolveMessage = (key, translate, messageKeys) => {
	const messageKey = messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys[key];
	if (messageKey && translate) {
		const translated = translate(messageKey);
		if (translated) return translated;
	}
	return DEFAULT_MESSAGES[key];
};
const usePasswordValidationRules = (passwordRules, options) => {
	const translate = options === null || options === void 0 ? void 0 : options.translate;
	const messageKeys = options === null || options === void 0 ? void 0 : options.messageKeys;
	const fallbackMinLength = options === null || options === void 0 ? void 0 : options.fallbackMinLength;
	return useMemo(() => {
		const rules = [{
			required: true,
			message: resolveMessage("required", translate, messageKeys)
		}];
		const effectiveRules = _objectSpread2({}, passwordRules);
		if (effectiveRules.minLength == null && fallbackMinLength) effectiveRules.minLength = fallbackMinLength;
		if (effectiveRules.minLength) {
			const rawMessage = resolveMessage("minLength", translate, messageKeys);
			rules.push({
				min: effectiveRules.minLength,
				message: formatMessage(rawMessage, { length: effectiveRules.minLength })
			});
		}
		if (effectiveRules.requireUppercase || effectiveRules.requireLowercase || effectiveRules.requireNumber || effectiveRules.requireSpecialCharacters) rules.push({ validator: (_rule, value) => {
			if (!value) return Promise.resolve();
			const requirements = [];
			if (effectiveRules.requireUppercase && !/[A-Z]/.test(value)) requirements.push(resolveMessage("requireUppercase", translate, messageKeys));
			if (effectiveRules.requireLowercase && !/[a-z]/.test(value)) requirements.push(resolveMessage("requireLowercase", translate, messageKeys));
			if (effectiveRules.requireNumber && !/\d/.test(value)) requirements.push(resolveMessage("requireNumber", translate, messageKeys));
			if (effectiveRules.requireSpecialCharacters && !/[!@#$%^&*]/.test(value)) requirements.push(resolveMessage("requireSpecialCharacters", translate, messageKeys));
			if (requirements.length > 0) {
				const template = resolveMessage("summary", translate, messageKeys);
				return Promise.reject(new Error(formatMessage(template, { requirements: requirements.join(", ") })));
			}
			return Promise.resolve();
		} });
		return rules;
	}, [
		fallbackMinLength,
		messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.requireLowercase,
		messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.requireNumber,
		messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.requireSpecialCharacters,
		messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.requireUppercase,
		messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.minLength,
		messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.required,
		messageKeys === null || messageKeys === void 0 ? void 0 : messageKeys.summary,
		passwordRules === null || passwordRules === void 0 ? void 0 : passwordRules.minLength,
		passwordRules === null || passwordRules === void 0 ? void 0 : passwordRules.requireLowercase,
		passwordRules === null || passwordRules === void 0 ? void 0 : passwordRules.requireNumber,
		passwordRules === null || passwordRules === void 0 ? void 0 : passwordRules.requireSpecialCharacters,
		passwordRules === null || passwordRules === void 0 ? void 0 : passwordRules.requireUppercase,
		translate
	]);
};
//#endregion
export { usePasswordValidationRules as default };
