import type {
	ApiDeclaredItem,
	ApiItem,
	ApiItemContainerMixin,
	ApiProperty,
	ApiPropertySignature,
} from '@discordjs/api-extractor-model';
import { ApiItemKind } from '@discordjs/api-extractor-model';
import { Fragment, useMemo } from 'react';
import { resolveMembers } from '~/util/members';
import { Property } from './Property';

export function isPropertyLike(item: ApiItem): item is ApiProperty | ApiPropertySignature {
	return item.kind === ApiItemKind.Property || item.kind === ApiItemKind.PropertySignature;
}

export function PropertyList({ item }: { readonly item: ApiItemContainerMixin }) {
	const members = resolveMembers(item, isPropertyLike);

	const propertyItems = useMemo(
		() =>
			members.map((prop, idx) => {
				return (
					<Fragment key={`${prop.item.displayName}-${idx}`}>
						<Property
							inheritedFrom={prop.inherited as ApiDeclaredItem & ApiItemContainerMixin}
							item={prop.item as ApiProperty}
						/>
						<div className="border-t-2 border-light-900 dark:border-dark-100" />
					</Fragment>
				);
			}),
		[members],
	);

	return <div className="flex flex-col gap-4">{propertyItems}</div>;
}
