import { __ } from '@wordpress/i18n';

export function esc(v: unknown): string {
	return String(v).replace(/[&<>"']/g, (m) =>
		({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' } as Record<string, string>)[m] ?? m,
	);
}

export function maskEmail(email: string): string {
	const parts = email.split('@');
	if (parts.length !== 2) {
		return email;
	}
	const [name, domain] = parts;
	const head = name.slice(0, 2);
	return head + '*'.repeat(Math.max(1, name.length - 2)) + '@' + domain;
}

export function formatAdminDate(value: string | undefined | null): string {
	if (!value) return '-';
	const raw = String(value);
	const parsed = new Date(raw.replace(' ', 'T'));
	if (Number.isNaN(parsed.getTime())) return raw;
	return new Intl.DateTimeFormat(undefined, {
		month: 'short', day: 'numeric', year: 'numeric',
		hour: 'numeric', minute: '2-digit',
	}).format(parsed);
}

export function getNotificationStatus(value: string | undefined): { label: string; className: string } {
	switch (String(value || '').toLowerCase()) {
		case 'notified':    return { label: __( 'Notified', 'stockbeacon-product-waitlists-for-woocommerce' ),     className: 'is-notified' };
		case 'purchased':   return { label: __( 'Purchased', 'stockbeacon-product-waitlists-for-woocommerce' ),    className: 'is-purchased' };
		case 'pending':     return { label: __( 'Pending', 'stockbeacon-product-waitlists-for-woocommerce' ),      className: 'is-pending' };
		case 'unsubscribed': return { label: __( 'Unsubscribed', 'stockbeacon-product-waitlists-for-woocommerce' ), className: 'is-unsubscribed' };
		default:            return { label: __( 'Waiting', 'stockbeacon-product-waitlists-for-woocommerce' ),      className: 'is-waiting' };
	}
}
