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,MAAM,CAAC,EAAE,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;gBACrD,IAAI,KAAK,CAAC,SAAS,GAAG,MAAM,EAAE,CAAC;oBAC7B,OAAO,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;gBACxB,CAAC;YACH,CAAC;YACD,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC;QACjC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,IAAI,GAAG,EAAE,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA1ED,8CA0EC","sourcesContent":["import type {\n  ControllerGetStateAction,\n  ControllerStateChangeEvent,\n  StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport { Duration, inMilliseconds } from '@metamask/utils';\nimport { v4 as uuid } from 'uuid';\n\nimport type { LoggingControllerMethodActions } from './LoggingController-method-action-types.js';\nimport type { Log } from './logTypes/index.js';\n\n/**\n * LogEntry is the entry that will be added to the logging controller state.\n * It consists of a entry key that must be on of the Log union types, and an\n * additional id and timestamp.\n */\nexport type LogEntry = {\n  id: string;\n  timestamp: number;\n  log: Log;\n};\n\n/**\n * Logging controller state\n *\n * @property logs - An object of logs indexed by their ids\n */\nexport type LoggingControllerState = {\n  logs: {\n    [id: string]: LogEntry;\n  };\n};\n\nconst name = 'LoggingController';\n\nconst MESSENGER_EXPOSED_METHODS = ['add', 'clear'] as const;\n\nexport type LoggingControllerGetStateAction = ControllerGetStateAction<\n  typeof name,\n  LoggingControllerState\n>;\n\nexport type LoggingControllerActions =\n  | LoggingControllerGetStateAction\n  | LoggingControllerMethodActions;\n\nexport type LoggingControllerStateChangeEvent = ControllerStateChangeEvent<\n  typeof name,\n  LoggingControllerState\n>;\n\nexport type LoggingControllerEvents = LoggingControllerStateChangeEvent;\n\nexport type LoggingControllerMessenger = Messenger<\n  typeof name,\n  LoggingControllerActions,\n  LoggingControllerEvents\n>;\n\nconst metadata: StateMetadata<LoggingControllerState> = {\n  logs: {\n    includeInStateLogs: true,\n    persist: true,\n    includeInDebugSnapshot: false,\n    usedInUi: false,\n  },\n};\n\nconst defaultState = {\n  logs: {},\n};\n\n/**\n * Controller that manages a list of logs for signature requests.\n */\nexport class LoggingController extends BaseController<\n  typeof name,\n  LoggingControllerState,\n  LoggingControllerMessenger\n> {\n  readonly #expiryTime: number;\n\n  /**\n   * Creates a LoggingController instance.\n   *\n   * @param options - Constructor options\n   * @param options.messenger - An instance of the Messenger\n   * @param options.state - Initial state to set on this controller.\n   * @param options.expiryTime - The number of milliseconds before we consider a log entry expired.\n   */\n  constructor({\n    messenger,\n    state,\n    expiryTime = inMilliseconds(7, Duration.Day),\n  }: {\n    messenger: LoggingControllerMessenger;\n    state?: Partial<LoggingControllerState>;\n    expiryTime?: number;\n  }) {\n    super({\n      name,\n      metadata,\n      messenger,\n      state: {\n        ...defaultState,\n        ...state,\n      },\n    });\n\n    this.#expiryTime = expiryTime;\n\n    this.messenger.registerMethodActionHandlers(\n      this,\n      MESSENGER_EXPOSED_METHODS,\n    );\n  }\n\n  /**\n   * Add log to the state.\n   *\n   * @param log - Log to add to the controller\n   */\n  add(log: Log) {\n    const newLog: LogEntry = {\n      id: uuid(),\n      timestamp: Date.now(),\n      log,\n    };\n\n    const expiry = Date.now() - this.#expiryTime;\n\n    this.update((state) => {\n      for (const [id, entry] of Object.entries(state.logs)) {\n        if (entry.timestamp < expiry) {\n          delete state.logs[id];\n        }\n      }\n      state.logs[newLog.id] = newLog;\n    });\n  }\n\n  /**\n   * Removes all log entries.\n   */\n  clear() {\n    this.update((state) => {\n      state.logs = {};\n    });\n  }\n}\n"]}