import { BulkSelectionOptions } from "@ax/components";
import { getContentItemLabel } from "@ax/helpers/content";
import type { IBulkSelectedItems } from "@ax/hooks";
import { usePermission } from "@ax/hooks";
import type { IColumn, IQueryValue } from "@ax/types";

import TableHeader from "./TableHeader";

const BulkHeader = (props: IProps): JSX.Element => {
	const {
		showBulk,
		checkState,
		bulkDelete,
		bulkPublish,
		bulkUnpublish,
		selectItems,
		selectAllItems,
		totalItems,
		isScrolling,
		isStructuredData,
		sortItems,
		filterItems,
		sortedListStatus,
		isEditable,
		filterValues,
		categoryColumns,
		columns,
		setColumns,
		isGlobalPages,
		siteID,
		maxColumns,
		exportAction,
		setHoverCheck,
		selectedItems,
		isLoading,
		checkFromPage,
	} = props;

	const isAllowedToDeletePage = usePermission("content.deletePages");
	const isAllowedToPublishPages = usePermission("content.publishUnpublishPages");

	const deleteAction = {
		icon: "delete",
		text: "delete",
		action: bulkDelete,
	};

	const actions = isAllowedToPublishPages
		? [
				{
					icon: "upload-pending",
					text: "publish",
					action: bulkPublish,
				},
				{
					icon: "offline",
					text: "unpublish",
					action: bulkUnpublish,
				},
			]
		: [];

	const bulkActions = isEditable && isAllowedToDeletePage ? [deleteAction, ...actions] : actions;

	return showBulk ? (
		<BulkSelectionOptions
			isScrolling={isScrolling}
			checkState={checkState}
			actions={bulkActions}
			selectItems={selectItems}
			totalItems={totalItems}
			exportAction={exportAction}
			selectedItems={selectedItems}
			isLoading={isLoading}
			itemLabel={getContentItemLabel({
				isPageContentType: checkFromPage,
				isSimpleContentType: isStructuredData,
				isStandardPage: !checkFromPage && !isStructuredData,
			})}
		/>
	) : (
		<TableHeader
			isScrolling={isScrolling}
			isStructuredData={isStructuredData}
			selectAllItems={selectAllItems}
			checkState={checkState}
			sortItems={sortItems}
			filterItems={filterItems}
			sortedListStatus={sortedListStatus}
			totalItems={totalItems}
			filterValues={filterValues}
			categoryColumns={categoryColumns}
			columns={columns}
			setColumns={setColumns}
			isGlobalPages={isGlobalPages}
			siteID={siteID}
			maxColumns={maxColumns}
			setHoverCheck={setHoverCheck}
		/>
	);
};

interface IProps {
	showBulk: boolean;
	checkState: any;
	bulkDelete: () => void;
	bulkPublish: () => void;
	bulkUnpublish: () => void;
	selectItems: () => void;
	selectAllItems: () => void;
	totalItems: number;
	isScrolling: boolean;
	isStructuredData: boolean;
	sortItems: (orderPointer: IQueryValue[], isAscending: boolean) => void;
	filterItems: (filterPointer: string, filtersSelected: IQueryValue[]) => void;
	sortedListStatus: any;
	isEditable?: boolean | null;
	filterValues: any;
	categoryColumns: any[];
	columns: IColumn[];
	setColumns: (columns: IColumn[]) => void;
	maxColumns: { value: number; text: string };
	isGlobalPages: boolean;
	siteID: number;
	exportAction?(formats: (number | string)[]): void;
	setHoverCheck: (state: boolean) => void;
	selectedItems: IBulkSelectedItems;
	isLoading: boolean;
	checkFromPage: boolean;
}

export default BulkHeader;
