/**
 * Wet-sand edge blend toward liquid water.
 *
 * Within the outer band of a sand tile facing water (cardinal or diagonal),
 * pixels lerp toward the wet sprite: edge = 100% wet, beyond the band = dry.
 * Default band is 60% (client graphics setting). In winter, shallow water is ice —
 * only deep water counts (see isLiquidWaterGround).
 */

/** Default wet shoreline width as a fraction of the tile (60%). */
export const WET_SAND_BAND_FRACTION = 0.6;

/** All eight neighbors — diagonals wet the corner fan toward water. */
export const WET_SAND_NEIGHBOR_DIRS = Object.freeze([
	Object.freeze({ ndx: -1, ndy: -1 }),
	Object.freeze({ ndx: 0, ndy: -1 }),
	Object.freeze({ ndx: 1, ndy: -1 }),
	Object.freeze({ ndx: -1, ndy: 0 }),
	Object.freeze({ ndx: 1, ndy: 0 }),
	Object.freeze({ ndx: -1, ndy: 1 }),
	Object.freeze({ ndx: 0, ndy: 1 }),
	Object.freeze({ ndx: 1, ndy: 1 })
]);

/**
 * Distance in pixels from the edge(s) facing neighbor (ndx, ndy).
 * Cardinal: axis distance from that edge.
 * Diagonal: Chebyshev max so the wet region is a square fan from the corner.
 * @returns {number|null}
 */
export function wetSandEdgeDistance (px, py, ndx, ndy, size = 16)
{
	const last = size - 1;
	let edgeX = null;
	let edgeY = null;
	if (ndx > 0)
	{ edgeX = last - px; }
	else if (ndx < 0)
	{ edgeX = px; }
	if (ndy > 0)
	{ edgeY = last - py; }
	else if (ndy < 0)
	{ edgeY = py; }

	if (ndx !== 0 && ndy !== 0)
	{
		if (edgeX == null || edgeY == null)
		{ return null; }
		return Math.max(edgeX, edgeY);
	}
	if (ndx !== 0)
	{ return edgeX; }
	if (ndy !== 0)
	{ return edgeY; }
	return null;
}

/**
 * Wet blend weight for pixel (px, py) toward water neighbor (ndx, ndy).
 * 1 at the abutting edge/corner, 0 beyond `bandFraction` of the tile.
 * @param {number} [bandFraction=WET_SAND_BAND_FRACTION]
 */
export function wetSandBlendWeight (px, py, ndx, ndy, size = 16, bandFraction = WET_SAND_BAND_FRACTION)
{
	const dist = wetSandEdgeDistance(px, py, ndx, ndy, size);
	if (dist == null)
	{ return 0; }
	const band = Math.max(0, Number(bandFraction) || 0) * size;
	if (!(band > 0) || dist >= band)
	{ return 0; }
	return 1 - dist / band;
}

/**
 * Max wetness from all liquid-water neighbor directions.
 * @param {Array<{ ndx: number, ndy: number }>} waterDirs
 * @param {number} [bandFraction=WET_SAND_BAND_FRACTION]
 */
export function wetSandWetness (px, py, waterDirs, size = 16, bandFraction = WET_SAND_BAND_FRACTION)
{
	let max = 0;
	for (const dir of waterDirs ?? [])
	{
		const w = wetSandBlendWeight(px, py, dir.ndx, dir.ndy, size, bandFraction);
		if (w > max)
		{ max = w; }
	}
	return max;
}

/**
 * Lerp dry → wet RGBA by wetness t in [0,1].
 * @param {{ r: number, g: number, b: number, a: number }} dry
 * @param {{ r: number, g: number, b: number, a: number }} wet
 */
export function mixWetSandPixel (dry, wet, t)
{
	const w = Math.max(0, Math.min(1, Number(t) || 0));
	if (w <= 0)
	{ return dry; }
	if (w >= 1)
	{ return wet; }
	const keep = 1 - w;
	return {
		r: Math.round(dry.r * keep + wet.r * w),
		g: Math.round(dry.g * keep + wet.g * w),
		b: Math.round(dry.b * keep + wet.b * w),
		a: Math.round(dry.a * keep + wet.a * w)
	};
}
