<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

require_once dirname( __DIR__, 3 ) . '/includes/table-block-render-helpers.php';

$axcelersblocks_block_name = isset( $block->name ) ? str_replace( '/', '-', $block->name ) . '-block' : 'axcelersblocks-table-cell-block';

// Merged-cell spans → colspan / rowspan attributes (only when > 1).
$axcelersblocks_cell_extra = array();
$axcelersblocks_colspan    = isset( $attributes['colSpan'] ) ? absint( $attributes['colSpan'] ) : 1;
$axcelersblocks_rowspan    = isset( $attributes['rowSpan'] ) ? absint( $attributes['rowSpan'] ) : 1;
if ( $axcelersblocks_colspan > 1 ) {
	$axcelersblocks_cell_extra['colspan'] = $axcelersblocks_colspan;
}
if ( $axcelersblocks_rowspan > 1 ) {
	$axcelersblocks_cell_extra['rowspan'] = $axcelersblocks_rowspan;
}

// `scope` is only meaningful on header cells.
$axcelersblocks_tag = isset( $attributes['tagName'] ) ? $attributes['tagName'] : 'td';
if ( 'th' === $axcelersblocks_tag && ! empty( $attributes['scope'] ) ) {
	$axcelersblocks_cell_extra['scope'] = sanitize_html_class( $attributes['scope'] );
}

echo wp_kses_post(
	axcelersblocks_table_render_element(
		$attributes,
		$content,
		$axcelersblocks_block_name,
		array(
			'allowed_tags'  => array( 'th', 'td' ),
			'default_tag'   => 'td',
			'base_classes'  => array(
				'axcelersblocks-table-cell',
				! empty( $attributes['tagName'] ) ? 'axcelersblocks-table-cell--' . sanitize_html_class( $attributes['tagName'] ) : '',
			),
			'extra_attrs'   => $axcelersblocks_cell_extra,
			'fallback_slug' => 'axb-table-cell',
		)
	)
);
