import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import dayjs from "dayjs";
import { locales } from "@pisell/utils";
//#region src/components/schedules/products/utils.ts
/**
* @title: 格式化商品为二维结构
* @description: 
* @param {any} list
* @param {any} filterItem
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-03-05 18:01
*/
const formatProductListByCategory = (list, filterItem) => {
	let _list = list || [];
	if (filterItem) _list = _list.filter(filterItem);
	let categoryObject = {};
	for (let i = 0; i < _list.length; i++) {
		const productItem = _list[i];
		if ((productItem.category || []).length) for (let j = 0; j < productItem.category.length; j++) {
			let categoryItem = productItem.category[j];
			if (!categoryObject[categoryItem.id]) categoryObject[categoryItem.id] = _objectSpread2(_objectSpread2({}, categoryItem), {}, { products: [] });
			categoryObject[categoryItem.id] = _objectSpread2(_objectSpread2({}, categoryItem), {}, { products: [...categoryObject[categoryItem.id].products, productItem] });
		}
		else {
			if (!categoryObject["other"]) categoryObject["other"] = {
				id: 0,
				title: locales.getText("pisell2.text.other"),
				name: locales.getText("pisell2.text.other"),
				cover: "",
				products: []
			};
			categoryObject["other"] = _objectSpread2(_objectSpread2({}, categoryObject["other"]), {}, { products: [...categoryObject["other"].products, productItem] });
		}
	}
	return Object.values(categoryObject);
};
/**
* @title: 获取背景色
* @description: 
* @param {string} colors
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-03-05 18:02
*/
const formatBackgroundColors = (colors) => {
	let _colors = Array.from(new Set(colors.map((color) => color === null || color === void 0 ? void 0 : color.toLocaleLowerCase()) || [])).filter((d) => !!d);
	let colorStr = _colors.map((color, index) => {
		return `${color} ${index * 100 / _colors.length}% ${(index + 1) * 100 / _colors.length}%`;
	}).join(", ");
	if (colorStr) return `conic-gradient(${colorStr})`;
	return "";
};
/**
* @title: 获取 schedules 的所有颜色
* @description: 
* @param {any} schedules
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-03-05 18:02
*/
const getScheduleColors = (schedules) => {
	return formatBackgroundColors(schedules.map((d) => d.color));
};
/**
* @title: 获取数组内最小的时间和最大的时间
* @description: 
* @param {any} array
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-03-05 18:04
*/
const getMinTimeRange = (array) => {
	if (!array.length) return {
		minTime: "",
		maxTime: ""
	};
	let minTime = array[0].minTime;
	let maxTime = array[0].maxTime;
	for (let i = 1; i < array.length; i++) {
		let startTime = array[i].minTime;
		let endTime = array[i].maxTime;
		if (startTime.isBefore(minTime)) minTime = startTime;
		if (endTime.isAfter(maxTime)) maxTime = endTime;
	}
	return {
		minTime,
		maxTime
	};
};
/**
* @title: 获取店铺的营业时间 开始-结束
* @description: 
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-03-05 18:04
*/
const getShopOpeningHoursRange = () => {
	let operating_day_boundary = {
		time: "23:59",
		type: "end_time"
	};
	if (localStorage.getItem("shopOpeningHoursObject")) operating_day_boundary = JSON.parse(localStorage.getItem("shopOpeningHoursObject") || "");
	let time = dayjs(operating_day_boundary.time, "HH:mm");
	let type = operating_day_boundary.type;
	let startDate = dayjs(time);
	if (type === "end_time") startDate = startDate.subtract(1, "days");
	let startDay = dayjs(startDate);
	let endDate = dayjs(time).add(1, "days").subtract(1, "second");
	if (type === "end_time") endDate = endDate.subtract(1, "days");
	let endDay = dayjs(endDate);
	return {
		shopStartDate: startDay,
		shopEndDate: endDay,
		shopStartTime: startDay.format("HH:mm"),
		shopEndTime: endDay.format("HH:mm")
	};
};
/**
* @title: 将时间范围格式化
* @description: 
* @param {any} range
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-03-05 18:05
*/
const getMinTimeRangeStr = (range) => {
	if (!range.minTime || !range.maxTime) return "";
	if (range.minTime.format("HH:mm") === "00:00" && range.maxTime.format("HH:mm") === "23:59") return locales.getText("pisell2.text.schedules.all-day");
	let { shopStartDate, shopEndDate } = getShopOpeningHoursRange();
	let minTime = "";
	let maxTime = "";
	if (range.minTime.format("YYYY-MM-DD HH:mm") === shopStartDate.format("YYYY-MM-DD HH:mm")) minTime = "open";
	else minTime = range.minTime.format("hh:mm a");
	if (range.maxTime.format("YYYY-MM-DD HH:mm") === shopEndDate.format("YYYY-MM-DD HH:mm")) maxTime = "close";
	else maxTime = range.maxTime.format("hh:mm a");
	return `${minTime} - ${maxTime}`;
};
/**
* @title: 获取可用的 schedule
* @description: 
* @param {any} schedule
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-03-05 18:05
*/
const getUsableSchedules = (schedule) => {
	if (!schedule) return true;
	if (!schedule.minTime || !schedule.maxTime) return false;
	if (dayjs().isAfter(schedule.maxTime)) return false;
	return true;
};
const formatProductItemSchedules = (item, scheduleMap) => {
	let total = getMinTimeRange((item["schedule.ids"] || []).map((t) => {
		var _scheduleMap$t, _scheduleMap$t2;
		return {
			id: t,
			minTime: (_scheduleMap$t = scheduleMap[t]) === null || _scheduleMap$t === void 0 ? void 0 : _scheduleMap$t.minTime,
			maxTime: (_scheduleMap$t2 = scheduleMap[t]) === null || _scheduleMap$t2 === void 0 ? void 0 : _scheduleMap$t2.maxTime
		};
	}).filter((t) => !!t.minTime && !!t.maxTime));
	let str = getMinTimeRangeStr(total);
	return _objectSpread2(_objectSpread2({}, item), {}, { _schedules: {
		minTime: total.minTime,
		maxTime: total.maxTime,
		str
	} });
};
/**
* 1. 获取每个日程的最小开始时间和最大结束时间.
* 2. 
*/
const formatProductSchedules = (products, scheduleMap) => {
	return products.map((d) => formatProductItemSchedules(d, scheduleMap));
};
const updateProductById = ({ products, scheduleMap, id, schedule }) => {
	return products.map((d) => {
		return d.id === id ? formatProductItemSchedules(_objectSpread2(_objectSpread2({}, d), {}, { "schedule.ids": schedule }), scheduleMap) : d;
	});
};
//#endregion
export { formatBackgroundColors, formatProductListByCategory, formatProductSchedules, getScheduleColors, getUsableSchedules, updateProductById };
