'use client';

import type { TreeNode, TreeRowSlot } from '../Tree/types';
import { FileIcon, type FileIconSize } from './FileIcon';

export interface CreateFileIconSlotOptions<T> {
  /**
   * How to read the displayed name (with extension) for a node. Use the same
   * accessor you pass to `<TreeRoot getItemName={...} />`.
   */
  getName: (node: TreeNode<T>) => string;
  /** Pixel size for both file and folder icons. Default: 16. */
  size?: FileIconSize;
}

/**
 * Build a `renderIcon` slot for `<TreeRoot>` that uses `<FileIcon>` for both
 * leaves and folders.
 *
 * @example
 * ```tsx
 * <TreeRoot
 *   data={data}
 *   getItemName={(n) => n.data.name}
 *   renderIcon={createFileIconSlot({ getName: (n) => n.data.name })}
 * />
 * ```
 */
export function createFileIconSlot<T>({
  getName,
  size = 16,
}: CreateFileIconSlotOptions<T>): TreeRowSlot<T> {
  return ({ node, isFolder, isExpanded }) => (
    <FileIcon
      name={getName(node)}
      isFolder={isFolder}
      isExpanded={isExpanded}
      size={size}
    />
  );
}
