---
/* ---------------------------------------------------------------------------
 * FieldRenderer — dispatches on field.kind to the right input component
 * and wraps it with label + helper + error.
 *
 * Branches must stay exhaustive; the `never` fallback flags any unhandled
 * Field kind at compile time.
 * ------------------------------------------------------------------------- */

import TextField from './fields/TextField.astro';
import TextareaField from './fields/TextareaField.astro';
import MarkdownField from './fields/MarkdownField.astro';
import SelectField from './fields/SelectField.astro';
import SelectAsyncField from './fields/SelectAsyncField.astro';
import MultiSelectAsyncField from './fields/MultiSelectAsyncField.astro';
import MultiTextField from './fields/MultiTextField.astro';
import DateField from './fields/DateField.astro';
import DatetimeField from './fields/DatetimeField.astro';
import NumberField from './fields/NumberField.astro';
import ReadonlyField from './fields/ReadonlyField.astro';
import type { Field } from '../resource-types';

interface Props {
  field: Field;
  value: unknown;
  error?: string;
  item: Record<string, unknown> | null;
}

const { field, value, error, item } = Astro.props;
---

<div class="bs-field" data-field={field.key}>
  <label class="bs-label" for={`f-${field.key}`}>
    {field.label}
    {field.required && <span class="bs-required" aria-hidden="true">*</span>}
  </label>

  {field.kind === 'text' && <TextField field={field} value={value} />}
  {field.kind === 'textarea' && <TextareaField field={field} value={value} />}
  {field.kind === 'markdown' && <MarkdownField field={field} value={value} />}
  {field.kind === 'select' && <SelectField field={field} value={value} />}
  {field.kind === 'select-async' && <SelectAsyncField field={field} value={value} />}
  {field.kind === 'multi-select-async' && <MultiSelectAsyncField field={field} value={value} />}
  {field.kind === 'multi-text' && <MultiTextField field={field} value={value} />}
  {field.kind === 'date' && <DateField field={field} value={value} />}
  {field.kind === 'datetime' && <DatetimeField field={field} value={value} />}
  {field.kind === 'number' && <NumberField field={field} value={value} />}
  {field.kind === 'readonly' && <ReadonlyField field={field} value={value} item={item} />}

  {field.helperText && (
    <p class="bs-helper">{field.helperText}</p>
  )}
  {error && (
    <p class="bs-field-error" role="alert">{error}</p>
  )}
</div>
