{"version":3,"file":"whatsapp.cjs","names":[],"sources":["../../src/br/whatsapp.ts"],"sourcesContent":["/** Country calling code for Brazil, the digits `wa.me` needs in front of a national number. */\nconst BR_COUNTRY_CODE = \"55\";\n\nconst NATIONAL_LENGTHS = new Set([10, 11]);\nconst E164_MIN_WITH_COUNTRY_CODE = 12;\nconst E164_MAX = 15;\n\n/**\n * Normalise a phone number to the digits-only form `wa.me` accepts.\n *\n * A Brazilian number is stored almost everywhere **without** the country code —\n * ten digits for a landline, eleven for a mobile — and `wa.me` refuses anything\n * that is not fully qualified. Those two shapes get `55` in front; a number that\n * already carries a country code (12 to 15 digits, the E.164 range that no\n * Brazilian national number can reach) is returned untouched, so a foreign\n * client's number still works.\n *\n * Everything else returns an empty string rather than a best guess. Handing back\n * the digits as typed is what the naive version does, and it builds a `wa.me`\n * URL that opens WhatsApp on a number nobody owns — a typo becomes a dead chat\n * window instead of a disabled button.\n *\n * Trunk and carrier-selection prefixes (`0`, `021`, …) are **not** stripped: a\n * leading zero is ambiguous between the two, so a number carrying one reads as\n * invalid instead of being silently reinterpreted. Pass the plain number. The\n * leading zero is what rules it out even at a plausible length — no E.164\n * country code starts with `0`, so `011999998888` is a national number wearing a\n * trunk prefix, never an international one.\n *\n * @example\n * toWhatsAppNumber(\"(11) 99999-8888\"); // \"5511999998888\"\n * toWhatsAppNumber(\"+55 11 99999-8888\"); // \"5511999998888\"\n * toWhatsAppNumber(\"99999-8888\"); // \"\" — no area code, nothing to dial\n *\n * @param phone - Phone number in any shape: masked, spaced, `+`-prefixed.\n * @returns Digits ready for a `wa.me` link, or an empty string when the input\n *   cannot be resolved to a dialable number.\n */\nexport function toWhatsAppNumber(phone: string): string {\n    const digits = phone.replace(/\\D/g, \"\");\n    if (NATIONAL_LENGTHS.has(digits.length)) return `${BR_COUNTRY_CODE}${digits}`;\n    const withinE164 = digits.length >= E164_MIN_WITH_COUNTRY_CODE && digits.length <= E164_MAX;\n    if (withinE164 && !digits.startsWith(\"0\")) return digits;\n    return \"\";\n}\n\n/**\n * Build the `wa.me` deep link that opens a chat with an optional message\n * already typed.\n *\n * This is a **deep link, never a send**: the chat opens with the text ready and\n * the person presses send. That is the feature, not a limitation — it needs no\n * official Cloud API, no approved template and no business verification, and it\n * cannot let the UI claim a message was delivered when it was not.\n *\n * `api.whatsapp.com/send?phone=…` is deliberately not offered as an alternative\n * host: both resolve to the same chooser, and `wa.me` is the short form of it.\n *\n * @example\n * whatsAppUrl(\"(11) 99999-8888\", \"Seu horário é amanhã às 14h.\");\n * // \"https://wa.me/5511999998888?text=Seu%20hor%C3%A1rio%20%C3%A9%20amanh%C3%A3%20%C3%A0s%2014h.\"\n *\n * @param phone - Phone number in any shape; normalised by {@link toWhatsAppNumber}.\n * @param text - Message to pre-fill. Omitted or empty leaves the composer blank.\n * @returns The `https://wa.me/…` URL, or an empty string when the number cannot\n *   be normalised.\n */\nexport function whatsAppUrl(phone: string, text?: string): string {\n    const number = toWhatsAppNumber(phone);\n    if (!number) return \"\";\n    const url = `https://wa.me/${number}`;\n    return text ? `${url}?text=${encodeURIComponent(text)}` : url;\n}\n\n/**\n * Open the WhatsApp chat in a new tab.\n *\n * @example\n * <Button onClick={() => openWhatsApp(client.phone, greeting)}>\n *     Falar no WhatsApp\n * </Button>\n *\n * @param phone - Phone number in any shape; normalised by {@link toWhatsAppNumber}.\n * @param text - Message to pre-fill. Omitted or empty leaves the composer blank.\n * @returns `true` when a URL was built and the open was requested, `false` when\n *   the number is unusable or there is no `window` (test runner, service worker,\n *   build plugin). It reports the **request**, not the tab: opening with\n *   `noopener` makes `window.open` return `null` even on success, so its result\n *   cannot distinguish a blocked popup from an opened one.\n */\nexport function openWhatsApp(phone: string, text?: string): boolean {\n    if (typeof window === \"undefined\") return false;\n    const url = whatsAppUrl(phone, text);\n    if (!url) return false;\n    window.open(url, \"_blank\", \"noopener,noreferrer\");\n    return true;\n}\n"],"mappings":"AACA,IAEM,EAAmB,IAAI,IAAI,CAAC,GAAI,EAAE,CAAC,EACnC,EAA6B,GAC7B,EAAW,GAiCjB,SAAgB,EAAiB,EAAuB,CACpD,IAAM,EAAS,EAAM,QAAQ,MAAO,EAAE,EAItC,OAHI,EAAiB,IAAI,EAAO,MAAM,EAAU,KAAqB,IAClD,EAAO,QAAU,GAA8B,EAAO,QAAU,GACjE,CAAC,EAAO,WAAW,GAAG,EAAU,EAC3C,EACX,CAuBA,SAAgB,EAAY,EAAe,EAAuB,CAC9D,IAAM,EAAS,EAAiB,CAAK,EACrC,GAAI,CAAC,EAAQ,MAAO,GACpB,IAAM,EAAM,iBAAiB,IAC7B,OAAO,EAAO,GAAG,EAAI,QAAQ,mBAAmB,CAAI,IAAM,CAC9D,CAkBA,SAAgB,EAAa,EAAe,EAAwB,CAChE,GAAI,OAAO,OAAW,IAAa,MAAO,GAC1C,IAAM,EAAM,EAAY,EAAO,CAAI,EAGnC,OAFK,GACL,OAAO,KAAK,EAAK,SAAU,qBAAqB,EACzC,IAFU,EAGrB"}