import { __ } from '@wordpress/i18n';
import { BlockControls } from '@wordpress/block-editor';
import { ToolbarGroup, ToolbarDropdownMenu } from '@wordpress/components';
import {
	table,
	tableRowBefore,
	tableRowAfter,
	tableRowDelete,
	tableColumnBefore,
	tableColumnAfter,
	tableColumnDelete,
} from '@wordpress/icons';
import { useTableCellActions } from '../../table/tableActions';

/**
 * Block toolbar for a table cell: add/remove rows & columns and merge/split
 * cells. All operations work on the block tree (no numeric row/column sync).
 */
export default function TableCellToolbar( { clientId } ) {
	const {
		insertRow, deleteRow, insertColumn, deleteColumn, mergeRight, mergeDown, splitCell,
	} = useTableCellActions( clientId );

	const controls = [
		[
			{ icon: tableRowBefore,    title: __( 'Insert row above', 'axcelersblocks' ),  onClick: () => insertRow( 0 ) },
			{ icon: tableRowAfter,     title: __( 'Insert row below', 'axcelersblocks' ),  onClick: () => insertRow( 1 ) },
			{ icon: tableRowDelete,    title: __( 'Delete row', 'axcelersblocks' ),        onClick: () => deleteRow() },
		],
		[
			{ icon: tableColumnBefore, title: __( 'Insert column left', 'axcelersblocks' ),  onClick: () => insertColumn( 0 ) },
			{ icon: tableColumnAfter,  title: __( 'Insert column right', 'axcelersblocks' ), onClick: () => insertColumn( 1 ) },
			{ icon: tableColumnDelete, title: __( 'Delete column', 'axcelersblocks' ),       onClick: () => deleteColumn() },
		],
		[
			{ icon: tableColumnAfter,  title: __( 'Merge with cell on right', 'axcelersblocks' ), onClick: () => mergeRight() },
			{ icon: tableRowAfter,     title: __( 'Merge with cell below', 'axcelersblocks' ),    onClick: () => mergeDown() },
			{ icon: table,             title: __( 'Split merged cell', 'axcelersblocks' ),        onClick: () => splitCell() },
		],
	];

	return (
		<BlockControls group="block">
			<ToolbarGroup>
				<ToolbarDropdownMenu
					icon={ table }
					label={ __( 'Edit table', 'axcelersblocks' ) }
					controls={ controls }
				/>
			</ToolbarGroup>
		</BlockControls>
	);
}
