import type { CrewWidgetConfigItem } from '@/types/response/crew/widgetConfig';

export type FormState = Omit<CrewWidgetConfigItem, 'id'>;

export function createEmptyForm(): FormState {
	return {
		title: '크루 순위',
		theme: 0,
		period: 3,
		startAt: null,
		endAt: null,
		maxDisplayCount: 5,
		isShowAmount: true,
		isShowDonationCount: false,
		isShowContributionRate: true,
		isShowMemberIcon: true,
		isActive: true,
		bgColor: '#1A1A2E',
		titleFontFamily: null,
		titleFontSizePx: 18,
		titleFontColor: '#FFFFFF',
		rank1FontFamily: null,
		rank1FontSizePx: 15,
		rank1FontColor: '#FFD700',
		rank2FontFamily: null,
		rank2FontSizePx: 15,
		rank2FontColor: '#C0C0C0',
		rank3FontFamily: null,
		rank3FontSizePx: 15,
		rank3FontColor: '#CD7F32',
		rowFontFamily: null,
		rowFontSizePx: 14,
		rowFontColor: '#FFFFFF'
	};
}

export function formatInput(date: Date): string {
	const y = date.getFullYear();
	const m = String(date.getMonth() + 1).padStart(2, '0');
	const d = String(date.getDate()).padStart(2, '0');
	const h = String(date.getHours()).padStart(2, '0');
	const min = String(date.getMinutes()).padStart(2, '0');
	return `${y}.${m}.${d} ${h}:${min}`;
}

export function parseInput(str: string): string {
	if (!str || str.length < 16) return '';
	const [datePart, timePart] = str.split(' ');
	if (!datePart || !timePart) return '';
	const [y, m, d] = datePart.split('.');
	const [h, min] = timePart.split(':');
	const date = new Date(Number(y), Number(m) - 1, Number(d), Number(h), Number(min));
	return isNaN(date.getTime()) ? '' : date.toISOString();
}

export function formatDateTime(dateStr: string|null): string {
	if (!dateStr) return '-';
	const d = new Date(dateStr);
	if (isNaN(d.getTime())) return '-';
	return formatInput(d);
}