<?php
/**
 * Brentwood Image Gallery — brick-layout placement helpers.
 *
 * The "brick" template: even-index columns (0,2,4…) hold two stacked images
 * (one row each); odd-index columns (1,3…) hold a single tall image spanning two
 * rows. Filling order is column by column within each two-row group, which
 * reproduces the brentwood.ca/arts/dance arrangement.
 *
 * Mirrored in blocks/image-gallery/editor.js (keep the two in sync).
 *
 * @package kadence-child
 */

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'bw_gallery_brick_template' ) ) {
	/**
	 * One two-row group's worth of cell descriptors for the given column count.
	 *
	 * @param int $cols Number of columns (1–8).
	 * @return array<int,array{col:int,rowOff:int,span:int}>
	 */
	function bw_gallery_brick_template( $cols ) {
		$cols = max( 1, min( 8, (int) $cols ) );
		$t    = array();
		for ( $c = 0; $c < $cols; $c++ ) {
			if ( 0 === $c % 2 ) {
				$t[] = array( 'col' => $c, 'rowOff' => 0, 'span' => 1 );
				$t[] = array( 'col' => $c, 'rowOff' => 1, 'span' => 1 );
			} else {
				$t[] = array( 'col' => $c, 'rowOff' => 0, 'span' => 2 );
			}
		}
		return $t;
	}
}

if ( ! function_exists( 'bw_gallery_brick_position' ) ) {
	/**
	 * CSS grid placement (1-based) for the i-th item in the brick layout.
	 *
	 * @param int   $i        Zero-based item ordinal.
	 * @param array $template Output of bw_gallery_brick_template().
	 * @param int   $per      count( $template ) (items per two-row group).
	 * @return array{col:int,rowStart:int,rowEnd:int}
	 */
	function bw_gallery_brick_position( $i, $template, $per ) {
		$per   = max( 1, (int) $per );
		$t     = $template[ $i % $per ];
		$group = (int) floor( $i / $per );
		$start = $group * 2 + 1 + (int) $t['rowOff'];
		return array(
			'col'      => (int) $t['col'] + 1,
			'rowStart' => $start,
			'rowEnd'   => $start + (int) $t['span'],
		);
	}
}

if ( ! function_exists( 'bw_gallery_mosaic_template' ) ) {
	/**
	 * One group of the "mosaic" / featured layout (mirrors
	 * brentwood.ca/arts/musical): a fixed 3-column, 3-row group of 7 cells — a
	 * tall image (left, 2 rows), two small (top-right), one wide (2 cols, middle
	 * row), then a regular bottom row of three. Cells carry colSpan + rowSpan.
	 *
	 * Mirrored in blocks/image-gallery/editor.js (keep the two in sync).
	 *
	 * @return array<int,array{col:int,rowOff:int,colSpan:int,rowSpan:int}>
	 */
	function bw_gallery_mosaic_template() {
		return array(
			array( 'col' => 0, 'rowOff' => 0, 'colSpan' => 1, 'rowSpan' => 2 ), // tall left
			array( 'col' => 1, 'rowOff' => 0, 'colSpan' => 1, 'rowSpan' => 1 ), // small
			array( 'col' => 2, 'rowOff' => 0, 'colSpan' => 1, 'rowSpan' => 1 ), // small
			array( 'col' => 1, 'rowOff' => 1, 'colSpan' => 2, 'rowSpan' => 1 ), // wide
			array( 'col' => 0, 'rowOff' => 2, 'colSpan' => 1, 'rowSpan' => 1 ), // bottom row
			array( 'col' => 1, 'rowOff' => 2, 'colSpan' => 1, 'rowSpan' => 1 ),
			array( 'col' => 2, 'rowOff' => 2, 'colSpan' => 1, 'rowSpan' => 1 ),
		);
	}
}

if ( ! function_exists( 'bw_gallery_feature_template' ) ) {
	/**
	 * One group of the "feature" layout (mirrors
	 * brentwood.ca/athletics/outdoor-pursuits): a fixed 3-column, 3-row group of 5
	 * cells — a tall image (left, 2 rows), two wide images (right, 2 cols each,
	 * stacked), then a wide image (bottom-left, 2 cols) beside a small one
	 * (bottom-right). Cells carry colSpan + rowSpan.
	 *
	 * Mirrored in blocks/image-gallery/editor.js (keep the two in sync).
	 *
	 * @return array<int,array{col:int,rowOff:int,colSpan:int,rowSpan:int}>
	 */
	function bw_gallery_feature_template() {
		return array(
			array( 'col' => 0, 'rowOff' => 0, 'colSpan' => 1, 'rowSpan' => 2 ), // tall left
			array( 'col' => 1, 'rowOff' => 0, 'colSpan' => 2, 'rowSpan' => 1 ), // wide top-right
			array( 'col' => 1, 'rowOff' => 1, 'colSpan' => 2, 'rowSpan' => 1 ), // wide mid-right
			array( 'col' => 0, 'rowOff' => 2, 'colSpan' => 2, 'rowSpan' => 1 ), // wide bottom-left
			array( 'col' => 2, 'rowOff' => 2, 'colSpan' => 1, 'rowSpan' => 1 ), // small bottom-right
		);
	}
}

if ( ! function_exists( 'bw_gallery_feature_position' ) ) {
	/**
	 * CSS grid placement (1-based, with col/row spans) for the i-th item in the
	 * feature layout. Complete 5-image groups use the feature template; any
	 * trailing images flow as a plain 3-column grid so a partial last group never
	 * leaves a lone tall image or big gaps.
	 *
	 * @param int   $i        Zero-based item ordinal.
	 * @param array $template Output of bw_gallery_feature_template().
	 * @param int   $count    Total filled images.
	 * @return array{colStart:int,colEnd:int,rowStart:int,rowEnd:int}
	 */
	function bw_gallery_feature_position( $i, $template, $count ) {
		$per         = max( 1, count( $template ) );
		$rows_group  = 3;
		$full_groups = (int) floor( (int) $count / $per );

		if ( $i < $full_groups * $per ) {
			$t     = $template[ $i % $per ];
			$group = (int) floor( $i / $per );
			$row   = $group * $rows_group + 1 + (int) $t['rowOff'];
			$col   = (int) $t['col'] + 1;
			return array(
				'colStart' => $col,
				'colEnd'   => $col + (int) $t['colSpan'],
				'rowStart' => $row,
				'rowEnd'   => $row + (int) $t['rowSpan'],
			);
		}

		// Remainder → regular 3-column flow below the last full group.
		$r   = $i - $full_groups * $per;
		$row = $full_groups * $rows_group + 1 + (int) floor( $r / 3 );
		$col = ( $r % 3 ) + 1;
		return array(
			'colStart' => $col,
			'colEnd'   => $col + 1,
			'rowStart' => $row,
			'rowEnd'   => $row + 1,
		);
	}
}

if ( ! function_exists( 'bw_gallery_mosaic_position' ) ) {
	/**
	 * CSS grid placement (1-based, with col/row spans) for the i-th item in the
	 * mosaic layout. Complete 7-image groups use the feature template; any
	 * trailing images flow as a plain 3-column grid so a partial last group never
	 * leaves a lone tall image or big gaps.
	 *
	 * @param int   $i        Zero-based item ordinal.
	 * @param array $template Output of bw_gallery_mosaic_template().
	 * @param int   $count    Total filled images.
	 * @return array{colStart:int,colEnd:int,rowStart:int,rowEnd:int}
	 */
	function bw_gallery_mosaic_position( $i, $template, $count ) {
		$per         = max( 1, count( $template ) );
		$rows_group  = 3;
		$full_groups = (int) floor( (int) $count / $per );

		if ( $i < $full_groups * $per ) {
			$t     = $template[ $i % $per ];
			$group = (int) floor( $i / $per );
			$row   = $group * $rows_group + 1 + (int) $t['rowOff'];
			$col   = (int) $t['col'] + 1;
			return array(
				'colStart' => $col,
				'colEnd'   => $col + (int) $t['colSpan'],
				'rowStart' => $row,
				'rowEnd'   => $row + (int) $t['rowSpan'],
			);
		}

		// Remainder → regular 3-column flow below the last full group.
		$r   = $i - $full_groups * $per;
		$row = $full_groups * $rows_group + 1 + (int) floor( $r / 3 );
		$col = ( $r % 3 ) + 1;
		return array(
			'colStart' => $col,
			'colEnd'   => $col + 1,
			'rowStart' => $row,
			'rowEnd'   => $row + 1,
		);
	}
}
