import { ALIGN_CONTENT_MAP, ALIGN_MAP, DIRECTION_MAP, JUSTIFY_MAP, WRAP_MAP, calculateFlexBasis, normalizeGap } from "../utils/index.js";
import { useMemo } from "react";
//#region src/components/PisellBasicGrid/hooks/useGridStyle.ts
/**
* 计算 Grid 布局样式的 Hook
* @param params - 布局参数
* @returns 容器样式和子元素样式
* @description
* - 根据传入的布局参数计算容器的 flex 样式
* - 根据 columns 和 gap 计算子元素的 flex-basis
* - 使用 useMemo 缓存计算结果，避免不必要的重复计算
*/
const useGridStyle = (params) => {
	const { direction = "horizontal", wrap = "nowrap", columns, equalWidth = false, gap, rowGap, columnGap, justify = "start", align = "stretch", alignContent = "stretch" } = params;
	return {
		containerStyle: useMemo(() => {
			const style = {
				display: "flex",
				flexDirection: DIRECTION_MAP[direction],
				flexWrap: WRAP_MAP[wrap],
				justifyContent: JUSTIFY_MAP[justify],
				alignItems: ALIGN_MAP[align]
			};
			if (wrap !== "nowrap") style.alignContent = ALIGN_CONTENT_MAP[alignContent];
			const normalizedGap = normalizeGap(gap);
			const normalizedRowGap = normalizeGap(rowGap);
			const normalizedColumnGap = normalizeGap(columnGap);
			if (normalizedRowGap || normalizedColumnGap) {
				if (normalizedRowGap) style.rowGap = normalizedRowGap;
				if (normalizedColumnGap) style.columnGap = normalizedColumnGap;
				if (normalizedGap) {
					if (!normalizedRowGap) style.rowGap = normalizedGap;
					if (!normalizedColumnGap) style.columnGap = normalizedGap;
				}
			} else if (normalizedGap) style.gap = normalizedGap;
			return style;
		}, [
			direction,
			wrap,
			justify,
			align,
			alignContent,
			gap,
			rowGap,
			columnGap
		]),
		itemStyle: useMemo(() => {
			const style = { alignItems: "stretch" };
			if (columns !== void 0 && columns > 0) {
				style.flexBasis = calculateFlexBasis(columns, columnGap !== null && columnGap !== void 0 ? columnGap : gap);
				style.flexGrow = 0;
				style.flexShrink = 0;
			} else if (equalWidth) {
				style.flexGrow = 1;
				style.flexShrink = 1;
				style.flexBasis = 0;
			} else {
				style.flexGrow = 0;
				style.flexShrink = 0;
				style.flexBasis = "auto";
			}
			return style;
		}, [
			columns,
			equalWidth,
			gap,
			columnGap
		])
	};
};
//#endregion
export { useGridStyle as default };
