import { BulkSelectionOptions } from "@ax/components";
import { itemLabel } from "@ax/constants";
import type { IBulkSelectedItems } from "@ax/hooks";
import { usePermission } from "@ax/hooks";
import type { IQueryValue, IRole } from "@ax/types";

import TableHeader from "./TableHeader";

const BulkHeader = (props: IProps): JSX.Element => {
	const {
		showBulk,
		checkState,
		bulkDelete,
		selectItems,
		selectAllItems,
		totalItems,
		isScrolling,
		sortItems,
		filterItems,
		sortedListStatus,
		filterValues,
		isSiteView = false,
		roles,
		setHoverCheck,
		selectedItems,
		isLoading,
	} = props;

	const isAllowedToRemoveUsers = usePermission("usersRoles.removeUsers");

	const bulkActions = isAllowedToRemoveUsers
		? [
				{
					icon: "delete",
					text: isSiteView ? "Remove from this site" : "delete",
					action: bulkDelete,
				},
			]
		: [];

	return showBulk ? (
		<BulkSelectionOptions
			checkState={checkState}
			actions={bulkActions}
			selectItems={selectItems}
			totalItems={totalItems}
			selectedItems={selectedItems}
			isLoading={isLoading}
			itemLabel={itemLabel.USER}
		/>
	) : (
		<TableHeader
			filterValues={filterValues}
			totalItems={totalItems}
			selectAllItems={selectAllItems}
			filterItems={filterItems}
			isScrolling={isScrolling}
			sortItems={sortItems}
			sortedListStatus={sortedListStatus}
			isSiteView={isSiteView}
			roles={roles}
			setHoverCheck={setHoverCheck}
			checkState={checkState}
		/>
	);
};

interface IProps {
	showBulk: boolean;
	isSiteView?: boolean;
	checkState: Record<string, boolean>;
	bulkDelete: () => void;
	selectItems: () => void;
	selectAllItems: () => void;
	totalItems: number;
	isScrolling: boolean;
	sortItems: (orderPointer: IQueryValue[], isAscending: boolean) => void;
	sortedListStatus: { isAscending: boolean; sortedByName: boolean; sortedByDateCreated: boolean };
	filterValues: Record<string, IQueryValue[]>;
	filterItems: (filterPointer: string, filtersSelected: IQueryValue[]) => void;
	roles: IRole[];
	setHoverCheck: (state: boolean) => void;
	selectedItems: IBulkSelectedItems;
	isLoading: boolean;
}

export default BulkHeader;
