"use server";

import { cookiePolicyService } from "../services/cookiePolicyService";
import { extractDomain } from "../utils/domain";

/**
 * Fetch the Cookie Notice, published by the hub alongside the other policies.
 */
export async function getCookiePolicyDataAction(
  franchiseeId?: string | null,
  language?: string | null,
) {
  try {
    const domain = await extractDomain();

    const cookiePolicyData = await cookiePolicyService.fetchByDomain(
      domain,
      franchiseeId,
      language,
    );

    return {
      success: true,
      data: cookiePolicyData,
    };
  } catch (error) {
    console.error("[getCookiePolicyDataAction] Error:", error);
    return {
      success: false,
      data: null,
    };
  }
}
