const require_runtime = require("../../_virtual/_rolldown/runtime.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let _pisell_utils = require("@pisell/utils");
function formatDateKey(date) {
	return date.format("YYYY-MM-DD");
}
function getWeekDays(anchor) {
	const weekStart = anchor.startOf("week");
	return Array.from({ length: 7 }, (_, i) => weekStart.add(i, "day"));
}
function getMonthLabel(date) {
	return date.format("MMMM YYYY");
}
function getMonthDays(date) {
	const firstOfMonth = date.startOf("month");
	const daysInMonth = date.daysInMonth();
	const firstDayOfWeek = firstOfMonth.day();
	const offset = firstDayOfWeek === 0 ? 6 : firstDayOfWeek - 1;
	const cells = [];
	for (let i = 0; i < offset; i++) cells.push(null);
	for (let d = 1; d <= daysInMonth; d++) cells.push(firstOfMonth.date(d));
	while (cells.length % 7 !== 0) cells.push(null);
	return cells;
}
function getWeekdayShortLabel(date) {
	const weekday = date.day();
	if (weekday === 1) return String(_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.mon") || "周一");
	if (weekday === 2) return String(_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.tue") || "周二");
	if (weekday === 3) return String(_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.wed") || "周三");
	if (weekday === 4) return String(_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.thu") || "周四");
	if (weekday === 5) return String(_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.fri") || "周五");
	if (weekday === 6) return String(_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.sat") || "周六");
	return String(_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.sun") || "周日");
}
function isHourBoundary(slot) {
	return slot.endsWith(":00");
}
/**
* 格式化小时列头
* 使用 i18n 模板 `pisell2.venue.time.hour.format`，支持 {suffix} / {hour} 两个占位符
*   - en / pt: "{hour}{suffix}" → "6pm"
*   - zh-CN / zh-TW: "{suffix}{hour}点/點" → "下午6点"
*   - ja: "{suffix}{hour}時" → "午後6時"
* 未命中模板时回退到 "{hour}{suffix}" 兼容旧行为
*/
function formatHourHeader(slot) {
	const [hour] = slot.split(":").map(Number);
	const suffix = hour >= 12 ? _pisell_utils.locales.getText("pisell2.venue.time.pm") || "pm" : _pisell_utils.locales.getText("pisell2.venue.time.am") || "am";
	const hour12 = hour % 12 === 0 ? 12 : hour % 12;
	return (_pisell_utils.locales.getText("pisell2.venue.time.hour.format") || "{hour}{suffix}").replace("{hour}", String(hour12)).replace("{suffix}", suffix);
}
/** 从 timeLabels 中提取整点小时（用于表头合并列） */
function buildHourGroups(timeLabels) {
	return timeLabels.filter((slot) => slot.endsWith(":00"));
}
/**
* 从 API 返回的 dateSummary 中查找指定日期的展示信息
* 替代原来基于 mock 数据自行计算的逻辑
*/
function getDateMetaFromSummary(date, dateSummary, symbol = "$") {
	const dateKey = formatDateKey(date);
	const item = dateSummary.find((s) => s.date === dateKey);
	if (!item) return {
		topTag: null,
		startingPrice: null,
		fewLeft: false,
		soldOut: true,
		unavailable: true
	};
	const soldOut = item.status === "sold_out" || item.status === "unavailable";
	const fewLeft = item.status === "few_left";
	const unavailable = item.status === "unavailable";
	const startingPrice = item.startingPrice ? `${symbol}${item.startingPrice}+` : null;
	return {
		topTag: null,
		startingPrice: soldOut || unavailable ? null : startingPrice,
		fewLeft,
		soldOut,
		unavailable
	};
}
/**
* 获取资源维度的可用性概况（某个资源在当天的整体情况）
*/
function getResourceCourtMeta(resource) {
	const total = resource.slots.length;
	const availableCount = resource.slots.filter((s) => s.status === "available").length;
	const ratio = total > 0 ? availableCount / total : 0;
	return {
		soldOut: availableCount === 0,
		fewLeft: availableCount > 0 && ratio < .2
	};
}
/**
* 获取时段维度的可用性概况（某个时间点所有资源的情况）
*/
function getTimeMetaFromGrid(resources, slotIndex) {
	const availableCount = resources.filter((r) => r.slots[slotIndex] && r.slots[slotIndex].status === "available").length;
	const ratio = resources.length > 0 ? availableCount / resources.length : 0;
	return {
		soldOut: availableCount === 0,
		fewLeft: availableCount > 0 && ratio < .2
	};
}
/**
* 构建 SlotPriceInfo 用于价格样式映射
* 从 VenueResourceSlot 的 price 转换为 UI 展示所需的格式
*/
function buildSlotPriceInfo(slot) {
	return {
		price: slot.price ? parseFloat(slot.price) : 0,
		originalPrice: null,
		label: "",
		highlight: false
	};
}
function getPriceColor(params) {
	if (params.highlight) return "text-emerald-600";
	if (params.courtType === "Pickleball") return "vb-primary-text";
	return "text-zinc-800";
}
function getSlotTag(priceInfo) {
	if (priceInfo.highlight || priceInfo.label === "Promo") return {
		text: "pisell2.venue.label.promo",
		className: "bg-emerald-50 text-emerald-700 ring-emerald-200"
	};
	if (priceInfo.label === "Off-Peak") return {
		text: "pisell2.venue.label.off.peak",
		className: "bg-zinc-100 text-zinc-600 ring-zinc-200"
	};
	return null;
}
/**
* 获取列头标签（某个时间点的统一标签）
* 暂时返回 null，后续 API 可扩展返回标签信息
*/
function getColumnTag(resources, slotIndex) {
	return null;
}
function getTableGridStyle(slotCount) {
	return {
		gridTemplateColumns: `120px repeat(${slotCount}, minmax(70px, 1fr))`,
		minWidth: `${120 + slotCount * 70}px`,
		width: "100%"
	};
}
/** 找到第一个有 available 时段的 slot 索引 */
function getFirstAvailableSlotIndex(grid) {
	if (!grid) return 0;
	for (let i = 0; i < grid.timeLabels.length; i++) if (grid.resources.some((r) => r.slots[i] && r.slots[i].status === "available")) return i;
	return 0;
}
//#endregion
exports.buildHourGroups = buildHourGroups;
exports.buildSlotPriceInfo = buildSlotPriceInfo;
exports.formatDateKey = formatDateKey;
exports.formatHourHeader = formatHourHeader;
exports.getColumnTag = getColumnTag;
exports.getDateMetaFromSummary = getDateMetaFromSummary;
exports.getFirstAvailableSlotIndex = getFirstAvailableSlotIndex;
exports.getMonthDays = getMonthDays;
exports.getMonthLabel = getMonthLabel;
exports.getPriceColor = getPriceColor;
exports.getResourceCourtMeta = getResourceCourtMeta;
exports.getSlotTag = getSlotTag;
exports.getTableGridStyle = getTableGridStyle;
exports.getTimeMetaFromGrid = getTimeMetaFromGrid;
exports.getWeekDays = getWeekDays;
exports.getWeekdayShortLabel = getWeekdayShortLabel;
exports.isHourBoundary = isHourBoundary;
