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wB,GAAG,MAAM,CAAC;IAC7C,KAAK,EAAE,mBAAmB;IAC1B,IAAI,EAAE,MAAM,EAAE;IACd,MAAM,EAAE,MAAM,EAAE;CACjB,CAAC,CAAC;AAWH,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CACzC,kBAAkB,EAClB,MAAM,CAAC;IACL,IAAI,EAAE,OAAO,CAAC,kBAAkB,CAAC,eAAe,CAAC;IACjD,KAAK,EAAE,MAAM,CACX,MAAM,EAAE,EACR,QAAQ,CACN,KAAK,CAAC;QACJ,MAAM,EAAE;QACR,UAAU;QACV,OAAO,EAAE;QACT,0BAA0B;QAC1B,wBAAwB;KACzB,CAAC,CACH,CACF;IACD,IAAI,EAAE,MAAM,EAAE;CACf,CAAC,CACH,CAAC;AAiBF,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAC1C,kBAAkB,EAClB,MAAM,CAAC;IACL,IAAI,EAAE,OAAO,CAAC,kBAAkB,CAAC,gBAAgB,CAAC;IAClD,IAAI,EAAE,MAAM,EAAE;IACd,KAAK,EAAE,QAAQ,CACb,KAAK,CAAC;QACJ,MAAM,EAAE;QACR,OAAO,EAAE;QACT,0BAA0B;QAC1B,wBAAwB;KACzB,CAAC,CACH;CACF,CAAC,CACH,CAAC;AAYF,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CACzC,kBAAkB,EAClB,MAAM,CAAC;IACL,IAAI,EAAE,OAAO,CAAC,kBAAkB,CAAC,eAAe,CAAC;IACjD,IAAI,EAAE,MAAM,EAAE;IACd,IAAI,EAAE,QAAQ,CAAC,UAAU,CAAC;CAC3B,CAAC,CACH,CAAC;AAcF,MAAM,CAAC,MAAM,oBAAoB,GAAG,UAAU,CAAC;IAC7C,sBAAsB;IACtB,qBAAqB;IACrB,sBAAsB;IACtB,qBAAqB;CACtB,CAAC,CAAC","sourcesContent":["import type { Infer } from '@metamask/superstruct';\nimport {\n  number,\n  assign,\n  nullable,\n  object,\n  optional,\n  record,\n  string,\n  union,\n  boolean,\n  array,\n} from '@metamask/superstruct';\nimport { CaipAssetTypeStruct, CaipAccountIdStruct } from '@metamask/utils';\n\nimport { typedUnion, literal } from '../../internals';\nimport type { InterfaceContext } from '../interface';\n\n/**\n * The type of user input event fired.\n * Currently only three events are supported:\n *\n * - `ButtonClickEvent` - A button has been clicked in the UI.\n * - `FormSubmitEvent` - A Form has been submitted in the UI.\n * - `InputChangeEvent` - The value of an input field has changed in the UI.\n * - `FileUploadEvent` - A file has been uploaded in the UI.\n */\nexport enum UserInputEventType {\n  ButtonClickEvent = 'ButtonClickEvent',\n  FormSubmitEvent = 'FormSubmitEvent',\n  InputChangeEvent = 'InputChangeEvent',\n  FileUploadEvent = 'FileUploadEvent',\n}\n\nexport const GenericEventStruct = object({\n  type: string(),\n  name: optional(string()),\n});\n\nexport const ButtonClickEventStruct = assign(\n  GenericEventStruct,\n  object({\n    type: literal(UserInputEventType.ButtonClickEvent),\n    name: optional(string()),\n  }),\n);\n\n/**\n * A button click event fired in the UI. This is passed to the params of the\n * `onUserInput` handler.\n *\n * @property type - The type of event fired. See {@link UserInputEventType} for\n * the different types. This is always `ButtonClickEvent`.\n * @property name - The optional component name that fired the event.\n */\nexport type ButtonClickEvent = Infer<typeof ButtonClickEventStruct>;\n\nexport const AccountSelectorStateStruct = object({\n  accountId: string(),\n  addresses: array(CaipAccountIdStruct),\n});\n\n/**\n * The state of an `AccountSelector` component.\n *\n * @property accountId - The account ID of the account.\n * @property addresses - The addresses of the account as CAIP-10 account ID.\n */\nexport type AccountSelectorState = Infer<typeof AccountSelectorStateStruct>;\n\nexport const FileStruct = object({\n  name: string(),\n  size: number(),\n  contentType: string(),\n  contents: string(),\n});\n\n/**\n * A file object containing the file name, size, content type, and the base64\n * encoded contents of the file.\n *\n * @property name - The name of the file.\n * @property size - The size of the file in bytes.\n * @property contentType - The content type of the file.\n * @property contents - The base64 encoded contents of the file.\n */\nexport type File = Infer<typeof FileStruct>;\n\nexport const AssetSelectorStateStruct = object({\n  asset: CaipAssetTypeStruct,\n  name: string(),\n  symbol: string(),\n});\n\n/**\n * The state of the asset selector component.\n *\n * @property asset - The CAIP-19 asset ID.\n * @property name - The name of the asset.\n * @property symbol - The symbol of the asset.\n */\nexport type AssetSelectorState = Infer<typeof AssetSelectorStateStruct>;\n\nexport const FormSubmitEventStruct = assign(\n  GenericEventStruct,\n  object({\n    type: literal(UserInputEventType.FormSubmitEvent),\n    value: record(\n      string(),\n      nullable(\n        union([\n          string(),\n          FileStruct,\n          boolean(),\n          AccountSelectorStateStruct,\n          AssetSelectorStateStruct,\n        ]),\n      ),\n    ),\n    name: string(),\n  }),\n);\n\n/**\n * A form submit event, which is fired when a submit button is clicked.\n *\n * @property type - The type of event fired. This is always `FormSubmitEvent`.\n * @property name - The name of the form that was submitted.\n * @property value - The form values submitted as an object. The keys are the\n * names of the form fields and the values are the values of the form fields. If\n * a form field is empty, the value is `null` or an empty string.\n * @property files - The files uploaded in the form. The keys are the names of\n * the file input fields and the values are the file objects containing the file\n * name, size, content type, and the base64 encoded contents of the file. See\n * {@link File}.\n */\nexport type FormSubmitEvent = Infer<typeof FormSubmitEventStruct>;\n\nexport const InputChangeEventStruct = assign(\n  GenericEventStruct,\n  object({\n    type: literal(UserInputEventType.InputChangeEvent),\n    name: string(),\n    value: nullable(\n      union([\n        string(),\n        boolean(),\n        AccountSelectorStateStruct,\n        AssetSelectorStateStruct,\n      ]),\n    ),\n  }),\n);\n\n/**\n * An input change event, which is fired when the value of an input field\n * changes.\n *\n * @property type - The type of event fired. This is always `InputChangeEvent`.\n * @property name - The name of the input field that changed.\n * @property value - The new value of the input field.\n */\nexport type InputChangeEvent = Infer<typeof InputChangeEventStruct>;\n\nexport const FileUploadEventStruct = assign(\n  GenericEventStruct,\n  object({\n    type: literal(UserInputEventType.FileUploadEvent),\n    name: string(),\n    file: nullable(FileStruct),\n  }),\n);\n\n/**\n * A file upload event, which is fired when a file is uploaded.\n *\n * @property type - The type of event fired. This is always `FileUploadEvent`.\n * @property name - The name of the file input field that was used to upload the\n * file.\n * @property file - The file object containing the file name, size,\n * content type, and the base64 encoded contents of the file.\n * @see File\n */\nexport type FileUploadEvent = Infer<typeof FileUploadEventStruct>;\n\nexport const UserInputEventStruct = typedUnion([\n  ButtonClickEventStruct,\n  FormSubmitEventStruct,\n  InputChangeEventStruct,\n  FileUploadEventStruct,\n]);\n\n/**\n * A user input event fired in the UI. This is passed to the params of the `onUserInput` handler.\n *\n * @property type - The type of event fired. See {@link UserInputEventType} for the different types.\n * @property name - The component name that fired the event. It is optional for\n * an {@link UserInputEventType.ButtonClickEvent}.\n * @property value - The value associated with the event. Only available when an\n * {@link UserInputEventType.FormSubmitEvent} is fired. It contains the form values submitted.\n */\nexport type UserInputEvent =\n  | ButtonClickEvent\n  | FormSubmitEvent\n  | InputChangeEvent\n  | FileUploadEvent;\n\n/**\n * The `onUserInput` handler. This is called when an user input event is fired in the UI.\n *\n * @param args - The user input event.\n * @param args.id - The user interface id.\n * @param args.event - The {@link UserInputEvent} object, containing the data about the fired event.\n */\nexport type OnUserInputHandler = (args: {\n  id: string;\n  event: UserInputEvent;\n  context: InterfaceContext | null;\n}) => Promise<void>;\n"]}