import {
  type ForwardedRef,
  type ReactNode,
  forwardRef,
  type CSSProperties,
} from 'react';
import type { Spacing } from '../../../types';
import { List, type ListProps } from '../../atoms';
import styles from './grid.module.scss';

export type GridProps<T extends boolean> = Omit<
  ListProps<T, false>,
  'children' | 'hideMarker' | 'isHierarchical' | 'isInline' | 'spacing'
> & {
  /**
   * How the items should be aligned?
   *
   * @default undefined // The default behavior is `stretch`.
   */
  alignItems?: 'center' | 'end' | 'start';
  /**
   * The grid items.
   */
  children: ReactNode;
  /**
   * Control the number of column.
   *
   * @default 'auto-fit'
   */
  col?: number | 'auto-fill' | 'auto-fit';
  /**
   * The gap between the items.
   *
   * @default null
   */
  gap?: Spacing | null;
  /**
   * Should the grid be centered?
   *
   * @default false
   */
  isCentered?: boolean;
  /**
   * Define a fixed size for each item.
   *
   * You should either use `size` or `sizeMax`/`sizeMin` not both.
   *
   * @default undefined
   */
  size?: string;
  /**
   * Define the maximal size of each item.
   *
   * You should either use `size` or `sizeMax`/`sizeMin` not both.
   *
   * @default '1fr'
   */
  sizeMax?: string;
  /**
   * Define the maximal size of each item.
   *
   * You should either use `size` or `sizeMax`/`sizeMin` not both.
   *
   * @default 0
   */
  sizeMin?: 0 | string;
};

const GridWithRef = <T extends boolean>(
  {
    alignItems,
    children,
    className = '',
    col = 'auto-fit',
    gap,
    isCentered = false,
    size,
    sizeMax,
    sizeMin,
    style,
    ...props
  }: GridProps<T>,
  ref?: ForwardedRef<T extends true ? HTMLOListElement : HTMLUListElement>
) => {
  const gridClass = [
    styles.wrapper,
    styles[alignItems ? `wrapper--align-items-${alignItems}` : ''],
    styles[isCentered ? 'wrapper--is-centered' : ''],
    styles[size ? 'wrapper--has-fixed-size' : ''],
    styles[sizeMin ? 'wrapper--has-min-size' : ''],
    className,
  ].join(' ');
  const gridStyles = {
    ...style,
    '--col': col,
    ...(size ? { '--size': size } : {}),
    ...(sizeMax ? { '--size-max': sizeMax } : {}),
    ...(sizeMin ? { '--size-min': sizeMin } : {}),
    ...(gap ? { '--gap': `var(--spacing-${gap})` } : {}),
  } as CSSProperties;

  return (
    <List
      {...props}
      className={gridClass}
      hideMarker
      ref={ref}
      style={gridStyles}
    >
      {children}
    </List>
  );
};

export const Grid = forwardRef(GridWithRef);

<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/www.armandphilippot.com/tree/?h=v1.0.0&amp;id=163f9dc0fe436b708de4e59999e87005c6685a0f'>root</a>/<a href='/www.armandphilippot.com/tree/src?h=v1.0.0&amp;id=163f9dc0fe436b708de4e59999e87005c6685a0f'>src</a>/<a href='/www.armandphilippot.com/tree/src/components?h=v1.0.0&amp;id=163f9dc0fe436b708de4e59999e87005c6685a0f'>components</a>/<a href='/www.armandphilippot.com/tree/src/components/Copyright?h=v1.0.0&amp;id=163f9dc0fe436b708de4e59999e87005c6685a0f'>Copyright</a>/<a href='/www.armandphilippot.com/tree/src/components/Copyright/Copyright.tsx?h=v1.0.0&amp;id=163f9dc0fe436b708de4e59999e87005c6685a0f'>Copyright.tsx</a></div><div class='content'>blob: d2de2e99fa1091d0107ceb8ff2bf5c5db3ac5db7 (<a href='/www.armandphilippot.com/plain/src/components/Copyright/Copyright.tsx?h=v1.0.0&amp;id=163f9dc0fe436b708de4e59999e87005c6685a0f'>plain</a>)
<table summary='blob content' class='blob'>
<tr><td class='linenumbers'><pre><a id='n1' href='#n1'>1</a>
<a id='n2' href='#n2'>2</a>
<a id='n3' href='#n3'>3</a>
<a id='n4' href='#n4'>4</a>
<a id='n5' href='#n5'>5</a>
<a id='n6' href='#n6'>6</a>
<a id='n7' href='#n7'>7</a>
<a id='n8' href='#n8'>8</a>
<a id='n9' href='#n9'>9</a>
<a id='n10' href='#n10'>10</a>
<a id='n11' href='#n11'>11</a>
<a id='n12' href='#n12'>12</a>
<a id='n13' href='#n13'>13</a>
<a id='n14' href='#n14'>14</a>
<a id='n15' href='#n15'>15</a>
<a id='n16' href='#n16'>16</a>
<a id='n17' href='#n17'>17</a>
</pre></td>
<td class='lines'><pre><code>