import type { HTMLElementName, HTMLFieldElement } from '../types/dom';
import { map } from 'rxjs';
import { inputPipe } from '../utils/input-pipe';
import { autoValue } from '../utils/auto-value';
/**
* An Event Operator emitting a typed FormData object from the underlying <form> element instead of a regular DOM Event object
* Field types are taken from their respective data-type attribute or string if unset
* @returns OperatorFunction
* @example
**/
export const autoForm = () =>
map((e: Event) => Object.fromEntries(
[...(e.currentTarget).elements]
// FIXME: doesn't emit unchecked radio buttons. Should emit null, instead?
.map((e: Element) => [(e as HTMLFieldElement).name ?? e.id, autoValue(e as HTMLFieldElement)])
.filter(([k, v]) => k!=='' && v!==undefined)
))
;
/**
* An Event Adapter emitting a FormData object from the underlying form element instead of a regular DOM Event object
* @returns EventSource
* @example
* @example
**/
export const AutoForm = inputPipe>(
autoForm(),
);
export const asTypedFormData = autoForm;
export const AsTypedFormData = AutoForm;