import { setResponseValueAndErrors as f } from "../errorMessages.js";
let m;
const o = {
  /**
   * `c` was changed to `[cC]` to replicate /i flag
   */
  cuid: /^[cC][^\s-]{8,}$/,
  cuid2: /^[0-9a-z]+$/,
  ulid: /^[0-9A-HJKMNP-TV-Z]{26}$/,
  /**
   * `a-z` was added to replicate /i flag
   */
  email: /^(?!\.)(?!.*\.\.)([a-zA-Z0-9_'+\-\.]*)[a-zA-Z0-9_+-]@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/,
  /**
   * Constructed a valid Unicode RegExp
   *
   * Lazily instantiate since this type of regex isn't supported
   * in all envs (e.g. React Native).
   *
   * See:
   * https://github.com/colinhacks/zod/issues/2433
   * Fix in Zod:
   * https://github.com/colinhacks/zod/commit/9340fd51e48576a75adc919bff65dbc4a5d4c99b
   */
  emoji: () => (m === void 0 && (m = RegExp("^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$", "u")), m),
  /**
   * Unused
   */
  uuid: /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/,
  /**
   * Unused
   */
  ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,
  ipv4Cidr: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/,
  /**
   * Unused
   */
  ipv6: /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/,
  ipv6Cidr: /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,
  base64: /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/,
  base64url: /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/,
  nanoid: /^[a-zA-Z0-9_-]{21}$/,
  jwt: /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/
};
function v(t, r) {
  const a = {
    type: "string"
  };
  if (t.checks)
    for (const e of t.checks)
      switch (e.kind) {
        case "min":
          f(a, "minLength", typeof a.minLength == "number" ? Math.max(a.minLength, e.value) : e.value, e.message, r);
          break;
        case "max":
          f(a, "maxLength", typeof a.maxLength == "number" ? Math.min(a.maxLength, e.value) : e.value, e.message, r);
          break;
        case "email":
          switch (r.emailStrategy) {
            case "format:email":
              l(a, "email", e.message, r);
              break;
            case "format:idn-email":
              l(a, "idn-email", e.message, r);
              break;
            case "pattern:zod":
              s(a, o.email, e.message, r);
              break;
          }
          break;
        case "url":
          l(a, "uri", e.message, r);
          break;
        case "uuid":
          l(a, "uuid", e.message, r);
          break;
        case "regex":
          s(a, e.regex, e.message, r);
          break;
        case "cuid":
          s(a, o.cuid, e.message, r);
          break;
        case "cuid2":
          s(a, o.cuid2, e.message, r);
          break;
        case "startsWith":
          s(a, RegExp(`^${p(e.value, r)}`), e.message, r);
          break;
        case "endsWith":
          s(a, RegExp(`${p(e.value, r)}$`), e.message, r);
          break;
        case "datetime":
          l(a, "date-time", e.message, r);
          break;
        case "date":
          l(a, "date", e.message, r);
          break;
        case "time":
          l(a, "time", e.message, r);
          break;
        case "duration":
          l(a, "duration", e.message, r);
          break;
        case "length":
          f(a, "minLength", typeof a.minLength == "number" ? Math.max(a.minLength, e.value) : e.value, e.message, r), f(a, "maxLength", typeof a.maxLength == "number" ? Math.min(a.maxLength, e.value) : e.value, e.message, r);
          break;
        case "includes": {
          s(a, RegExp(p(e.value, r)), e.message, r);
          break;
        }
        case "ip": {
          e.version !== "v6" && l(a, "ipv4", e.message, r), e.version !== "v4" && l(a, "ipv6", e.message, r);
          break;
        }
        case "base64url":
          s(a, o.base64url, e.message, r);
          break;
        case "jwt":
          s(a, o.jwt, e.message, r);
          break;
        case "cidr": {
          e.version !== "v6" && s(a, o.ipv4Cidr, e.message, r), e.version !== "v4" && s(a, o.ipv6Cidr, e.message, r);
          break;
        }
        case "emoji":
          s(a, o.emoji(), e.message, r);
          break;
        case "ulid": {
          s(a, o.ulid, e.message, r);
          break;
        }
        case "base64": {
          switch (r.base64Strategy) {
            case "format:binary": {
              l(a, "binary", e.message, r);
              break;
            }
            case "contentEncoding:base64": {
              f(a, "contentEncoding", "base64", e.message, r);
              break;
            }
            case "pattern:zod": {
              s(a, o.base64, e.message, r);
              break;
            }
          }
          break;
        }
        case "nanoid":
          s(a, o.nanoid, e.message, r);
      }
  return a;
}
function p(t, r) {
  return r.patternStrategy === "escape" ? A(t) : t;
}
const b = new Set("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789");
function A(t) {
  let r = "";
  for (let a = 0; a < t.length; a++)
    b.has(t[a]) || (r += "\\"), r += t[a];
  return r;
}
function l(t, r, a, e) {
  t.format || t.anyOf?.some((i) => i.format) ? (t.anyOf || (t.anyOf = []), t.format && (t.anyOf.push({
    format: t.format,
    ...t.errorMessage && e.errorMessages && {
      errorMessage: { format: t.errorMessage.format }
    }
  }), delete t.format, t.errorMessage && (delete t.errorMessage.format, Object.keys(t.errorMessage).length === 0 && delete t.errorMessage)), t.anyOf.push({
    format: r,
    ...a && e.errorMessages && { errorMessage: { format: a } }
  })) : f(t, "format", r, a, e);
}
function s(t, r, a, e) {
  t.pattern || t.allOf?.some((i) => i.pattern) ? (t.allOf || (t.allOf = []), t.pattern && (t.allOf.push({
    pattern: t.pattern,
    ...t.errorMessage && e.errorMessages && {
      errorMessage: { pattern: t.errorMessage.pattern }
    }
  }), delete t.pattern, t.errorMessage && (delete t.errorMessage.pattern, Object.keys(t.errorMessage).length === 0 && delete t.errorMessage)), t.allOf.push({
    pattern: d(r, e),
    ...a && e.errorMessages && { errorMessage: { pattern: a } }
  })) : f(t, "pattern", d(r, e), a, e);
}
function d(t, r) {
  if (!r.applyRegexFlags || !t.flags)
    return t.source;
  const a = {
    i: t.flags.includes("i"),
    m: t.flags.includes("m"),
    s: t.flags.includes("s")
    // `.` matches newlines
  }, e = a.i ? t.source.toLowerCase() : t.source;
  let i = "", u = !1, g = !1, c = !1;
  for (let n = 0; n < e.length; n++) {
    if (u) {
      i += e[n], u = !1;
      continue;
    }
    if (a.i) {
      if (g) {
        if (e[n].match(/[a-z]/)) {
          c ? (i += e[n], i += `${e[n - 2]}-${e[n]}`.toUpperCase(), c = !1) : e[n + 1] === "-" && e[n + 2]?.match(/[a-z]/) ? (i += e[n], c = !0) : i += `${e[n]}${e[n].toUpperCase()}`;
          continue;
        }
      } else if (e[n].match(/[a-z]/)) {
        i += `[${e[n]}${e[n].toUpperCase()}]`;
        continue;
      }
    }
    if (a.m) {
      if (e[n] === "^") {
        i += `(^|(?<=[\r
]))`;
        continue;
      } else if (e[n] === "$") {
        i += `($|(?=[\r
]))`;
        continue;
      }
    }
    if (a.s && e[n] === ".") {
      i += g ? `${e[n]}\r
` : `[${e[n]}\r
]`;
      continue;
    }
    i += e[n], e[n] === "\\" ? u = !0 : g && e[n] === "]" ? g = !1 : !g && e[n] === "[" && (g = !0);
  }
  try {
    new RegExp(i);
  } catch {
    return console.warn(`Could not convert regex pattern at ${r.currentPath.join("/")} to a flag-independent form! Falling back to the flag-ignorant source`), t.source;
  }
  return i;
}
export {
  v as parseStringDef,
  o as zodPatterns
};
//# sourceMappingURL=string.js.map
