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CAAC;AAWU,QAAA,qBAAqB,GAAG,IAAA,oBAAM,EACzC,0BAAkB,EAClB,IAAA,oBAAM,EAAC;IACL,IAAI,EAAE,IAAA,mBAAO,EAAC,kBAAkB,CAAC,eAAe,CAAC;IACjD,KAAK,EAAE,IAAA,oBAAM,EACX,IAAA,oBAAM,GAAE,EACR,IAAA,sBAAQ,EACN,IAAA,mBAAK,EAAC;QACJ,IAAA,oBAAM,GAAE;QACR,kBAAU;QACV,IAAA,qBAAO,GAAE;QACT,kCAA0B;QAC1B,gCAAwB;KACzB,CAAC,CACH,CACF;IACD,IAAI,EAAE,IAAA,oBAAM,GAAE;CACf,CAAC,CACH,CAAC;AAiBW,QAAA,sBAAsB,GAAG,IAAA,oBAAM,EAC1C,0BAAkB,EAClB,IAAA,oBAAM,EAAC;IACL,IAAI,EAAE,IAAA,mBAAO,EAAC,kBAAkB,CAAC,gBAAgB,CAAC;IAClD,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,KAAK,EAAE,IAAA,sBAAQ,EACb,IAAA,mBAAK,EAAC;QACJ,IAAA,oBAAM,GAAE;QACR,IAAA,qBAAO,GAAE;QACT,kCAA0B;QAC1B,gCAAwB;KACzB,CAAC,CACH;CACF,CAAC,CACH,CAAC;AAYW,QAAA,qBAAqB,GAAG,IAAA,oBAAM,EACzC,0BAAkB,EAClB,IAAA,oBAAM,EAAC;IACL,IAAI,EAAE,IAAA,mBAAO,EAAC,kBAAkB,CAAC,eAAe,CAAC;IACjD,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,IAAI,EAAE,IAAA,sBAAQ,EAAC,kBAAU,CAAC;CAC3B,CAAC,CACH,CAAC;AAcW,QAAA,oBAAoB,GAAG,IAAA,sBAAU,EAAC;IAC7C,8BAAsB;IACtB,6BAAqB;IACrB,8BAAsB;IACtB,6BAAqB;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"]}