const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
require("./ShiftPreview.less");
//#region src/plus/saasAttendance/settings/components/shiftModal/ShiftPreview.tsx
const WEEKDAYS = [
	1,
	2,
	3,
	4,
	5,
	6,
	7
];
const getWeekdayText = (weekday) => {
	return _pisell_utils.locales.getText(`pisell2.attendance.group-settings.shift-schedule.modal.weekday.${weekday}`);
};
const parseMinutes = (hhmm) => {
	const [h, m] = (hhmm || "").split(":").map((v) => Number(v));
	if (!Number.isFinite(h) || !Number.isFinite(m)) return null;
	return h * 60 + m;
};
const formatClock = (hhmm) => {
	const totalMinutes = parseMinutes(hhmm);
	if (totalMinutes === null) return "";
	const hours24 = Math.floor(totalMinutes / 60) % 24;
	const minutes = totalMinutes % 60;
	const suffix = hours24 >= 12 ? _pisell_utils.locales.getText("pisell2.attendance.group-settings.shift-schedule.preview.period.pm") : _pisell_utils.locales.getText("pisell2.attendance.group-settings.shift-schedule.preview.period.am");
	return `${hours24 % 12 === 0 ? 12 : hours24 % 12}:${String(minutes).padStart(2, "0")} ${suffix}`;
};
const formatWeeklyHours = (totalMinutes) => {
	const hours = totalMinutes / 60;
	if (Number.isInteger(hours)) return `${hours}`;
	return `${hours.toFixed(2)}`.replace(/\.?0+$/, "");
};
const formatDuration = (totalMinutes) => {
	const hours = Math.floor(totalMinutes / 60);
	const minutes = totalMinutes % 60;
	return _pisell_utils.locales.getText("pisell2.attendance.group-settings.shift-schedule.preview.duration")(hours, minutes);
};
const ShiftPreview = (props) => {
	const schedule = Array.isArray(props.value) ? props.value : [];
	const previewDays = (0, react.useMemo)(() => {
		const byWeekday = /* @__PURE__ */ new Map();
		schedule.forEach((item) => {
			if (item && typeof item.weekday === "number") byWeekday.set(item.weekday, item);
		});
		return WEEKDAYS.map((weekday) => {
			const item = byWeekday.get(weekday);
			const times = Array.isArray(item === null || item === void 0 ? void 0 : item.times) ? item.times : [];
			return {
				weekday,
				timeRanges: times.map((time) => {
					const startText = formatClock(time === null || time === void 0 ? void 0 : time.start);
					const endText = formatClock(time === null || time === void 0 ? void 0 : time.end);
					if (!startText || !endText) return null;
					return {
						startText,
						endText
					};
				}).filter(Boolean),
				totalMinutes: times.reduce((sum, time) => {
					const start = parseMinutes(time === null || time === void 0 ? void 0 : time.start);
					const end = parseMinutes(time === null || time === void 0 ? void 0 : time.end);
					if (start === null || end === null || end <= start) return sum;
					return sum + (end - start);
				}, 0)
			};
		});
	}, [schedule]);
	const workingDays = previewDays.filter((day) => day.totalMinutes > 0).length;
	const weeklyMinutes = previewDays.reduce((sum, day) => sum + day.totalMinutes, 0);
	return /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-preview" }, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-summary" }, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-summary-card" }, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-summary-label" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.shift-schedule.preview.working-days")), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-summary-value" }, workingDays), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-summary-unit" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.shift-schedule.preview.days-per-week"))), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-summary-card" }, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-summary-label" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.shift-schedule.preview.weekly-hours")), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-summary-value" }, formatWeeklyHours(weeklyMinutes)), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-summary-unit" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.shift-schedule.preview.hours")))), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-table" }, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-table-head" }, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-table-cell" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.shift-schedule.preview.table.day")), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-table-cell" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.shift-schedule.preview.table.shift-times")), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-table-cell" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.shift-schedule.preview.table.duration"))), previewDays.map((day) => /* @__PURE__ */ react.default.createElement("div", {
		key: day.weekday,
		className: "saas-attendance-group-settings__shift-table-row"
	}, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-table-cell" }, getWeekdayText(day.weekday)), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-table-cell" }, day.timeRanges.length > 0 ? /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-times" }, day.timeRanges.map((range, index) => /* @__PURE__ */ react.default.createElement("div", {
		key: `${day.weekday}-${index}`,
		className: "saas-attendance-group-settings__shift-time-tag"
	}, /* @__PURE__ */ react.default.createElement("span", { className: "saas-attendance-group-settings__shift-time-text" }, range.startText), /* @__PURE__ */ react.default.createElement("span", { className: "saas-attendance-group-settings__shift-time-arrow" }, "→"), /* @__PURE__ */ react.default.createElement("span", { className: "saas-attendance-group-settings__shift-time-text" }, range.endText)))) : /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-empty" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.shift-schedule.preview.no-shift"))), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-table-cell" }, day.totalMinutes > 0 ? /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-duration" }, formatDuration(day.totalMinutes)) : /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__shift-duration-empty" }, formatDuration(0)))))));
};
//#endregion
exports.default = ShiftPreview;
