import { RULE_VISIBILITY, type RuleVisibility } from "./ruleVisibility"; const SHARE_SLUG_RE = /^[a-z0-9]{4}-[a-z0-9]{4}$/; export function isShareSlug(value: string): boolean { return SHARE_SLUG_RE.test(value); } export function publishedRulePath(ruleId: string): string { return `/rules/${encodeURIComponent(ruleId)}`; } export function shareRulePath(shareSlug: string): string { return `/r/${encodeURIComponent(shareSlug)}`; } export function buildPublicRuleUrl(origin: string, ruleId: string): string { const base = origin.replace(/\/$/, ""); return `${base}${publishedRulePath(ruleId)}`; } export function buildShareRuleUrl(origin: string, shareSlug: string): string { const base = origin.replace(/\/$/, ""); return `${base}${shareRulePath(shareSlug)}`; } /** Host + path as drawn in the Share control, e.g. `communityrule.info/r/k7f2-9qxm`. */ export function formatShareUrlDisplay(origin: string, shareSlug: string): string { try { return `${new URL(origin).host}/r/${shareSlug}`; } catch { const host = origin.replace(/^https?:\/\//, "").replace(/\/$/, ""); return `${host}/r/${shareSlug}`; } } /** * URL the Share control copies and shows. * Shared-by-link (and featured rows that still have a slug) use `/r/{slug}`. * Featured without a slug uses `/rules/{id}`. Private uses `/rules/{id}`. */ export function shareCopyUrl(opts: { origin: string; ruleId: string; shareSlug: string | null; visibility: RuleVisibility; }): string { if ( opts.shareSlug && (opts.visibility === RULE_VISIBILITY.shared_by_link || opts.visibility === RULE_VISIBILITY.public_and_featured) ) { return buildShareRuleUrl(opts.origin, opts.shareSlug); } return buildPublicRuleUrl(opts.origin, opts.ruleId); }