"use client";

import { useEffect } from "react";
import { defaultSEOConfig } from "@/config/seoConfig";

const DEFAULT_TITLE =
  typeof defaultSEOConfig.title === "string"
    ? defaultSEOConfig.title
    : "مدستاگرام";

export function formatPageTitle(title: string) {
  const trimmed = title.trim();
  if (!trimmed) return DEFAULT_TITLE;
  if (trimmed.includes("مدستاگرام")) return trimmed;
  return `${trimmed} | مدستاگرام`;
}

export function usePageTitle(title?: string | null) {
  useEffect(() => {
    if (!title?.trim()) return;
    document.title = formatPageTitle(title);
  }, [title]);
}
