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

import type { ISortedListStatus } from "../../utils";
import TableHeader from "./TableHeader";

import * as S from "./style";

const BulkHeader = (props: IBulkHeaderProps): JSX.Element => {
	const {
		showBulk,
		checkState,
		bulkPublish,
		bulkUnpublish,
		selectItems,
		selectAllItems,
		totalItems,
		sortItems,
		filterItems,
		sortedListStatus,
		filterValues,
		setHoverCheck,
		selectedItems,
		isLoading,
	} = props;

	const allowedToPublishSite = usePermission("general.publishSite");
	const allowedToUnpublishSite = usePermission("general.unpublishSite");

	const publishAction = allowedToPublishSite
		? {
				icon: "upload-pending",
				text: "publish",
				action: bulkPublish,
			}
		: undefined;

	const unpublishAction = allowedToUnpublishSite
		? {
				icon: "offline",
				text: "unpublish",
				action: bulkUnpublish,
			}
		: undefined;

	const actions = [publishAction, unpublishAction];

	return showBulk ? (
		<S.BulkWrapper data-testid="sites-list-bulk-wrapper">
			<BulkSelectionOptions
				isScrolling={false}
				checkState={checkState}
				actions={actions}
				selectItems={selectItems}
				totalItems={totalItems}
				selectedItems={selectedItems}
				isLoading={isLoading}
				itemLabel={itemLabel.SITE}
			/>
		</S.BulkWrapper>
	) : (
		<TableHeader
			isScrolling={false}
			selectAllItems={selectAllItems}
			checkState={checkState}
			sortItems={sortItems}
			filterItems={filterItems}
			sortedListStatus={sortedListStatus}
			totalItems={totalItems}
			filterValues={filterValues}
			setHoverCheck={setHoverCheck}
		/>
	);
};

export interface IBulkHeaderProps {
	showBulk: boolean;
	checkState: Record<string, boolean>;
	bulkPublish: () => void;
	bulkUnpublish: () => void;
	selectItems: () => void;
	selectAllItems: () => void;
	totalItems: number;
	sortItems: (orderPointer: IQueryValue[], isAscending: boolean) => void;
	filterItems: (filterPointer: string, filtersSelected: IQueryValue[]) => void;
	sortedListStatus: ISortedListStatus;
	filterValues: Record<string, IQueryValue[]>;
	setHoverCheck: (state: boolean) => void;
	selectedItems: IBulkSelectedItems;
	isLoading: boolean;
}

export default BulkHeader;
