import {
	CAMPAIGN_CONFIG,
	CAMPAIGN_TIMES,
} from "@components/common/CampaignBanners/campaignData";

/**
 * Check if a campaign is expired based on its ID
 * @param campaignId - The campaign ID to check
 * @returns boolean indicating if the campaign is expired
 */
export const isCampaignExpired = (campaignId: string): boolean => {
	const expiryTime = CAMPAIGN_TIMES[campaignId];
	if (!expiryTime) {
		return true; // No expiry time = expired
	}

	const now = Date.now();
	const expiryTimeMs = expiryTime * 1000; // Convert seconds to milliseconds
	return now > expiryTimeMs;
};

/**
 * Check if a campaign has started based on its ID
 * @param campaignId - The campaign ID to check
 * @returns boolean indicating if the campaign has started
 */
export const isCampaignStarted = (campaignId: string): boolean => {
	const startTime = CAMPAIGN_CONFIG[campaignId]?.startTime;
	if (!startTime) {
		return true; // No start time = already started (default behavior)
	}

	const now = Date.now();
	const startTimeMs = startTime * 1000; // Convert seconds to milliseconds
	return now >= startTimeMs;
};

/**
 * Filter campaigns to only include active (non-expired) ones
 * @param campaigns - Array of campaigns with id property
 * @returns Array of active campaigns
 */
export const filterActiveCampaigns = <T extends { id: string }>(
	campaigns: T[],
): T[] => {
	return campaigns.filter(
		(campaign) =>
			!isCampaignExpired(campaign.id) && isCampaignStarted(campaign.id),
	);
};

/**
 * Filter campaign entries to only include active (non-expired) ones
 * @param campaignEntries - Array of [campaignId, config] tuples
 * @returns Array of active campaign entries
 */
export const filterActiveCampaignEntries = <T>(
	campaignEntries: Array<[string, T]>,
): Array<[string, T]> => {
	return campaignEntries.filter(
		([campaignId]) =>
			!isCampaignExpired(campaignId) && isCampaignStarted(campaignId),
	);
};
