// src/lib/sites/url.ts — pure URL helpers shared by the site form & queries.

/** Scheme prefixes offered in the domain dropdown. */
export const SCHEMES = [
  "https://",
  "http://",
  "https://www.",
  "http://www.",
] as const;

/** Strip any pasted scheme / www / trailing slash so only the bare host remains. */
export function cleanDomain(d: string): string {
  return d
    .trim()
    .replace(/^https?:\/\//i, "")
    .replace(/^www\./i, "")
    .replace(/\/+$/, "")
    .toLowerCase();
}

/** Split a stored URL back into a { scheme, domain } pair for form pre-fill. */
export function splitUrl(url: string): { scheme: string; domain: string } {
  const trimmed = url.trim();
  const secure = !/^http:\/\//i.test(trimmed);
  const www = /^https?:\/\/www\./i.test(trimmed);
  const scheme = `${secure ? "https" : "http"}://${www ? "www." : ""}`;
  return { scheme, domain: cleanDomain(trimmed) };
}
