import { RESERVATION_TIMELINE_BUSINESS_RANGE } from "../reservationConstants.js";
import dayjs from "dayjs";
import { getHorizontalAxisSlotCount, getRangeBounds } from "@pisell/materials";
//#region src/plus/pisellReservation/data/reservationDensity.ts
const TIMELINE_RANGE_FORMAT = "YYYY-MM-DD HH:mm";
/**
* 与顶栏 PisellTimeNavigator 一致：按锚定自然日 + 营业区间得到时间轴起止（如 02:00–次日 02:00）。
* 用于 `getTimelineHighlights(列表, startStr, endStr)` 与宿主对齐。
*/
function reservationTimelineRangeStrings(anchorDay, range = RESERVATION_TIMELINE_BUSINESS_RANGE) {
	const [rangeStart, rangeEnd] = getRangeBounds(range, anchorDay.startOf("day"));
	return {
		startStr: rangeStart.format(TIMELINE_RANGE_FORMAT),
		endStr: rangeEnd.format(TIMELINE_RANGE_FORMAT)
	};
}
/**
* 解析宿主 `sales.getTimelineHighlights` 返回值：数字序列（如 [0,0,1,1,2,…]），用于时间轴折线。
* 支持数组、类数组（含 TypedArray）、可迭代对象。
*/
function normalizeSalesTimelineHighlightsRaw(raw) {
	let values;
	if (Array.isArray(raw)) values = raw;
	else if (raw != null && typeof raw === "object") if (Symbol.iterator in raw) try {
		values = Array.from(raw);
	} catch (_unused) {
		return null;
	}
	else {
		const keys = Object.keys(raw).filter((k) => /^\d+$/.test(k)).sort((a, b) => Number(a) - Number(b));
		if (keys.length === 0) return null;
		values = keys.map((k) => raw[k]);
	}
	else return null;
	const out = values.map((v) => {
		const n = typeof v === "number" ? v : Number(v);
		return Number.isFinite(n) ? Math.max(0, Math.round(n)) : 0;
	});
	return out.length > 0 ? out : null;
}
/**
* 将宿主返回的密度序列裁剪/补零到与当前时间轴格数一致（与 PisellTimeNavigator 内 `axis.ticks` 长度对齐）。
*/
function fitTimelineHighlightsToSlotCount(series, slotCount) {
	const n = Math.max(1, slotCount);
	if (series.length === n) return [...series];
	if (series.length > n) return series.slice(0, n);
	return [...series, ...new Array(n - series.length).fill(0)];
}
/** 预约页时间轴在指定锚定日、步长下的横向格数（与 TimeNavigator + 营业 range 一致） */
function reservationTimelineAxisSlotCount(referenceDate, stepMinutes, range = RESERVATION_TIMELINE_BUSINESS_RANGE) {
	return getHorizontalAxisSlotCount(range, referenceDate.startOf("day"), stepMinutes);
}
/**
* 时间轴密度层：按 referenceDate 所在自然日统计预约开始时刻落入的槽位。
*/
function computeReservationDensityBuckets(densityRows, referenceDate, stepMinutes) {
	const day = referenceDate.startOf("day");
	const slotCount = Math.max(1, Math.round(1440 / Math.max(1, stepMinutes)));
	const buckets = new Array(slotCount).fill(0);
	for (const row of densityRows) {
		var _ref, _ref2;
		const raw = (_ref = (_ref2 = row.bookingTimeStart) !== null && _ref2 !== void 0 ? _ref2 : row.start_at) !== null && _ref !== void 0 ? _ref : row.start_date && row.start_time ? `${row.start_date} ${row.start_time}` : void 0;
		if (!raw || typeof raw !== "string") continue;
		const t = dayjs(raw);
		if (!t.isValid() || !t.isSame(day, "day")) continue;
		const idx = Math.min(slotCount - 1, Math.floor((t.hour() * 60 + t.minute()) / stepMinutes));
		buckets[idx] += 1;
	}
	/** 无当天数据时给一条可视 mock 折线，便于验证密度层布局 */
	if (!buckets.some((v) => v > 0)) return buckets.map((_, i) => {
		const waveA = Math.sin(i / Math.max(1, slotCount - 1) * Math.PI * 2);
		const waveB = Math.sin(i / Math.max(1, slotCount - 1) * Math.PI * 6);
		return Math.max(0, Math.round(2 + waveA * 2 + waveB));
	});
	return buckets;
}
//#endregion
export { computeReservationDensityBuckets, fitTimelineHighlightsToSlotCount, normalizeSalesTimelineHighlightsRaw, reservationTimelineAxisSlotCount, reservationTimelineRangeStrings };
