/**  克隆对象 */
export async function cloneObjects(objects = []) {
	const arrs = []
	for (const obj of objects) {
		const clonedObj = await new Promise((resolve, reject) => {
			obj.clone((v) => {
				v.set({
					left: obj.left,
					top: obj.top,
					width: obj.width,
					height: obj.height,
					scaleX: obj.scaleX,
					scaleY: obj.scaleY,
					angle: obj.angle,
				})
				resolve(v)
			})
		})
		arrs.push(clonedObj)
	}
	return arrs
}


/**  获取组合对象边界 */
export async function getObjectsBoundingBox(objects = []) {
	const box1 = { x: Infinity, y: Infinity }
	const box2 = { x: -Infinity, y: -Infinity }
	objects.forEach(obj => {
		const rect = obj.getBoundingRect()
		box1.x = Math.min(box1.x, rect.left)
		box1.y = Math.min(box1.y, rect.top)
		box2.x = Math.max(box2.x, rect.left + rect.width)
		box2.y = Math.max(box2.y, rect.top + rect.height)
	})
	return {
		left: box1.x,
		top: box1.y,
		width: box2.x - box1.x,
		height: box2.y - box1.y,
	}
}

/** 获取五角星数组 */
export function getStarArr(width = 0, height = 0) {
	const arr = [
		{ x: 0, y: -0.5 },     // 顶点0 (上)
		{ x: 0.15, y: -0.15 },    // 顶点1 (内)
		{ x: 0.50, y: -0.15 },    // 顶点2 (右上外)
		{ x: 0.20, y: 0.10 },     // 顶点3 (内)
		{ x: 0.30, y: 0.50 },     // 顶点4 (右下外)
		{ x: 0.0, y: 0.25 },      // 顶点5 (内)
		{ x: -0.30, y: 0.50 },    // 顶点6 (左下外)
		{ x: -0.20, y: 0.10 },    // 顶点7 (内)
		{ x: -0.50, y: -0.15 },   // 顶点8 (左上外)
		{ x: -0.15, y: -0.15 }    // 顶点9 (内)
	]
	return arr.map(item => ({
		x: item.x * width,
		y: item.y * height,
	}))
}
/** 获取箭头路径 */
export function getArrowPath(width = 0, height = 0) {
	const arr = [
		["M", 0, height / 2],
		["L", width, height / 2],
		["M", width - 8, 0],
		["L", width, height / 2],
		["L", width - 8, height],
	]
	var path = ""
	arr.forEach(item => {
		path += item.join(" ") + " "
	})
	return path
}

/** 计算两点之间的距离 */
export function distance(x1, y1, x2, y2) {
	const dx = x2 - x1;
	const dy = y2 - y1;
	return Math.sqrt(dx * dx + dy * dy);
}
/** 计算两点之间的角度（角度） */
export function angleBetweenPointsDegrees(x1, y1, x2, y2) {
	const dx = x2 - x1;
	const dy = y2 - y1;

	// 计算弧度并转换为角度
	const rad = Math.atan2(dy, dx);
	const deg = rad * 180 / Math.PI;

	return deg;
}
