<script lang="ts" module>
  import type { CellContext, HeaderContext, RowData } from '@tanstack/table-core';
  import { type Snippet } from 'svelte';

  export type FlexRenderProps<TData extends RowData, TValue> = {
    ctx: CellContext<TData, TValue> | HeaderContext<TData, TValue>;
    render: ((ctx: CellContext<TData, TValue> | HeaderContext<TData, TValue>) => unknown) | undefined;
    snippet?: Snippet<[{ props: Record<string, unknown> }]>;
  };
</script>

<script lang="ts">
  let { ctx, render, snippet }: import('./flex-render.svelte').FlexRenderProps<any, any> = $props();

  const cell = $derived(
    typeof render === 'function' ? (render as any)(ctx) : render
  );
</script>

{#if snippet}
  {@render snippet({ props: ctx as any })}
{:else if typeof cell === 'string' || typeof cell === 'number' || typeof cell === 'boolean'}
  {cell}
{:else if cell}
  {@html (cell as { html?: string }).html ?? cell}
{/if}