---
import type { DatetimeField } from '../../resource-types';

interface Props {
  field: DatetimeField;
  value: unknown;
}

const { field, value } = Astro.props;

// Coerce ISO datetime (which may be "YYYY-MM-DD HH:MM:SS" SQLite-style or
// "YYYY-MM-DDTHH:mm:ss[Z|+offset]") to the "YYYY-MM-DDTHH:mm" the input wants.
function toDatetimeLocal(v: unknown): string {
  if (v == null || v === '') return '';
  const s = String(v).replace(' ', 'T');
  const m = s.match(/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2})/);
  return m ? m[1] : '';
}
const v = toDatetimeLocal(value);
---

<input
  type="datetime-local"
  id={`f-${field.key}`}
  name={field.key}
  class="bs-input"
  value={v}
  required={field.required}
  readonly={field.readOnly}
/>
