import { c as allowedPasswordCharactersString } from "./passwordUtils-B3CN2t6O.js";
import { s as stringType, Z as ZodIssueCode } from "./types-espwwxTQ.js";
const escapeForRegex = (str) => {
  return str.replace(/[-\\[\]^]/g, "\\$&");
};
const usePasswordSchema = (options, t) => {
  const maxLength = 30;
  const minLength = options.MinLength && options.MinLength >= 8 && options.MinLength <= 30 ? options.MinLength : 8;
  return stringType().superRefine((val, ctx) => {
    var _a;
    if (!val || val.length < minLength || val.length > maxLength) {
      ctx.addIssue({
        code: ZodIssueCode.custom,
        message: t("passwordReset.passwordPolicy.minMaxLength", {
          MinLength: minLength,
          MaxLength: maxLength
        })
      });
    }
    if (val.includes(" ")) {
      ctx.addIssue({
        code: ZodIssueCode.custom,
        message: t("passwordReset.passwordPolicy.passwordShouldNotContainSpaces")
      });
      return;
    }
    if (options.UseLatin && !/^[a-zA-Z0-9!-/:-@[-`{-~]*$/.test(val)) {
      ctx.addIssue({
        code: ZodIssueCode.custom,
        message: t("passwordReset.passwordPolicy.passwordLatinOnly")
      });
      return;
    }
    if (options.UseCapitalLetters && !/[A-Z]/.test(val)) {
      ctx.addIssue({
        code: ZodIssueCode.custom,
        message: t("passwordReset.passwordPolicy.useCapitalLetters")
      });
      return;
    }
    if (options.UseLowLetter && !/[a-z]/.test(val)) {
      ctx.addIssue({
        code: ZodIssueCode.custom,
        message: t("passwordReset.passwordPolicy.useLowLetter")
      });
      return;
    }
    if (options.UseDigital && !/\d/.test(val)) {
      ctx.addIssue({
        code: ZodIssueCode.custom,
        message: t("passwordReset.passwordPolicy.digital")
      });
      return;
    }
    if (options.UseAllowedSpecialCharacters) {
      const escaped = escapeForRegex(allowedPasswordCharactersString);
      const allowedClass = `a-zA-Z0-9${escaped}`;
      const invalidCharRegex = new RegExp(`[^${allowedClass}]`, "g");
      const invalid = ((_a = val.match(invalidCharRegex)) == null ? void 0 : _a.filter((v, i, a) => a.indexOf(v) === i)) || [];
      if (invalid.length > 0) {
        ctx.addIssue({
          code: ZodIssueCode.custom,
          message: t("passwordReset.passwordPolicy.allowedSpecialCharacters", {
            characters: allowedPasswordCharactersString,
            symbols: ` (${invalid.join(", ")})`
          })
        });
        return;
      }
    }
    if (options.UseSpecialCharacters) {
      const input = val || "";
      let passed = false;
      if (options.UseAllowedSpecialCharacters) {
        const escaped = escapeForRegex(allowedPasswordCharactersString);
        const regex = new RegExp(`[${escaped}]`);
        passed = regex.test(input);
      } else {
        passed = /[^a-zA-Z0-9]/.test(input);
      }
      if (!passed) {
        ctx.addIssue({
          code: ZodIssueCode.custom,
          message: t("passwordReset.passwordPolicy.useSpecialCharacters")
        });
        return;
      }
    }
  });
};
const escapeForCharClass = (str) => str.replace(/[-[\]\\^]/g, "\\$&");
const useMailServerPasswordSchema = (rules, t) => {
  return stringType().superRefine((val, ctx) => {
    var _a, _b;
    if (!rules) return;
    if (!val || val.length < rules.MinLength || val.length > rules.MaxLength) {
      ctx.addIssue({
        code: ZodIssueCode.custom,
        message: t("passwordReset.passwordPolicy.minMaxLength", {
          MinLength: rules.MinLength,
          MaxLength: rules.MaxLength
        })
      });
      return;
    }
    if (rules.ForbidWhitespace && /\s/.test(val)) {
      ctx.addIssue({
        code: ZodIssueCode.custom,
        message: t("passwordReset.passwordPolicy.passwordShouldNotContainSpaces")
      });
      return;
    }
    if (rules.RequireUppercase && !/[A-Z]/.test(val)) {
      ctx.addIssue({
        code: ZodIssueCode.custom,
        message: t("passwordReset.passwordPolicy.useCapitalLetters")
      });
      return;
    }
    if (rules.RequireLowercase && !/[a-z]/.test(val)) {
      ctx.addIssue({
        code: ZodIssueCode.custom,
        message: t("passwordReset.passwordPolicy.useLowLetter")
      });
      return;
    }
    if (rules.RequireDigit && !/\d/.test(val)) {
      ctx.addIssue({
        code: ZodIssueCode.custom,
        message: t("passwordReset.passwordPolicy.digital")
      });
      return;
    }
    if (rules.AllowedSpecialCharacters !== null && rules.AllowedSpecialCharacters !== void 0) {
      const escaped = escapeForCharClass(rules.AllowedSpecialCharacters);
      const invalidCharRegex = new RegExp(`[^a-zA-Z0-9${escaped}]`, "g");
      const invalid = ((_a = val.match(invalidCharRegex)) == null ? void 0 : _a.filter((v, i, a) => a.indexOf(v) === i)) ?? [];
      if (invalid.length > 0) {
        ctx.addIssue({
          code: ZodIssueCode.custom,
          message: t("passwordReset.passwordPolicy.allowedSpecialCharacters", {
            characters: rules.AllowedSpecialCharacters,
            symbols: ` (${invalid.join(", ")})`
          })
        });
        return;
      }
    }
    if (rules.ForbiddenSpecialCharacters !== null && rules.ForbiddenSpecialCharacters !== void 0) {
      const escaped = escapeForCharClass(rules.ForbiddenSpecialCharacters);
      const forbiddenRegex = new RegExp(`[${escaped}]`, "g");
      const found = ((_b = val.match(forbiddenRegex)) == null ? void 0 : _b.filter((v, i, a) => a.indexOf(v) === i)) ?? [];
      if (found.length > 0) {
        ctx.addIssue({
          code: ZodIssueCode.custom,
          message: t("passwordReset.passwordPolicy.forbiddenSpecialCharacters", {
            symbols: ` (${found.join(", ")})`
          })
        });
        return;
      }
    }
  });
};
export {
  useMailServerPasswordSchema as a,
  usePasswordSchema as u
};
//# sourceMappingURL=useGeneratePolicy-B4rO5xUa.js.map
