const require_index = require("../../../../locales/index.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
//#region src/components/pisellRecordBoard/shellFrame/ResourceWall/venueWallUtils.ts
const VENUE_WALL_STATUS_KEYS = [
	"inUse",
	"endingSoon",
	"expired",
	"available",
	"boardingSoon",
	"late"
];
/**
* 六态默认色与占位英文；`getVenueWallStatusToneMap` 会将 label/note 换为 i18n，此处仅作 merge 基底。
*/
const VENUE_WALL_STATUS_COLOR_MAP = {
	inUse: {
		key: "inUse",
		label: "In Use",
		note: "This court is currently in use.",
		color: "#6f4ae6",
		noteBg: "rgba(111,74,230,0.92)"
	},
	endingSoon: {
		key: "endingSoon",
		label: "Ending Soon",
		note: "Please leave on time to avoid overtime charges.",
		color: "#f59e0b",
		noteBg: "rgba(245,158,11,0.96)"
	},
	expired: {
		key: "expired",
		label: "Expired",
		note: "This booking has ended. Please clear the court.",
		color: "#ef4444",
		noteBg: "rgba(239,68,68,0.96)"
	},
	available: {
		key: "available",
		label: "Available",
		note: "This court is now available.",
		color: "#94a3b8",
		noteBg: "rgba(148,163,184,0.22)"
	},
	boardingSoon: {
		key: "boardingSoon",
		label: "Boarding Soon",
		note: "Please check in to start.",
		color: "#6f4ae6",
		noteBg: "rgba(111,74,230,0.18)"
	},
	late: {
		key: "late",
		label: "Late",
		note: "Please check in as soon as possible.",
		color: "#ef4444",
		noteBg: "rgba(239,68,68,0.22)"
	}
};
/** 将 `#RGB` / `#RRGGBB` 转为 `rgba(...)`，供渐变与半透明底 */
function alphaColor(hex, alpha) {
	const clean = hex.replace("#", "");
	const full = clean.length === 3 ? clean.split("").map((c) => c + c).join("") : clean;
	const num = parseInt(full, 16);
	return `rgba(${num >> 16 & 255}, ${num >> 8 & 255}, ${num & 255}, ${alpha})`;
}
/** 将数值限制在 [min, max]（布局行列、主文案比例等） */
const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
/** 由卡片字段推导状态键（与演示页 test.tsx 规则一致） */
function deriveVenueWallStatus(card) {
	const s = card.status;
	if (s === "available") return "available";
	if (s === "boardingSoon") return "boardingSoon";
	if (s === "late") return "late";
	if (s === "endingSoon") return "endingSoon";
	if (s === "expired") return "expired";
	if (s === "inUse") return "inUse";
	/** 兼容旧数据：无 status 时退回 remainingMin 推断（不再看 light 字段） */
	if (card.currentBooking === "Available to Book") return "available";
	const rm = card.remainingMin;
	if (rm != null && rm <= 0) return "expired";
	if (rm != null && rm <= 10) return "endingSoon";
	return "inUse";
}
/**
* ToolBar Sort 写入的 `searchParams.sort`（如 `status:asc:string`）拆出字段名与升降序。
*/
function parseRecordBoardToolbarSortParam(sort) {
	if (sort == null || typeof sort !== "string") return null;
	const idx = sort.indexOf(":");
	if (idx <= 0) return null;
	const field = sort.slice(0, idx);
	const rest = sort.slice(idx + 1);
	const dirSep = rest.indexOf(":");
	const dirRaw = dirSep >= 0 ? rest.slice(0, dirSep) : rest;
	if (dirRaw !== "asc" && dirRaw !== "desc") return null;
	return {
		field,
		dir: dirRaw
	};
}
/** 当 Sort 字段名匹配这些 key（忽略大小写）时，资源墙按占用态重排卡片 */
const DEFAULT_RESOURCE_WALL_STATUS_SORT_FIELD_KEYS = ["status"];
function matchesResourceWallStatusSortField(sortField, keys) {
	const n = sortField.trim().toLowerCase();
	return keys.some((k) => k.trim().toLowerCase() === n);
}
/**
* 用于资源墙排序：占用/将结/已结束为 1，其余为 0（与 {@link deriveVenueWallStatus} 一致）。
* asc → 空闲侧在前；desc → 占用侧在前。
*/
function venueWallOccupiedSortRank(card) {
	const k = deriveVenueWallStatus(card);
	return k === "inUse" || k === "endingSoon" || k === "expired" ? 1 : 0;
}
function formatDurationHM(minAbs) {
	const safe = Number.isFinite(minAbs) ? Math.max(0, Math.floor(minAbs)) : 0;
	if (safe < 60) return `${safe}m`;
	const h = Math.floor(safe / 60);
	const m = safe % 60;
	/** `11h 28m`：h 与分钟之间加空格，避免 `11h28m` 连成一体难读 */
	return `${h}h ${String(m).padStart(2, "0")}m`;
}
/**
* 将「剩余/超支分钟数」格式化为跑码条/主副标题用短文案（走 i18n 模板，负值表示 overtime）。
*/
function formatVenueWallCountdown(min) {
	if (min < 0) {
		const n = formatDurationHM(Math.abs(min));
		return require_index.getText("pisell-resource-wall-countdown-overtime").replace("{n}", n);
	}
	if (min === 0) return require_index.getText("pisell-resource-wall-countdown-ending-now");
	return require_index.getText("pisell-resource-wall-countdown-left").replace("{n}", formatDurationHM(min));
}
function cloneTone(t) {
	return require_objectSpread2._objectSpread2({}, t);
}
/**
* 合并默认六态配色与业务覆盖，供 {@link VenueWallCard} 与 Control Panel 表单初值共用。
*/
function getVenueWallStatusToneMap(overrides) {
	const out = {};
	for (const key of VENUE_WALL_STATUS_KEYS) {
		const base = cloneTone(VENUE_WALL_STATUS_COLOR_MAP[key]);
		base.label = require_index.getText(`pisell-resource-wall-tone-${key}-label`);
		base.note = require_index.getText(`pisell-resource-wall-tone-${key}-note`);
		const patch = overrides === null || overrides === void 0 ? void 0 : overrides[key];
		out[key] = patch ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, base), patch), {}, { key }) : base;
	}
	return out;
}
/**
* 将编辑后的完整六态表压成「相对默认」的 overrides，便于持久化与 props 传递。
*/
function diffVenueWallStatusToneOverrides(draft) {
	const defaults = getVenueWallStatusToneMap();
	const out = {};
	for (const k of VENUE_WALL_STATUS_KEYS) {
		const d = draft[k];
		const b = defaults[k];
		const patch = {};
		if (d.label !== b.label) patch.label = d.label;
		if (d.note !== b.note) patch.note = d.note;
		if (d.color !== b.color) patch.color = d.color;
		if (d.noteBg !== b.noteBg) patch.noteBg = d.noteBg;
		if (Object.keys(patch).length > 0) out[k] = patch;
	}
	return out;
}
//#endregion
exports.DEFAULT_RESOURCE_WALL_STATUS_SORT_FIELD_KEYS = DEFAULT_RESOURCE_WALL_STATUS_SORT_FIELD_KEYS;
exports.VENUE_WALL_STATUS_KEYS = VENUE_WALL_STATUS_KEYS;
exports.alphaColor = alphaColor;
exports.clamp = clamp;
exports.deriveVenueWallStatus = deriveVenueWallStatus;
exports.diffVenueWallStatusToneOverrides = diffVenueWallStatusToneOverrides;
exports.formatDurationHM = formatDurationHM;
exports.formatVenueWallCountdown = formatVenueWallCountdown;
exports.getVenueWallStatusToneMap = getVenueWallStatusToneMap;
exports.matchesResourceWallStatusSortField = matchesResourceWallStatusSortField;
exports.parseRecordBoardToolbarSortParam = parseRecordBoardToolbarSortParam;
exports.venueWallOccupiedSortRank = venueWallOccupiedSortRank;
