{"version":3,"file":"unsubscribeHandler.cjs","sources":["../../../../../src/methods/ESPRMUser/EventSubscriptionModel/unsubscribeHandler.ts"],"sourcesContent":["/*\n * SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD\n *\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { ESPRMUser } from \"@espressif/rainmaker-base-sdk\";\nimport { ESPRMMatterEventType } from \"../../../types/subscription\";\nimport { ESPRMUnsubscribeMethod, EventModelContext } from \"./types\";\n\n/**\n * Builds a Matter-aware `unsubscribe` wrapper.\n *\n * Flow:\n * 1) Non-Matter event -> forward to parent unsubscribe (or local fallback remove).\n * 2) Matter event -> remove callback from SDK registry.\n * 3) Recompute remaining Matter callback counts and teardown:\n *    - stop only lost-listener when lost callbacks reach zero\n *    - stop full discovery when both discovery and lost callbacks are zero\n */\nexport function createUnsubscribeHandler(\n  user: ESPRMUser,\n  originalUnsubscribe: ESPRMUnsubscribeMethod | undefined,\n  context: EventModelContext\n): ESPRMUnsubscribeMethod {\n  return (event, callback): void => {\n    // Case 1: non-Matter event. Preserve parent behavior.\n    if (!context.isMatterDiscoveryEvent(event)) {\n      if (typeof originalUnsubscribe === \"function\") {\n        originalUnsubscribe.call(user, event, callback);\n        return;\n      }\n      // Fallback when parent method is unavailable.\n      context.removeCallback(user, event, callback);\n      return;\n    }\n\n    // Case 2: Matter event path.\n    context.removeCallback(user, event, callback);\n\n    // Case 3: evaluate whether listeners/discovery should be torn down.\n    const localDiscoveryCount = context.callbackCount(\n      user,\n      ESPRMMatterEventType.matterLocalDiscovery\n    );\n    const localDiscoveryLostCount = context.callbackCount(\n      user,\n      ESPRMMatterEventType.matterLocalDiscoveryLost\n    );\n\n    // No lost callbacks left -> stop only lost-listener path.\n    if (localDiscoveryLostCount === 0) {\n      context.stopLostListener(user);\n    }\n\n    // No discovery callbacks of either type left -> stop full discovery lifecycle.\n    if (localDiscoveryCount === 0 && localDiscoveryLostCount === 0) {\n      context.stopDiscovery(user);\n    }\n  };\n}\n\n"],"names":["ESPRMMatterEventType"],"mappings":";;;;AAAA;;;;AAIG;AAMH;;;;;;;;;AASG;SACa,wBAAwB,CACtC,IAAe,EACf,mBAAuD,EACvD,OAA0B,EAAA;AAE1B,IAAA,OAAO,CAAC,KAAK,EAAE,QAAQ,KAAU;;QAE/B,IAAI,CAAC,OAAO,CAAC,sBAAsB,CAAC,KAAK,CAAC,EAAE;AAC1C,YAAA,IAAI,OAAO,mBAAmB,KAAK,UAAU,EAAE;gBAC7C,mBAAmB,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,QAAQ,CAAC;gBAC/C;YACF;;YAEA,OAAO,CAAC,cAAc,CAAC,IAAI,EAAE,KAAK,EAAE,QAAQ,CAAC;YAC7C;QACF;;QAGA,OAAO,CAAC,cAAc,CAAC,IAAI,EAAE,KAAK,EAAE,QAAQ,CAAC;;AAG7C,QAAA,MAAM,mBAAmB,GAAG,OAAO,CAAC,aAAa,CAC/C,IAAI,EACJA,0BAAoB,CAAC,oBAAoB,CAC1C;AACD,QAAA,MAAM,uBAAuB,GAAG,OAAO,CAAC,aAAa,CACnD,IAAI,EACJA,0BAAoB,CAAC,wBAAwB,CAC9C;;AAGD,QAAA,IAAI,uBAAuB,KAAK,CAAC,EAAE;AACjC,YAAA,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC;QAChC;;QAGA,IAAI,mBAAmB,KAAK,CAAC,IAAI,uBAAuB,KAAK,CAAC,EAAE;AAC9D,YAAA,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC;QAC7B;AACF,IAAA,CAAC;AACH;;;;"}