import type { Form } from './type-helpers/form';
import type { ExtensionPropsBase, ConfigValue } from './base';
import type { Model } from './type-helpers/model';
import type { AppInstallation } from './type-helpers/appInstallation';
import type { Field } from './type-helpers/field';

export const FieldExtensionType = {
  STRING: 'STRING',
  RICHTEXT: 'RICHTEXT',
  INT: 'INT',
  FLOAT: 'FLOAT',
  BOOLEAN: 'BOOLEAN',
  JSON: 'JSON',
  DATETIME: 'DATETIME',
  DATE: 'DATE',
  LOCATION: 'LOCATION',
  COLOR: 'COLOR',
  ENUMERATION: 'ENUMERATION',
  RELATION: 'RELATION',
  ASSET: 'ASSET',
  UNION: 'UNION',
} as const;

export const FieldExtensionFeature = {
  FieldRenderer: 'FieldRenderer',
  ListRenderer: 'ListRenderer',
  TableRenderer: 'TableRenderer',
} as const;

export interface FieldExtensionProps extends ExtensionPropsBase {
  extension: {
    config: ConfigValue;
    tableConfig: ConfigValue;
    fieldConfig: ConfigValue;
    id: string;
  };

  isExpanded: boolean;
  expand: (expand: boolean | ((isExpanded: boolean) => boolean)) => unknown;

  // name of the field in the form (may differ from the field apiId, ie. for localized fields)
  name: string;

  // current locale on localized field
  locale: string | undefined;

  // current entry id, null for new entries;
  entryId: string | null;

  isTableCell: boolean;

  isReadOnly: boolean | undefined;

  value: any;

  onBlur: (event?: FocusEvent) => Promise<void>;

  onChange: (event: InputEvent | any) => Promise<void>;

  onFocus: (event?: FocusEvent) => Promise<void>;

  // @see https://final-form.org/docs/react-final-form/types/FieldRenderProps
  meta: {
    active: boolean;
    error: any;
    touched: boolean;
  };

  form: Form;
  field: Field;
  model: Model;
  installation: AppInstallation;
}
