#import "UITableViewAdditions.h"

NS_ASSUME_NONNULL_BEGIN

@implementation UITableView (UITableViewColloquyAdditions)
- (void) updateCellAtIndexPath:(NSIndexPath *) indexPath withAnimation:(UITableViewRowAnimation) animation {
	NSParameterAssert(indexPath != nil);

	NSArray <NSIndexPath *> *indexPaths = @[indexPath];

	NSIndexPath *selectedIndexPath = [self indexPathForSelectedRow];
	BOOL selected = (selectedIndexPath && indexPath.section == selectedIndexPath.section && indexPath.row == selectedIndexPath.row);
	CGPoint contentOffset = self.contentOffset;

	if (selected)
		[self deselectRowAtIndexPath:indexPath animated:NO];

	[self reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];

	[self setContentOffset:contentOffset animated:NO];

	if (selected)
		[self selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

- (void) performAction:(SEL) action forCell:(UITableViewCell *) cell sender:(__nullable id) sender {
	id delegate = self.delegate;
	if (![delegate respondsToSelector:@selector(tableView:performAction:forRowAtIndexPath:withSender:)])
		return;

	NSIndexPath *indexPath = [self indexPathForCell:cell];
	if (!indexPath)
		return;

	[delegate tableView:self performAction:action forRowAtIndexPath:indexPath withSender:sender];
}

- (NSUInteger) numberOfRows {
	NSUInteger numberOfRows = 0;

	for (NSInteger section = 0; section < [self numberOfSections]; section++)
		numberOfRows += [self numberOfRowsInSection:section];

	return numberOfRows;
}

- (void) hideEmptyCells {
	if (self.style == UITableViewStyleGrouped)
		return;

	if (!self.tableFooterView) {
		UIView *view = [[UIView alloc] initWithFrame:CGRectZero];

		self.tableFooterView = view;
	}
}
@end

NS_ASSUME_NONNULL_END
