import React from "react";
import { connect } from "react-redux";

import { IFolderTree, IRootState } from "@ax/types";
import { Icon } from "@ax/components";

import MenuList from "./MenuList";
import * as S from "./style";

const FolderTree = (props: IProps) => {
	const { folders, folderID, title, hidden, hideRoot, trimNames, createAction, onClick } = props;

	const NewFolderButton = () =>
		createAction ? (
			<S.NewIconWrapper onClick={createAction}>
				<Icon name="newFolder" size="16" />
			</S.NewIconWrapper>
		) : (
			<></>
		);

	const handleRootClick = () => onClick(0);

	return (
		<>
			{!hideRoot && (
				<S.Item selected={!folderID} onClick={handleRootClick}>
					<S.RootName>All Documents</S.RootName>
					<NewFolderButton />
				</S.Item>
			)}
			{title && <S.Title>{title}</S.Title>}
			<S.TreeWrapper>
				<MenuList
					list={folders}
					hidden={hidden}
					trimNames={trimNames}
					createAction={createAction}
					folderID={folderID}
					onClick={onClick}
				/>
			</S.TreeWrapper>
		</>
	);
};

interface IProps {
	folders: IFolderTree[];
	folderID: number;
	title?: string;
	hidden?: number[];
	hideRoot?: boolean;
	trimNames?: boolean;
	createAction?(): void;
	onClick(folderID: number): void;
}

const mapStateToProps = (state: IRootState) => ({
	folders: state.fileDrive.foldersTree,
});

export default connect(mapStateToProps)(FolderTree);
