import React from "react";

import { Icon, FloatingMenu, ListTitle, ListItem } from "@ax/components";
import { IQueryValue } from "@ax/types";

import * as S from "./style";

const StatusFilter = (props: IStatusFilterProps) => {
	const { sortItems, sortedState, isStructuredData } = props;
	const { isAscending, sortedByDate } = sortedState;
	const value = isStructuredData ? "date" : "modified";
	const sortByModifiedDate = (isAscending: boolean) => sortItems([{ value, label: value }], isAscending);

	const sortOldestModifiedDate = () => sortByModifiedDate(true);
	const sortMostRecentModifiedDate = () => sortByModifiedDate(false);

	const SortedStateArrow = () =>
		isAscending ? <Icon name="FullArrowUp" size="16" /> : <Icon name="FullArrowDown" size="16" />;

	const Header = () => (
		<S.Status isActive={sortedByDate} data-testid="status-filter-header">
			Last update
			<S.IconsWrapper>
				{sortedByDate ? (
					<SortedStateArrow />
				) : (
					<S.InteractiveArrow data-testid="status-filter-interactive-arrow">
						<Icon name="DownArrow" size="16" />
					</S.InteractiveArrow>
				)}
			</S.IconsWrapper>
		</S.Status>
	);

	return (
		<FloatingMenu Button={Header} position="left">
			<ListTitle>Sort by modified date</ListTitle>
			<ListItem isSelected={sortedByDate && !isAscending} onClick={sortMostRecentModifiedDate}>
				Most recent
			</ListItem>
			<ListItem isSelected={sortedByDate && isAscending} onClick={sortOldestModifiedDate}>
				Oldest
			</ListItem>
		</FloatingMenu>
	);
};

export interface IStatusFilterProps {
	sortedState: any;
	sortItems(orderPointer: IQueryValue[], isAscendent: boolean): any;
	isStructuredData?: boolean;
}

export default StatusFilter;
