export const SITE_URL = 'https://howdoyouconvert.com';
export const SITE_NAME = 'HowDoYouConvert.com';
export const DEFAULT_ROBOTS = 'index,follow';
export const DEFAULT_TWITTER_CARD = 'summary';

type OpenGraphType = 'website' | 'article';

type SeoInput = {
  title: string;
  description: string;
  pathname: string;
  type?: OpenGraphType;
};

const normalizePathname = (pathname: string): string => {
  const pathWithSlash = pathname.startsWith('/') ? pathname : `/${pathname}`;
  const normalized = pathWithSlash.replace(/\/{2,}/g, '/');
  if (normalized.length > 1 && normalized.endsWith('/')) {
    return normalized.slice(0, -1);
  }
  return normalized;
};

export const canonicalUrl = (pathname: string): string => `${SITE_URL}${normalizePathname(pathname)}`;

export const buildSeoMeta = ({ title, description, pathname, type = 'website' }: SeoInput) => {
  const canonical = canonicalUrl(pathname);
  return {
    canonical,
    robots: DEFAULT_ROBOTS,
    og: {
      type,
      title,
      description,
      url: canonical,
      siteName: SITE_NAME,
    },
    twitter: {
      card: DEFAULT_TWITTER_CARD,
      title,
      description,
    },
  };
};

export const toJsonLd = (value: unknown): string => JSON.stringify(value).replace(/</g, '\\u003c');
