import { z } from "zod";
import { parseBooleanWithDefault } from "@open-mercato/shared/lib/boolean";
const DEFAULT_POLICY = {
  minLength: 6,
  requireDigit: true,
  requireUppercase: true,
  requireSpecial: true
};
const ENV_KEYS = {
  minLength: "OM_PASSWORD_MIN_LENGTH",
  requireDigit: "OM_PASSWORD_REQUIRE_DIGIT",
  requireUppercase: "OM_PASSWORD_REQUIRE_UPPERCASE",
  requireSpecial: "OM_PASSWORD_REQUIRE_SPECIAL"
};
const PUBLIC_PREFIX = "NEXT_PUBLIC_";
function readEnvValue(env, key) {
  const rawKey = ENV_KEYS[key];
  const publicKey = `${PUBLIC_PREFIX}${rawKey}`;
  const rawValue = env[rawKey];
  if (typeof rawValue === "string" && rawValue.trim().length > 0) return rawValue;
  const publicValue = env[publicKey];
  if (typeof publicValue === "string" && publicValue.trim().length > 0) return publicValue;
  return void 0;
}
function parsePositiveInt(raw, fallback, min = 1) {
  if (!raw) return fallback;
  const parsed = Number.parseInt(raw, 10);
  if (Number.isNaN(parsed)) return fallback;
  return Math.max(min, parsed);
}
function getPasswordPolicy(env = process.env) {
  const minLength = parsePositiveInt(
    readEnvValue(env, "minLength"),
    DEFAULT_POLICY.minLength,
    1
  );
  return {
    minLength,
    requireDigit: parseBooleanWithDefault(
      readEnvValue(env, "requireDigit"),
      DEFAULT_POLICY.requireDigit
    ),
    requireUppercase: parseBooleanWithDefault(
      readEnvValue(env, "requireUppercase"),
      DEFAULT_POLICY.requireUppercase
    ),
    requireSpecial: parseBooleanWithDefault(
      readEnvValue(env, "requireSpecial"),
      DEFAULT_POLICY.requireSpecial
    )
  };
}
function getPasswordRequirements(policy = getPasswordPolicy()) {
  const requirements = [{ id: "minLength", value: policy.minLength }];
  if (policy.requireDigit) requirements.push({ id: "digit" });
  if (policy.requireUppercase) requirements.push({ id: "uppercase" });
  if (policy.requireSpecial) requirements.push({ id: "special" });
  return requirements;
}
function formatPasswordRequirements(policy, translate, keyPrefix = "auth.password.requirements") {
  const items = getPasswordRequirements(policy).map((requirement) => {
    switch (requirement.id) {
      case "minLength":
        return translate(
          `${keyPrefix}.minLength`,
          "At least {min} characters",
          { min: requirement.value ?? policy.minLength }
        );
      case "digit":
        return translate(`${keyPrefix}.digit`, "One number");
      case "uppercase":
        return translate(`${keyPrefix}.uppercase`, "One uppercase letter");
      case "special":
        return translate(`${keyPrefix}.special`, "One special character");
      default:
        return "";
    }
  }).filter((value) => value && value.trim().length > 0);
  if (!items.length) return "";
  const separator = translate(`${keyPrefix}.separator`, ", ");
  return items.join(separator);
}
function validatePassword(password, policy = getPasswordPolicy()) {
  const violations = [];
  if (password.length < policy.minLength) violations.push("minLength");
  if (policy.requireDigit && !/[0-9]/.test(password)) violations.push("digit");
  if (policy.requireUppercase && !/[A-Z]/.test(password)) violations.push("uppercase");
  if (policy.requireSpecial && !/[^A-Za-z0-9]/.test(password)) violations.push("special");
  return { ok: violations.length === 0, violations };
}
function buildPasswordSchema(options) {
  const policy = options?.policy ?? getPasswordPolicy();
  const maxLength = options?.maxLength;
  const message = options?.message ?? "Password does not meet the requirements.";
  let schema = z.string().min(policy.minLength, message);
  if (typeof maxLength === "number") schema = schema.max(maxLength, message);
  return schema.superRefine((value, ctx) => {
    const result = validatePassword(value, policy);
    if (!result.ok) {
      ctx.addIssue({ code: z.ZodIssueCode.custom, message });
    }
  });
}
export {
  buildPasswordSchema,
  formatPasswordRequirements,
  getPasswordPolicy,
  getPasswordRequirements,
  validatePassword
};
//# sourceMappingURL=passwordPolicy.js.map
