// The whole machine wiring in one tag: creates the service, connects it, and
// returns the api GETTER. This is the tag most components want.
//
//   <zag/api=() => switchMachine from=input/>
//   <label ...api().getRootProps()>
//
// It is exactly <zag-machine> plus the connect line, so everything documented
// there applies here: the value shorthand is the MODULE getter, `from=` picks
// the machine's props out of a component's input (generating an `id`, adding
// two-way `xChange` sugar to `onXChange` callbacks), `props=` is the
// serialization-safe place to build values with methods, and any other
// attribute written on the tag overrides what `from=` supplied:
//
//   <zag/api=() => tooltip from=input closeOnEscape=false
//     onOpenChange(details) { input.onOpenChange?.(details); track(details.open) }/>
//
// Reach for <zag-machine> instead only when the component needs the service
// itself — to thread into a child, or to connect a second module against.
//
// `normalize=` swaps the prop normalizer, defaulting to marko-zag's
// `normalizeProps`. It is a NormalizeProps OBJECT (what `createNormalizer`
// returns), passed straight to the module's `connect` — not a closure, and
// not a plain function. Build a wrapper with `createNormalizer`, never with
// object spread: `normalizeProps` is a Proxy with no own enumerable keys, so
// `{ ...normalizeProps }` silently yields `{}`.
import { normalizeProps } from "../normalize-props.ts";
import type { NormalizeProps } from "@zag-js/types";
import type { ZagModule, ZagMachineInput, ZagApi } from "../zag-module.ts";
import type { PropTypes } from "../prop-types.ts";

export interface Input<M extends ZagModule> extends ZagMachineInput<M> {
  /** Prop normalizer; defaults to marko-zag's `normalizeProps`. */
  normalize?: NormalizeProps<PropTypes>;
}

<const/mod=input.value/>
<zag-machine/service ...input/>
// The arrow MUST be written directly as the <const> value so the compiler
// wraps it in `_resume(...)` — an unregistered closure dies with `Unable to
// serialize` the moment a consumer spreads `api()` onto an element. The
// service getter's identity is the only dependency: it is a fresh closure on
// every machine notify and on every tracked props change, which is what makes
// this <const> recompute for both.
<const/api=() => mod().connect(service(), input.normalize ?? normalizeProps) as ZagApi<M>/>
<return=api/>
