{"version":3,"file":"setup-subscriptions.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/contact-syncing/setup-subscriptions.ts"],"names":[],"mappings":";;;AAEA,yEAGkC;AAClC,iDAAwD;AAExD,uCAAuD;AAEvD;;;;GAIG;AACH,SAAgB,gCAAgC,CAC9C,OAA8B;IAE9B,MAAM,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC;IAEjC,yEAAyE;IACzE,YAAY,EAAE,CAAC,SAAS,CACtB,sCAAsC,EACtC,CAAC,YAA8B,EAAE,EAAE;QACjC,mEAAmE;QACnE,CAAC,KAAK,IAAI,EAAE;YACV,IACE,CAAC,IAAA,qCAAwB,EAAC,OAAO,CAAC;gBAClC,IAAA,oCAA4B,EAAC,YAAY,CAAC,EAC1C,CAAC;gBACD,OAAO;YACT,CAAC;YAED,IAAI,CAAC;gBACH,sDAAsD;gBACtD,MAAM,IAAA,qDAA4B,EAAC,YAAY,EAAE,OAAO,CAAC,CAAC;YAC5D,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,CAAC,KAAK,CAAC,2CAA2C,EAAE,KAAK,CAAC,CAAC;YACpE,CAAC;QACH,CAAC,CAAC,EAAE,CAAC;IACP,CAAC,CACF,CAAC;IAEF,4EAA4E;IAC5E,YAAY,EAAE,CAAC,SAAS,CACtB,sCAAsC,EACtC,CAAC,YAA8B,EAAE,EAAE;QACjC,mEAAmE;QACnE,CAAC,KAAK,IAAI,EAAE;YACV,IACE,CAAC,IAAA,qCAAwB,EAAC,OAAO,CAAC;gBAClC,IAAA,oCAA4B,EAAC,YAAY,CAAC,EAC1C,CAAC;gBACD,OAAO;YACT,CAAC;YAED,IAAI,CAAC;gBACH,sDAAsD;gBACtD,MAAM,IAAA,qDAA4B,EAAC,YAAY,EAAE,OAAO,CAAC,CAAC;YAC5D,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,CAAC,KAAK,CAAC,6CAA6C,EAAE,KAAK,CAAC,CAAC;YACtE,CAAC;QACH,CAAC,CAAC,EAAE,CAAC;IACP,CAAC,CACF,CAAC;AACJ,CAAC;AAlDD,4EAkDC","sourcesContent":["import type { AddressBookEntry } from '@metamask/address-book-controller';\n\nimport {\n  updateContactInRemoteStorage,\n  deleteContactInRemoteStorage,\n} from './controller-integration';\nimport { canPerformContactSyncing } from './sync-utils';\nimport type { ContactSyncingOptions } from './types';\nimport { isContactBridgedFromAccounts } from './utils';\n\n/**\n * Initialize and setup events to listen to for contact syncing\n *\n * @param options - parameters used for initializing and enabling contact syncing\n */\nexport function setupContactSyncingSubscriptions(\n  options: ContactSyncingOptions,\n): void {\n  const { getMessenger } = options;\n\n  // Listen for contact updates and immediately sync the individual contact\n  getMessenger().subscribe(\n    'AddressBookController:contactUpdated',\n    (contactEntry: AddressBookEntry) => {\n      // eslint-disable-next-line @typescript-eslint/no-floating-promises\n      (async () => {\n        if (\n          !canPerformContactSyncing(options) ||\n          isContactBridgedFromAccounts(contactEntry)\n        ) {\n          return;\n        }\n\n        try {\n          // Use the targeted method to update just this contact\n          await updateContactInRemoteStorage(contactEntry, options);\n        } catch (error) {\n          console.error('Error updating contact in remote storage:', error);\n        }\n      })();\n    },\n  );\n\n  // Listen for contact deletions and immediately sync the individual deletion\n  getMessenger().subscribe(\n    'AddressBookController:contactDeleted',\n    (contactEntry: AddressBookEntry) => {\n      // eslint-disable-next-line @typescript-eslint/no-floating-promises\n      (async () => {\n        if (\n          !canPerformContactSyncing(options) ||\n          isContactBridgedFromAccounts(contactEntry)\n        ) {\n          return;\n        }\n\n        try {\n          // Use the targeted method to delete just this contact\n          await deleteContactInRemoteStorage(contactEntry, options);\n        } catch (error) {\n          console.error('Error deleting contact from remote storage:', error);\n        }\n      })();\n    },\n  );\n}\n"]}