import { EndpointHealth, CONNECTIVITY_REASONS } from "../registry/interfaces/EndpointHealth.js"; import { SchemaError } from "../registry/schema.js"; import type { Infer } from "../registry/schema.js"; import type { InterfaceDescriptor, Properties } from "../registry/types.js"; import { property } from "./property.js"; import type { PropertyOptions } from "./property.js"; import type { Property } from "./types.js"; /** Where a property goes in a ChangeReport: with the ones that changed, or with the rest in the context. */ export type Target = "context" | "change"; /** A capability of a device: its properties are reported under its instance. */ interface DeclaredCapability

{ readonly descriptor: InterfaceDescriptor; readonly instance: string; } export interface StateBuilderOptions { /** Where the properties go until changed() or unchanged() says otherwise. Default: "context". */ target?: Target; /** The time of a property that is given none. Default: the clock. */ now?: () => Date; } /** * Collects the properties of one message. For a Response or a StateReport they are its context. For a ChangeReport * changed() and unchanged() say which of the two lists the properties that follow belong to. */ export class StateBuilder { private readonly lists: Record = { context: [], change: [] }; private target: Target; private readonly now: () => Date; constructor(options: StateBuilderOptions = {}) { this.target = options.target ?? "context"; this.now = options.now ?? (() => new Date()); } /** * A property of an interface the library describes, its value checked: set(PowerController, "powerState", "ON"). * Given a capability of a device in place of the interface, the property carries the instance of the capability. * Throws SchemaError for a value Alexa would not take. */ set

( source: InterfaceDescriptor | DeclaredCapability

, name: K, value: Infer, options: PropertyOptions = {} ): this { const descriptor = "descriptor" in source ? source.descriptor : source; const instance = options.instance ?? ("descriptor" in source ? source.instance : ""); const where = `${descriptor.namespace}${instance ? ` "${instance}"` : ""}: ${name}`; const described = descriptor.properties[name]; if (!described) { const names = Object.keys(descriptor.properties); throw new SchemaError(where, names.length > 0 ? `not a property of the interface, which has ${names.join(", ")}` : "the library knows no property of the interface, report it with setRaw()"); } if (descriptor.instanced && !instance) { throw new SchemaError(where, "the interface has instances, pass the one that is reported: { instance: \"Blind.Lift\" }"); } return this.setRaw(descriptor.namespace, described.name, described.value.parse(value, where), { ...options, instance }); } /** A property as given, nothing checked: for an interface or a value the library does not describe. */ setRaw(namespace: string, name: string, value: unknown, options: PropertyOptions = {}): this { return this.add(property(namespace, name, value, { ...options, timeOfSample: options.timeOfSample ?? this.now() })); } /** The connectivity of Alexa.EndpointHealth, which belongs in every report of an endpoint that declares it. */ health(value: "OK" | "UNREACHABLE", reason?: (typeof CONNECTIVITY_REASONS)[number], options: PropertyOptions = {}): this { return this.set(EndpointHealth, "connectivity", reason ? { value, reason } : { value }, options); } /** A property built elsewhere, to the list that is filled now or to the one named. */ add(built: Property, target: Target = this.target): this { this.lists[target].push(built); return this; } /** The properties that follow are the ones that changed. */ changed(): this { this.target = "change"; return this; } /** The properties that follow did not change: a ChangeReport lists them in its context. */ unchanged(): this { this.target = "context"; return this; } /** The properties for the context of the message, in the order they were set. */ get context(): Property[] { return [...this.lists.context]; } /** The properties for payload.change of a ChangeReport. */ get change(): Property[] { return [...this.lists.change]; } }