import { getLocale } from "../../../utils/locales.js";
import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import locales_default from "../locales.js";
import { _objectWithoutProperties } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import { request } from "../utils.js";
import useEngineContext from "../../../hooks/useEngineContext.js";
import { getStatistic } from "./serve.js";
import dayjs from "dayjs";
import React, { forwardRef, useEffect, useImperativeHandle, useState } from "react";
import { locales } from "@pisell/utils";
import { PisellStatisticList } from "@pisell/materials";
//#region src/components/booking/dashboard/index.tsx
const _excluded = [
	"otherParams",
	"backgroundColors",
	"labelColor",
	"valueColor",
	"refreshKey",
	"onDataChange"
];
let translationOriginal = (text) => "";
const formatCapacityToChildrenData = (data) => {
	const map = {};
	data.forEach((item) => {
		const name = translationOriginal(item.label) || "default";
		if (!map[name]) map[name] = {
			label: !item.label ? locales.getText("pisell2.booking.dashboard.default") : item.label,
			value: 0
		};
		map[name].value = (map[name].value || 0) + (item.num || 0);
	});
	return Object.values(map).filter((item) => !!item.value).sort((a, b) => b.label.localeCompare(a.label));
};
locales.init(locales_default, getLocale());
const BookingDashboard = forwardRef((props, ref) => {
	var _context$appHelper, _businessUtils$getApp;
	const { otherParams = {}, backgroundColors, labelColor, valueColor, refreshKey, onDataChange } = props, others = _objectWithoutProperties(props, _excluded);
	const context = useEngineContext();
	request.setRequest(context.appHelper.utils.request);
	translationOriginal = context.appHelper.utils.translationOriginal;
	const { businessUtils } = ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const hasTurnstileApp = !!(businessUtils === null || businessUtils === void 0 || (_businessUtils$getApp = businessUtils.getAppByModal) === null || _businessUtils$getApp === void 0 ? void 0 : _businessUtils$getApp.call(businessUtils, "turnstile"));
	const [data, setData] = useState([
		{
			label: locales.getText("pisell2.booking.dashboard.groups-on-site-now"),
			value: "-",
			tip: locales.getText("pisell2.booking.dashboard.groups-on-site-now-tip")
		},
		{
			label: locales.getText("pisell2.booking.dashboard.people-on-site-now"),
			value: "-",
			tip: locales.getText("pisell2.booking.dashboard.people-on-site-now-tip")
		},
		{
			label: locales.getText("pisell2.booking.dashboard.total-visitors-today"),
			value: "-",
			tip: locales.getText("pisell2.booking.dashboard.total-visitors-today-tip")
		},
		{
			label: locales.getText("pisell2.booking.dashboard.ongoing-visitors"),
			value: "-",
			tip: locales.getText("pisell2.booking.dashboard.ongoing-visitors-tip")
		},
		{
			label: locales.getText("pisell2.booking.dashboard.upcoming-visitors"),
			value: "-",
			tip: locales.getText("pisell2.booking.dashboard.upcoming-visitors-tip")
		},
		{
			label: locales.getText("pisell2.booking.dashboard.overdue-visitors"),
			value: "-",
			tip: locales.getText("pisell2.booking.dashboard.overdue-visitors-tip")
		},
		{
			label: locales.getText("pisell2.booking.dashboard.completed-visitors"),
			value: "-",
			tip: locales.getText("pisell2.booking.dashboard.completed-visitors-tip")
		},
		{
			label: locales.getText("pisell2.booking.dashboard.departed-visitors"),
			value: "-",
			tip: locales.getText("pisell2.booking.dashboard.departed-visitors-tip")
		}
	]);
	const [statisticData, setStatisticData] = useState({});
	const getData = function() {
		var _ref = _asyncToGenerator(function* () {
			var _otherParams$booking_;
			console.log("otherParams", otherParams);
			let statisticsTime = (otherParams === null || otherParams === void 0 || (_otherParams$booking_ = otherParams.booking_time_start_between) === null || _otherParams$booking_ === void 0 ? void 0 : _otherParams$booking_[1]) || dayjs().format("YYYY-MM-DD HH:mm:ss");
			/**
			* statisticsTime 规则：
			* - 昨天及昨天之前：固定为该日 23:59:59
			* - 今天：取当前时间
			* - 明天及明天之后：固定为该日 00:00:00
			*/
			const statisticsDay = dayjs(statisticsTime);
			const now = dayjs();
			if (statisticsDay.isValid()) {
				if (statisticsDay.isSame(now, "day")) statisticsTime = now.format("YYYY-MM-DD HH:mm:ss");
				if (statisticsDay.isBefore(now, "day")) statisticsTime = statisticsDay.endOf("day").format("YYYY-MM-DD HH:mm:ss");
				if (statisticsDay.isAfter(now, "day")) statisticsTime = statisticsDay.startOf("day").format("YYYY-MM-DD HH:mm:ss");
			}
			setStatisticData(yield getStatistic({ statistics_time: statisticsTime }));
			calcBookingDataCount();
		});
		return function getData() {
			return _ref.apply(this, arguments);
		};
	}();
	const calcBookingDataCount = () => {
		var _statisticData$total_, _statisticData$total_2, _statisticData$ongoin, _statisticData$ongoin2, _statisticData$upcomi, _statisticData$upcomi2, _statisticData$comple, _statisticData$comple2;
		let groupsOnSiteNow = statisticData.groups_today_num || 0;
		let peopleOnSiteNow = statisticData.people_on_site_now_num || (hasTurnstileApp ? 0 : "-");
		let totalVisitorsToday = ((_statisticData$total_ = statisticData.total_visitors_today) === null || _statisticData$total_ === void 0 ? void 0 : _statisticData$total_.num) || 0;
		let totalVisitorsTodayChildren = ((_statisticData$total_2 = statisticData.total_visitors_today) === null || _statisticData$total_2 === void 0 ? void 0 : _statisticData$total_2.statistics) || [];
		let ongoing = ((_statisticData$ongoin = statisticData.ongoing_visitors) === null || _statisticData$ongoin === void 0 ? void 0 : _statisticData$ongoin.num) || 0;
		let ongoingChildren = ((_statisticData$ongoin2 = statisticData.ongoing_visitors) === null || _statisticData$ongoin2 === void 0 ? void 0 : _statisticData$ongoin2.statistics) || [];
		let upcoming = ((_statisticData$upcomi = statisticData.upcoming_visitors) === null || _statisticData$upcomi === void 0 ? void 0 : _statisticData$upcomi.num) || 0;
		let upcomingChildren = ((_statisticData$upcomi2 = statisticData.upcoming_visitors) === null || _statisticData$upcomi2 === void 0 ? void 0 : _statisticData$upcomi2.statistics) || [];
		let overdue = statisticData.overdue_visitors_num || (hasTurnstileApp ? 0 : "-");
		let completed = ((_statisticData$comple = statisticData.completed_visitors) === null || _statisticData$comple === void 0 ? void 0 : _statisticData$comple.num) || 0;
		let completedChildren = ((_statisticData$comple2 = statisticData.completed_visitors) === null || _statisticData$comple2 === void 0 ? void 0 : _statisticData$comple2.statistics) || [];
		let departed = statisticData.departed_visitors_num || (hasTurnstileApp ? 0 : "-");
		onDataChange === null || onDataChange === void 0 || onDataChange({
			groupsOnSiteNow,
			peopleOnSiteNow,
			totalVisitorsToday,
			ongoing,
			upcoming,
			overdue,
			completed,
			departed
		}, []);
		setData([
			{
				label: locales.getText("pisell2.booking.dashboard.groups-on-site-now"),
				tip: locales.getText("pisell2.booking.dashboard.groups-on-site-now-tip"),
				value: `${groupsOnSiteNow}`
			},
			{
				label: locales.getText("pisell2.booking.dashboard.people-on-site-now"),
				tip: locales.getText("pisell2.booking.dashboard.people-on-site-now-tip"),
				value: `${peopleOnSiteNow}`
			},
			{
				label: locales.getText("pisell2.booking.dashboard.total-visitors-today"),
				tip: locales.getText("pisell2.booking.dashboard.total-visitors-today-tip"),
				value: `${totalVisitorsToday}`,
				children: formatCapacityToChildrenData(totalVisitorsTodayChildren)
			},
			{
				label: locales.getText("pisell2.booking.dashboard.ongoing-visitors"),
				tip: locales.getText("pisell2.booking.dashboard.ongoing-visitors-tip"),
				value: `${ongoing}`,
				children: formatCapacityToChildrenData(ongoingChildren)
			},
			{
				label: locales.getText("pisell2.booking.dashboard.upcoming-visitors"),
				tip: locales.getText("pisell2.booking.dashboard.upcoming-visitors-tip"),
				value: `${upcoming}`,
				children: formatCapacityToChildrenData(upcomingChildren)
			},
			{
				label: locales.getText("pisell2.booking.dashboard.overdue-visitors"),
				tip: locales.getText("pisell2.booking.dashboard.overdue-visitors-tip"),
				value: `${overdue}`
			},
			{
				label: locales.getText("pisell2.booking.dashboard.completed-visitors"),
				tip: locales.getText("pisell2.booking.dashboard.completed-visitors-tip"),
				value: `${completed}`,
				children: formatCapacityToChildrenData(completedChildren)
			},
			{
				label: locales.getText("pisell2.booking.dashboard.departed-visitors"),
				tip: locales.getText("pisell2.booking.dashboard.departed-visitors-tip"),
				value: `${departed}`
			}
		]);
	};
	useEffect(() => {
		getData();
		const timer = setInterval(() => {
			getData();
		}, 60 * 1e3);
		return () => {
			clearInterval(timer);
		};
	}, [JSON.stringify(otherParams), refreshKey]);
	useImperativeHandle(ref, () => ({ refresh: () => {
		debugger;
		getData();
	} }));
	return /* @__PURE__ */ React.createElement(PisellStatisticList, _objectSpread2(_objectSpread2({}, others), {}, {
		data,
		backgroundColors,
		labelColor,
		valueColor
	}));
});
//#endregion
export { BookingDashboard as default };
