"use client";

import { useEffect } from "react";

type PreferredLocaleCookieSyncProps = {
  enabled: boolean;
};

export function PreferredLocaleCookieSync({ enabled }: PreferredLocaleCookieSyncProps) {
  useEffect(() => {
    if (!enabled) {
      return;
    }

    const hostname = window.location.hostname.toLowerCase();
    const isMailHost = hostname === "mail.officialsite.kr";
    const isLocalHost = hostname === "localhost" || hostname === "127.0.0.1";

    if (!isMailHost && !isLocalHost) {
      return;
    }

    void fetch("/api/user/language", {
      cache: "no-store",
      credentials: "same-origin",
      method: "POST",
    }).catch(() => {
      // Language remains available from the authenticated user snapshot.
    });
  }, [enabled]);

  return null;
}
