<?php
/**
 * Move responsive group gap setting into spacing blockGap.
 *
 * @package The7
 */

namespace The7\Mods\Theme_Update\Migrations\v14_3_4\Post_Migrations;

use The7\Mods\Theme_Update\Base\Block_Editor\Wp_Block_Migration_Base;

defined( 'ABSPATH' ) || exit;

final class Group_Gap extends Wp_Block_Migration_Base {

	protected const BLOCK_NAMES     = [ 'core/group' ];
	protected const ATTR_GROUP_NAME = 'wpbbeResponsive';

	public function need_to_process_block( array $block ): bool {
		if ( ! parent::need_to_process_block( $block ) ) {
			return false;
		}

		if ( ! is_array( $block['attrs']['wpbbeResponsive']['settings'] ?? null ) ) {
			return false;
		}

		return array_key_exists( 'gap', $block['attrs']['wpbbeResponsive']['settings'] );
	}

	public function process_block( array $block ): array {
		$gap = $block['attrs']['wpbbeResponsive']['settings']['gap'];

		if ( ! is_array( $block['attrs']['wpbbeResponsive']['settings']['spacing'] ?? null ) ) {
			$block['attrs']['wpbbeResponsive']['settings']['spacing'] = [];
		}

		$block['attrs']['wpbbeResponsive']['settings']['spacing']['blockGap'] = $gap;
		unset( $block['attrs']['wpbbeResponsive']['settings']['gap'] );

		return $block;
	}
}
