import type { StatementLocale } from "@accessibit/types";
import type { LocalizedDraft, StatementForm } from "./statementForm";

/**
 * What a section of the statement form is handed: the form, the ways to
 * write into it, and what the page knows about its gaps. The page holds the
 * state; a section only draws its fields.
 */
export interface StatementFormController {
  form: StatementForm;
  patch: <K extends keyof StatementForm>(
    key: K,
    value: StatementForm[K],
  ) => void;
  /** Writes one free-text field under one language, leaving the others alone. */
  patchLocalized: (
    locale: StatementLocale,
    field: keyof LocalizedDraft,
    value: string,
  ) => void;
  /** Integers ≥ 0 only, and "" stays "": blank means "not declared". */
  patchCount: (
    key: "employeesWithDisabilities" | "workstationsForDisabilities",
    value: string,
  ) => void;
  /** The error state of one field, to spread on its `StatementField`. */
  fieldError: (key: keyof StatementForm) => {
    invalid: boolean;
    errorMessage?: string;
  };
  /**
   * Whether the gaps are being shown: after a save that could not generate,
   * and on a statement saved incomplete and never generated.
   */
  showErrors: boolean;
  /** The required fields still blank, by form key. */
  missing: ReadonlySet<string>;
  /** A reader's form: every control is switched off. */
  readOnly: boolean;
  /** Today as `YYYY-MM-DD`, the cap of the two past-only dates. */
  today: string;
  /**
   * The page's document language: the one the free text is edited under and
   * the preview shows. One state for the whole page.
   */
  locale: StatementLocale;
  onLocaleChange: (locale: StatementLocale) => void;
}
